{include file="./index_top.tpl"}
<script language="JavaScript" type="text/javascript">
    // значения для системной подсказки
    var help_section = 'Settings';
    var help_sub_section = 'Users';

    function check_params() {
        var user_login = document.getElementById('user_login');
        var user_fio = document.getElementById('user_fio');
        var user_mail = document.getElementById('user_mail');

        if ( user_login.value.includes('<script>') ||
            user_fio.value.includes('<script>') ||
            user_mail.value.includes('<script>')
        ) {
          jalert('{$lang.script_found}');
          return false;
        }

        {if $editable_fields.login}
        if (user_login.value == "") {
            jalert('{$lang.alert_enter_login}');
            return false;
        }
        {/if}

        {if $editable_fields.fio}
        if (user_fio.value == "") {
            jalert('{$lang.alert_enter_name}');
            return false;
        }
        {/if}

        {if $editable_fields.e_mail}
        if (user_mail.value == "") {
            jalert('{$lang.alert_enter_email}');
            return false;
        }
        {/if}
        return true;
    }
</script>

<div align=center>

    <br>{$lang.Edit_user}<br><br>

    {if $error}
        <div style="color:red; font-size:13px;">{$error}</div>
        <br>
    {elseif $message}
        <div style="color:green; font-size:13px;">{$message}</div>
        <br>
    {/if}
    {if $config.rules_password!=0}
        <script language="JavaScript" type="text/javascript">
            $(document).ready(function () {
                    $("#user_confirm_password").on('keyup', function () {
                        if ($("#user_confirm_password").val() != "" && $("#user_password").val() != "" && $("#user_confirm_password").val() != "--------" && $("#user_password").val() != "--------") {
                            if ($("#user_password").val() == $("#user_confirm_password").val())
                                $("#user_confirm_password_result").html("<img src='images/tick.png' alt=''>");
                            else
                                $("#user_confirm_password_result").html("<img src='images/cross.png' alt=''>");
                        }
                        else $("#user_confirm_password_result").html("");
                    });

                    var strPassword;
                    var charPassword;
                    var passwordCheck;
                    var strRulesPassword;
                    var complexity = $("#user_password_result");
                    var inputPassword = $("#user_password");
                    var minPasswordLength = {if $config.rules_password_not_less_characters}{$config.rules_password_not_less_characters}{else}6{/if};

                    var num = {};
                    num.Letters = 0;
                    num.Upper = 0;
                    num.Numbers = 0;
                    num.Symbols = 0;

                    checkVal();
                    inputPassword.bind("keyup", checkVal);

                    function checkVal() {
                        if ($("#user_confirm_password").val() != "" && $("#user_password").val() != "" && $("#user_confirm_password").val() != "--------" && $("#user_password").val() != "--------") {
                            if ($("#user_password").val() == $("#user_confirm_password").val())
                                $("#user_confirm_password_result").html("<img src='images/tick.png' alt=''>");
                            else
                                $("#user_confirm_password_result").html("<img src='images/cross.png' alt=''>");
                        }
                        else $("#user_confirm_password_result").html("");

                        strPassword = inputPassword.val();
                        charPassword = strPassword.split("");

                        num.Letters = 0;
                        num.Upper = 0;
                        num.Numbers = 0;
                        num.Symbols = 0;
                        passwordCheck = 1;
                        strRulesPassword = "";

                        for (i = 0; i < charPassword.length; i++) {
                            if (charPassword[i].match(/[a-z,а-я]/g)) num.Letters++;
                            if (charPassword[i].match(/[A-Z,А-Я]/g)) num.Upper++;
                            if (charPassword[i].match(/[0-9]/g)) num.Numbers++;
                            if (charPassword[i].match(/(.*[!,@,#,$,%,^,&,*,?,_,~,+,-,=,<,>])/)) num.Symbols++;
                        }

                        if ((strPassword == '--------') || strPassword == '')
                            complexity.html("");
                        else {
                            if (charPassword.length < minPasswordLength) passwordCheck = 0;
                            {if $config.rules_password_characters_1}if (num.Letters == 0) passwordCheck = 0;
                            strRulesPassword += "{$lang.rules_password_characters_1}";{/if}
                            {if $config.rules_password_characters_2}if (num.Upper == 0) passwordCheck = 0;
                            if (strRulesPassword != "") strRulesPassword += ", ";
                            strRulesPassword += "{$lang.rules_password_characters_2}";{/if}
                            {if $config.rules_password_characters_3}if (num.Numbers == 0) passwordCheck = 0;
                            if (strRulesPassword != "") strRulesPassword += ", ";
                            strRulesPassword += "{$lang.rules_password_characters_3}";{/if}
                            {if $config.rules_password_characters_4}if (num.Symbols == 0) passwordCheck = 0;
                            if (strRulesPassword != "") strRulesPassword += ", ";
                            strRulesPassword += "{$lang.rules_password_characters_4}";{/if}
                            {if $config.rules_password_characters_1 or $config.rules_password_characters_2 or $config.rules_password_characters_3 or $config.rules_password_characters_4}
                            strRulesPassword = "{$lang.rules_password_result_3}" + strRulesPassword;
                            {/if}
                            if (passwordCheck) complexity.html("<img src='images/tick.png' alt=''>").removeClass("weak result_password").addClass("stronger result_confirm_password");
                            else complexity.html("{$lang.rules_password_result_1}" + minPasswordLength + "{$lang.rules_password_result_2}" + strRulesPassword).removeClass("stronger result_confirm_password").addClass("weak result_password");
                        }
                    }
                }
            );
        </script>
    {/if}
    <style type="text/css">
        .weak {
            color: #DD0000;
        }

        .stronger {
            color: #00DD00;
        }

        .result_confirm_password {
            position: absolute;
            margin-left: 5px;
            margin-top: 2px;
        }

        .result_password {
            position: absolute;
            margin-left: 25px;
            margin-top: 2px;
        }
    </style>

    <form action="" method="post" enctype="multipart/form-data" onsubmit="CB.pubsub.publish('submit/user_profile', {$user_data.id});">
        <table border=0 cellpadding=3 cellspacing=0 width=600>
            {if $editable_fields.login}
                <tr>
                    <td align="right">{$lang.Login}<span class='help_bt' h_id='Login'></span>:</td>
                    <td width="50%">
                        <input class="form-control form-control-160" type=text name="user_login" id="user_login"
                               value="{$user_data.login}" autocomplete="off">
                    </td>
                </tr>
            {/if}
            {if $editable_fields.password}
                <tr>
                    <td align="right">{$lang.Password}<span class='help_bt' h_id='Password'></span>:</td>
                    <td>
                        <input class="form-control form-control-160" type=password name="user_password"
                               value="{$user_data.password}" autocomplete="off" id="user_password">
                        <span id="user_password_result" class="result_password"></span>
                    </td>
                </tr>
                <tr>
                    <td align="right">{$lang.Password_again}<span class='help_bt' h_id='Password_again'></span>:</td>
                    <td>
                        <input class="form-control form-control-160" type=password name="user_confirm_password"
                               value="{$user_data.confirm_password}" id="user_confirm_password">
                        <span id="user_confirm_password_result" class="result_confirm_password"></span>
                    </td>
                </tr>
            {/if}
            {if $editable_fields.fio}
                <tr>
                    <td align="right">{$lang.Fillot}<span class='help_bt' h_id='Fillot'></span>:</td>
                    <td>
                        <input class="form-control form-control-160" type=text name="user_fio" id="user_fio"
                               value="{$user_data.fio}">
                    </td>
                </tr>
            {/if}
            {if $editable_fields.e_mail}
                <tr>
                    <td align="right">E-mail<span class='help_bt' h_id='Email'></span>:</td>
                    <td>
                        <input class="form-control form-control-160" type=text name="user_mail" id="user_mail"
                               value="{$user_data.e_mail}">
                    </td>
                </tr>
            {/if}
            {if $editable_fields.phone}
                <tr>
                    <td align="right">{$lang.Phone}<span class='help_bt' h_id='Phone_for_SMS'></span>:</td>
                    <td>
                        <input class="form-control form-control-160" type=text name="user_phone" value="{$user_data.phone}">
                    </td>
                </tr>
            {/if}
            {if $editable_fields.telegram && $all_modules.telegram_notify}
                <tr>
                    <td align="right">{$lang.Telegram_notify}<span class='help_bt' h_id='Telegram_notify'></span>:</td>
                    <td>
                        <input class="form-control form-control-160" type=text name="user_telegram" value="{$user_data.telegram}">
                    </td>
                </tr>
            {/if}
            {if $editable_fields.max_chat && $all_modules.telegram_notify}
                <tr>
                    <td align="right">{$lang.Max_notify}<span class='help_bt' h_id='Max_notify'></span>:</td>
                    <td>
                        <input class="form-control form-control-160" type=text name="user_max" value="{$user_data.max_chat}">
                    </td>
                </tr>
            {/if}
            {if $user.group_id == 1 || $user.sub_admin}
                <tr>
                    <td align="right">{$lang.developer_mode}<span class='help_bt' h_id='developer_mode'></span>:</td>
                    <td>
                        <input type=checkbox name="developer_mode" value=1 {if $user_data.developer_mode}checked{/if}>
                    </td>
                </tr>
            {/if}
            {if $editable_fields.lang}
                <tr>
                    <td align="right">{$lang.system_lang}<span class='help_bt' h_id='system_lang'></span>:</td>
                    <td>
                        <select class="form-control form-control-160" name="system_lang">
                            {foreach from=$langs_list item=lang_name key=sname}
                                <option value="{$sname}" {if $sname==$user_data.lang}selected{/if}>{$lang_name}</option>
                            {/foreach}
                        </select>
                    </td>
                </tr>
            {/if}
            {if $editable_fields.time_zone}
                <tr>
                    <td align="right">{$lang.time_zone}<span class='help_bt' h_id='time_zone'></span>:</td>
                    <td>
                        <select class="form-control form-control-160" name="time_zone">
                            {foreach from=$tz_list item=tz_name key=time_zone}
                                <option value="{$time_zone}"
                                        {if $time_zone==$user_data.time_zone}selected{/if}>{$tz_name}</option>
                            {/foreach}
                        </select>
                    </td>
                </tr>
            {/if}
            {if $editable_fields.window_scroll}
                <!--tr>
                    <td align="right">{$lang.window_scroll}<span class='help_bt' h_id='window_scroll'></span>:</td>
                    <td>
                        <select class="form-control form-control-160" name="window_scroll">
                            {foreach from=$ws_list item=ws_name key=ws_mode}
                                <option value="{$ws_mode}"
                                        {if $ws_mode==$user_data.window_scroll}selected{/if}>{$ws_name}</option>
                            {/foreach}
                        </select>
                    </td>
                </tr-->
            {/if}
            {if $editable_fields.sound_on}
                <tr>
                    <td align="right">{$lang.sound_notify}<span class='help_bt' h_id='sound_notify'></span>:</td>
                    <td>
                        <input type=checkbox name="sound_notify" value=1 {if $user_data.sound_on}checked{/if}>
                    </td>
                </tr>
            {/if}
            {if $editable_fields.cut_notify_text}
                <tr>
                    <td align="right">{$lang.cut_notify_text}<span class='help_bt' h_id='cut_notify_text'></span>:</td>
                    <td>
                        <input type=checkbox name="cut_notify_text" value=1
                               {if $user_data.cut_notify_text || $user_data.id eq "-1"}checked{/if}>
                    </td>
                </tr>
            {/if}
            {if $editable_fields.display_notification_on}
                <tr>
                    <td align="right">{$lang.display_notification_on}<span class='help_bt' h_id='display_notification_on'></span>:</td>
                    <td>
                        <input type=checkbox name="display_notification_on" value=1
                               {if $user_data.display_notification_on}checked{/if}>
                    </td>
                </tr>
            {/if}
            {if $editable_fields.user_avatar && $config.enable_new_design}
                <tr>
                    <td align="right">{$lang.user_avatar}<span class='help_bt' h_id='user_avatar'></span>:</td>
                    <td>
                        <input type="file" name="user_avatar" class="jfilestyle" data-input="false">
                        <span class="btn-delete"
                        onclick="delete_avatar({$user_data.id})"
                        style="{if !$user_data.avatar}display:none{/if}"
                        title="{$lang.Delete_avatar}"></span>
                    </td>
                </tr>
            {/if}
            {if $editable_fields.default_page}
                <tr>
                    <td align="right">{$lang.default_page}<span class='help_bt' h_id='default_page'></span>:</td>
                    <td>
                        <select class="form-control form-control-160" name="default_page">
                            <option value=" ">{$lang.absent_page}</option>
                            {foreach from=$dp_list item=dp_gr key=dp_gr_key}
                                <optgroup
                                        label="{if $lang.conf.{$dp_gr.name}}{$lang.conf.{$dp_gr.name}}{else}{$dp_gr.name}{/if}">
                                    {foreach from=$dp_gr.contents item=dp_item key=dp_item_key}
                                        <option value="{$dp_item.id}"
                                                {if $dp_item.id==$user_data.default_page}selected{/if}>
                                            {if $lang.conf.{$dp_item.name}}{$lang.conf.{$dp_item.name}}{else}{$dp_item.name}{/if}
                                            {if $dp_item.type=='r'} {$lang.report_page} {/if}
                                            {if $dp_item.type=='c'} {$lang.calendar_page} {/if}
                                        </option>
                                    {/foreach}
                                </optgroup>
                            {/foreach}
                        </select>
                    </td>
                </tr>
            {/if}

            {if $editable_fields.api_on}
            {if $php_version == '5.3'}
                <tr id="api_error">
                    <td align="right"></td>
                    <td><span style="color: red;">{$lang.api_error}</span></td>
                </tr>
            {/if}
            {if $user_data.telegram_on}
                <tr id="telegram_on">
                    <td align="right">{$lang.telegram_on}<span class='help_bt' h_id='telegram_on'></span>:</td>
                    <td><a href="{$user_data.telegram_url}" target="_blank">{$lang.Enter}</a></td>
                </tr>
            {/if}
            {/if}
            {if $config.push_on}
                <tr>
                    <td style="height:29px;" align="right">{$lang['Unsubscribe_from_push_notifications']}<span class='help_bt' h_id='unsubscribe_from_push'></span>:</td>
                    <td>
                        <div id="push_off">
                            {if $subscribe}
                            {$lang['unsubscribe_from_push']}
                            {else}
                            <button class="btn btn-default btn-sm" onclick="if (unsubscribe_from_push()) { return false; }">{$lang['unsubscribe']}</button>
                            {/if}
                        </div>
                    </td>
                </tr>
            {/if}
            {if $editable_fields.default_channel}
            {$wazzup_extension}
            {/if}
        </table>
        <br>
        <input type=hidden name="csrf" value='{$csrf}'>
        <input class="btn btn-default btn-sm" type=submit name="save" value="{$lang.Save}" onclick="if (!check_params()) return false;">
        <br><br>
    </form>
</div>

<script>
    function unsubscribe_from_push() {
        if (typeof messaging !== 'undefined') {
            messaging.deleteToken();
            messaging.getToken().then((currentToken) => {});
        }
        $('#push_off').text(lang['unsubscribe_from_push']);
        document.cookie = "subscribe=unsubscribe; expires=" + (new Date(Date.now() + 9e11));
    }
</script>
