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

    var cur_system_lang = '{$config.system_lang}';
    function set_formats() {
        jconfirm('{$lang.change_format_alert}',
            function() {
                cur_system_lang = $('[name="system_lang"]').val();
                $('[value="' + $('[value="' + cur_system_lang + '"]').attr('data-date-format') + '"]').prop('selected', true);
                $('[value="' + $('[value="' + cur_system_lang + '"]').attr('data-number-format') + '"]').prop('selected', true);
            },
            function() {
                $('[value="' + cur_system_lang + '"]').prop('selected', true);
            }
        );
    }

    function switch_add_set() {
        $('#add_set_link').html($('#add_set_link').html().trim() == '{$lang.Additional}' ? '{$lang.Hide}' : '{$lang.Additional}');
        $('#add_set').toggle();
    }
</script>
<div align=center>
    <br>{$lang.Common_settings}<span class='help_bt' h_id='Common_settings'></span><br><br>
    <form action="edit_config.php" method="post" id="edit_config_form">
        <table border=0 cellpadding=3>
            {if $message}
                <tr>
                    <td colspan=2 align=center>
                        <span id="edit_config__info-maessage" style="color:green">{$message}</span><br><br>
                    </td>
                </tr>
            {/if}
            <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" onchange="set_formats()">
                        {foreach from=$langs_list item=one_lang}
                            <option value="{$one_lang.sname}"
                                    data-date-format="{$one_lang.date_format_default}"
                                    data-number-format="{$one_lang.number_format_default}"
                                    {if $one_lang.sname==$config.system_lang}selected{/if}>{$one_lang.name}</option>
                        {/foreach}
                    </select>
                </td>
            </tr>
            <tr>
                <td align=right>{$lang.Date_format}<span class='help_bt' h_id='date_format'></span>:</td>
                <td>
                    <select class="form-control form-control-160" name="date_format">
                        {foreach from=$date_formats item=one_format key=name_format}
                            <option value="{$name_format}"
                                    {if $config.date_format == $name_format}selected{/if}>{$name_format}</option>
                        {/foreach}
                    </select>
                </td>
            </tr>
            <tr>
                <td align=right>{$lang.Number_format}<span class='help_bt' h_id='number_format'></span>:</td>
                <td>
                    <select class="form-control form-control-160" name="number_format">
                        {foreach from=$number_formats item=one_format key=name_format}
                            <option value="{$name_format}"
                                    {if $config.number_format == $name_format}selected{/if}>{$name_format}</option>
                        {/foreach}
                    </select>
                </td>
            </tr>
            <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=one_tz}
                            <option value="{$one_tz.name}" {if $one_tz.name==$config.time_zone}selected{/if}>
                                UTC{$one_tz.name}</option>
                        {/foreach}
                    </select>
                </td>
            </tr>

            {if $user.group_id == 1}
            <tr><td colspan="2"></td></tr>
            <tr>
                <td align=center colspan="2">
                    <a href="" id="add_set_link" onclick="switch_add_set(); return false;">{$lang.Additional}</a>
                </td>
            </tr>

            <tbody id="add_set" style="display:none">
            <tr><td colspan="2"></td></tr>
            <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">
                        <option value="0">{$lang.simple_scroll}</option>
                        <option value="1" {if $window_scroll==1}selected{/if}>{$lang.extended_scroll}</option>
                    </select>
                </td>
            </tr>
            {* скрыли в связи с новой опцией только быстрого
            <tr>
                <td align=right>{$lang.view_fast_edit}<span class='help_bt' h_id='view_fast_edit'></span>:</td>
                <td>
                    <input type="checkbox" name="view_fast_edit" value=1 {if $config.view_fast_edit}checked{/if}>
                </td>
            </tr>
            *}
            <tr>
                <td align=right>{$lang.multi_columns}<span class='help_bt' h_id='multi_columns'></span>:</td>
                <td>
                    <input type="checkbox" name="multi_columns" value=1 {if $config.multi_columns}checked{/if}>
                </td>
            </tr>
            <tr>
                <td align=right>{$lang.confirm_dispatch}<span class='help_bt' h_id='confirm_dispatch'></span>:</td>
                <td>
                    <input type="checkbox" name="confirm_dispatch" {if $config.confirm_dispatch}checked{/if} value=1>
                </td>
            </tr>
            <tr>
                <td align=right>{$lang.Hide_not_edit_fields_on_view_line}<span class='help_bt' h_id='hide_not_edit'></span>:</td>
                <td>
                    <input type="checkbox" name="hide_not_edit" value=1 {if $config.hide_not_edit}checked{/if}>
                </td>
            </tr>
            <tr>
                <td align=right>{$lang.Single_user_mode}<span class='help_bt' h_id='single_user_mode'></span>:</td>
                <td>
                    <input type="checkbox" name="single_user_mode" value=1 {if $config.single_user_mode}checked{/if}>
                </td>
            </tr>
            {if $config.type eq "SAAS"}
                <tr>
                    <td align=right>{$lang.Livechat_in_header}<span class='help_bt' h_id='livechat_in_header'></span>:
                    </td>
                    <td>
                        <input type="checkbox" name="livechat_in_header" value=1
                               {if $config.livechat_in_header}checked{/if}>
                    </td>
                </tr>
            {/if}
            {if $lang.short_name eq "ru"}
                <tr>
                    <td align=right>{$lang.Intuitive_search}<span class='help_bt' h_id='intuitive_search'> </span>:</td>
                    <td>
                        <input type="checkbox" name="intuitive_search" value=1
                               {if $config.intuitive_search}checked{/if}>
                    </td>
                </tr>
            {/if}
            <tr>
                <td align=right>{$lang.fields_context_menu}<span class='help_bt' h_id='fields_context_menu'></span>:
                </td>
                <td>
                    <input type="checkbox" name="fields_context_menu" value=1
                           {if $config.fields_context_menu}checked{/if}>
                </td>
            </tr>
            <tr>
                <td align=right>{$lang.hotkeys}<span class='help_bt' h_id='hotkeys'></span>:</td>
                <td>
                    <input type="checkbox" name="hotkeys" value=1 {if $config.hotkeys}checked{/if}>
                </td>
            </tr>
            <tr>
                <td align=right>{$lang.fix_subtables_headers}<span class='help_bt' h_id='fix_subtables_headers'></span>:</td>
                <td>
                    <input type="checkbox" name="fix_subtables_headers" value=1 {if $config.fix_subtables_headers}checked{/if}>
                </td>
            </tr>
            {if !$config.copyright}
            <tr>
                <td align=right>{$lang.show_alert}<span class='help_bt' h_id='show_alert'></span>:</td>
                <td>
                    <input type="checkbox" name="cb_alert_show[1]"   value=1 {if $config.cb_alert_show.1}  checked{/if}> {$lang.show_alert_administrators}<br>
                    <input type="checkbox" name="cb_alert_show[999]" value=1 {if $config.cb_alert_show.999}checked{/if}> {$lang.show_alert_users}<br>
                    <input type="checkbox" name="cb_alert_show[777]" value=1 {if $config.cb_alert_show.777}checked{/if}> {$lang.show_alert_clients}<br>
                </td>
            </tr>
            {/if}
            <tr>
                <td align=right>{$lang.on_page_rows} {$lang.default}<span class='help_bt' h_id='lop_and_slop_for_all_tables'></span>:</td>
                <td>
                    <input class="form-control form-control-25" type="text" name="common_lop" value="{$config.common_lop}"
                           onchange="if (this.value > 1000) this.value = 1000; if (this.value < 1) this.value = 1;">
                    <input type="button" value="{$lang.Apply_to_all_tables}" class="btn btn-default btn-sm" id="apply_lop"
                           onClick="jconfirm('{$lang.Lop_change_all_tbl}',
                                   function () {
                                   $('#apply_lop_check').val(1);
                                   $('#apply_lop').addClass('btn_aplly-active').removeClass('btn-default');
                                   }, function () {
                                   $('#apply_lop_check').val(0);
                                   $('#apply_lop').addClass('btn-default').removeClass('btn_aplly-active');
                                   });">
                    <input type="hidden" name="apply_lop" id="apply_lop_check" value="0">
                </td>
            </tr>
            <tr>
                <td align=right>{$lang.on_page_rows} {$lang.in_subtable_mode} {$lang.default}<span class='help_bt' h_id='lop_and_slop_for_all_tables'></span>:</td>
                <td>
                    <input class="form-control form-control-25" type="text" name="common_slop" value="{$config.common_slop}"
                           onchange="if (this.value > 1000) this.value = 1000; if (this.value < 1) this.value = 1;">
                    <input type="button" value="{$lang.Apply_to_all_tables}" class="btn btn-default btn-sm" id="apply_slop"
                           onClick="jconfirm('{$lang.Lop_change_all_tbl}',
                                   function () {
                                   $('#apply_slop_check').val(1);
                                   $('#apply_slop').addClass('btn_aplly-active').removeClass('btn-default');
                                   }, function () {
                                   $('#apply_slop_check').val(0);
                                   $('#apply_slop').addClass('btn-default').removeClass('btn_aplly-active');
                                   });">
                    <input type="hidden" name="apply_slop" id="apply_slop_check" value="0">
                </td>
            </tr>
            <tr>
            <td align=right>{$lang.Reduce_headers_to}<span class='help_bt' h_id='reduce_headers'></span>:</td>
                <td>
                    <input class="form-control form-control-25" type="text" name="reduce_headers" value="{$config.reduce_headers}" size="3"> {$lang.rh_symbols}
                </td>
            </tr>
            <tr>
                <td align=right>{$lang.Reduce_headers_title_to}<span class='help_bt' h_id='reduce_headers_title'></span>:</td>
                <td>
                    <input class="form-control form-control-25" type="text" name="reduce_headers_title" value="{$config.reduce_headers_title}" size="3"> {$lang.rh_symbols}
                </td>
            </tr>
            <tr>
                <td align=right class="config__req-field-margin">
                    <span class="config__required-field-star" title="{$lang.required_field}">*</span>
                        {$lang.Field_margin}
                    <span class='help_bt' h_id='field_margin'></span>:</td>
                <td>
                    <input class="form-control form-control-25" type="text" name="field_margin" id="config__field_margin-val" value="{$config.field_margin}" size="3"> {$lang.fixed_scroll_width2}
                </td>
            </tr>
            <tr>
                <td align=right class="config__req-tab-padding">
                    <span class="config__required-field-star" title="{$lang.required_field}">*</span>
                        {$lang.Table_rows_padding}
                    <span class='help_bt' h_id='table_rows_padding'></span>:</td>
                <td>
                    <input class="form-control form-control-25" type="text" name="table_rows_padding" value="{$config.table_rows_padding}" size="3" id="config__table_rows_padding" /> {$lang.fixed_scroll_width2}
                </td>
            </tr>
            <tr>
                <td align=right class="config__req-tab-padding">
                        {$lang.Table_number_min_width}
                    <span class='help_bt' h_id='table_number_min_width'></span>:</td>
                <td>
                    <input class="form-control form-control-25" type="text" name="table_number_min_width" value="{$config.table_number_min_width}" size="3" id="config__table_number_min_width" /> {$lang.fixed_scroll_width2}
                </td>
            </tr>
            <tr>
                <td align=right class="config__req-tab-padding">
                        {$lang.Table_number_max_width}
                    <span class='help_bt' h_id='table_number_max_width'></span>:</td>
                <td>
                    <input class="form-control form-control-25" type="text" name="table_number_max_width" value="{$config.table_number_max_width}" size="3" id="config__table_number_max_width" /> {$lang.fixed_scroll_width2}
                </td>
            </tr>
            <tr>
                <td align=right class="config__req-tab-padding">
                        {$lang.Table_other_min_width}
                    <span class='help_bt' h_id='table_other_min_width'></span>:</td>
                <td>
                    <input class="form-control form-control-25" type="text" name="table_other_min_width" value="{$config.table_other_min_width}" size="3" id="config__table_other_min_width" /> {$lang.fixed_scroll_width2}
                </td>
            </tr>
            <tr>
                <td align=right class="config__req-tab-padding">
                        {$lang.Table_other_max_width}
                    <span class='help_bt' h_id='table_other_max_width'></span>:</td>
                <td>
                    <input class="form-control form-control-25" type="text" name="table_other_max_width" value="{$config.table_other_max_width}" size="3" id="config__table_other_max_width" /> {$lang.fixed_scroll_width2}
                </td>
            </tr>
            <tr>
                <td align=right>{$lang.Reduce_notifications}<span class='help_bt' h_id='reduce_notifications'></span>:</td>
                <td>
                    <input class="form-control form-control-25" type="text" name="reduce_notifications" value="{$config.reduce_notifications}" size="3">
                </td>
            </tr>
            <tr>
                <td align=right>{$lang.delete_tips}<span class='help_bt' h_id='delete_tips'></span>:</td>
                <td>
                    <select class="form-control form-control-160" name="delete_tips">
                        <option value="1" {if $config.delete_tips==1}selected{/if}>1 {$lang.one_month}</option>
                        <option value="3" {if $config.delete_tips==3}selected{/if}>3 {$lang.months}</option>
                        <option value="6" {if $config.delete_tips==6}selected{/if}>6 {$lang.months}</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td align=right>{$lang.display_calendar_events}<span class='help_bt' h_id='display_calendar_events'></span>:</td>
                <td>
                    <select class="form-control form-control-160" name="display_calendar_events">
                        <option value="0" {if $config.display_calendar_events==0}selected{/if}>{$lang.on_click}</option>
                        <option value="1" {if $config.display_calendar_events==1}selected{/if}>{$lang.on_hover}</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td align=right>{$lang.notify_style}<span class='help_bt' h_id='notify_style'></span>:</td>
                <td>
                    <input type="checkbox" name="notify_style" value=1 {if $config.notify_style}checked{/if}>
                </td>
            </tr>
            <tr>
                <td align=right>{$lang.tips_show_table_name}<span class='help_bt' h_id='tips_show_table_name'></span>:</td>
                <td>
                    <input type="checkbox" name="tips_show_table_name" value=1 {if $config.tips_show_table_name}checked{/if}>
                </td>
            </tr>
            <tr>
                <td align=right>{$lang.send_push_notification} <span class='help_bt' h_id='push_on'></span>:</td>
                <td>
                    <input type="checkbox" name="push_on" value=1 {if $config.push_on}checked{/if}>
                </td>
            </tr>
            <tr>
                <td align=right>{$lang.open_links_in_new_tab}<span class='help_bt' h_id='open_links_in_new_tab'></span>:</td>
                <td>
                    <input type="checkbox" name="open_links_in_new_tab" value=1 {if $config.open_links_in_new_tab}checked{/if}>
                </td>
            </tr>
            <tr>
                <td align=right>{$lang.Link_preview_size}<span class='fhelp_bt' h_id='link_preview_size'></span>:</td>
                <td>
                    <input class="form-control form-control-25" type="text" name="link_preview_width" value="{$config.link_preview_width}" size="3">
                    <span>x</span>
                    <input class="form-control form-control-25" type="text" name="link_preview_height" value="{$config.link_preview_height}" size="3">
                </td>
            </tr>
            <tr>
                <td align=right>{$lang.show_filters_list}<span class='help_bt' h_id='show_filters_list'></span>:</td>
                <td>
                    <input type="checkbox" name="show_filters_list" value=1 {if $config.show_filters_list}checked{/if}>
                </td>
            </tr>
            {if $config.enable_new_design}
            <tr>
                <td align=right>{$lang.enable_new_design}<span class='help_bt' h_id='enable_new_design'></span>:</td>
                <td>
                    <input type="checkbox" name="enable_new_design" value=1 {if $config.enable_new_design}checked{/if}>
                </td>
            </tr>
            {/if}
            <tr>
                <td align=right>{$lang.fast_entry_add}<span class='help_bt' h_id='fast_entry_add'></span>:</td>
                <td>
                    <a href="" onclick="$('#fast_entry_add_block').show(); $(this).hide(); return false;">{$lang.show}</a>
                    <div id="fast_entry_add_block" style="display:none">
                    {foreach from=$all_tables item=one_table}
                        <input type="checkbox" name="fast_entry_add[]" value="{$one_table.id}" {if $one_table.fast_entry_add}checked{/if}> {$one_table.name_table}<br>
                    {/foreach}
                    </div>
                </td>
            </tr>
            <tr>
                <td align=right>{$lang.log_run_time}<span class='help_bt' h_id='log_run_time'></span>:</td>
                <td>
                    <input type="checkbox" name="log_calc_runtime" value=1 {if $config.log_calc_runtime}checked{/if}> {$lang.log_calc_runtime}<br>
                    <input type="checkbox" name="log_cron_runtime" value=1 {if $config.log_cron_runtime}checked{/if}> {$lang.log_cron_runtime}<br>
                    <input type="checkbox" name="log_tips_runtime" value=1 {if $config.log_tips_runtime}checked{/if}> {$lang.log_tips_runtime}<br>
                    <input type="checkbox" name="log_reports_runtime" value=1 {if $config.log_reports_runtime}checked{/if}> {$lang.log_reports_runtime}<br>
                    <input type="checkbox" name="log_tables_runtime" value=1 {if $config.log_tables_runtime}checked{/if}> {$lang.log_tables_runtime}<br>
                    <input type="checkbox" name="log_subtables_runtime" value=1 {if $config.log_subtables_runtime}checked{/if}> {$lang.log_subtables_runtime}<br>
                    <input type="checkbox" name="log_save_runtime" value=1 {if $config.log_save_runtime}checked{/if}> {$lang.log_save_runtime}
                </td>
            </tr>
            <tr>
                <td align=right style="vertical-align:top">{$lang.disable_full_edit}<span class='help_bt' h_id='disable_full_edit'></span>:</td>
                <td>
                    <input type="checkbox" name="disable_full_edit" value="1" {if $config.disable_full_edit}checked{/if} {if $calcs}disabled{/if}/>
                    <div style="float:right; margin-left:7px;">
                        {if $calcs}
                        <div style="margin-bottom:5px">
                            {$lang.Save_events_has_alert}<br>
                            <a href="" onclick="$(this).hide(); $('#calc_list').show(); return false;">{$lang.Show_content}</a>
                            <div id="calc_list" style="display:none">
                                {foreach from=$calcs item=calc_name key=calc_id}
                                    <div><a href="edit_calc.php?calc_id={$calc_id}" target="_blank">{$calc_name}</a></div>
                                {/foreach}
                            </div>
                        </div>
                        {/if}
                        {if $js_fields}
                        <div>
                            {$lang.JS_fields_has_alert}<br>
                            <a href="" onclick="$(this).hide(); $('#js_list').show(); return false;">{$lang.Show_content}</a>
                            <div id="js_list" style="display:none">
                                {foreach from=$js_fields item=field_name key=field_id}
                                    <div><a href="edit_field.php?field={$field_id}" target="_blank">{$field_name}</a></div>
                                {/foreach}
                            </div>
                        </div>
                        {/if}
                    </div>
                </td>
            </tr>
            </tbody>
            {/if}

            <tr>
                <td colspan=2 align=center>
                    <br><input class="btn btn-default btn-sm" type="submit" name='save' value="{$lang.Save}">
                    <input type=hidden name="csrf" value="{$csrf}">
                </td>
            </tr>

        </table>
    </form>
    <br>
    <a href="edit_add.php">{$lang.return_to_add_settings}</a><br><br>
</div>

<script>
    $('#edit_config_form').on('submit', function() {
        const configInfoMsg = $('#edit_config__info-maessage');
        const paddingField = $('#config__table_rows_padding');
        const marginField = $('#config__field_margin-val');
        let requiredField = $('.config__req-field-margin');
        let fieldVal = marginField.val().replace(/\s/g, '');
        let errorMsg = lang.Parameter + ' «' + lang.Field_margin + '» ';
        let errors = false;

        // Проверка поля "Отступ полей в записи"
        if (fieldVal.length < 1) {
            errorMsg += lang.can_not_be_empty;
            displayInfoTipAboutRequiredField(requiredField, marginField, configInfoMsg, errorMsg);
            return false;
        } else if (fieldVal === '0') {
            errorMsg += lang.can_not_be_null;
            displayInfoTipAboutRequiredField(requiredField, marginField, configInfoMsg, errorMsg);
            return false;
        } else {
            setSuccessfulCheckStyle(requiredField, marginField, configInfoMsg);
        }

        // Проверка поля "Oтступ строк в таблице"
        requiredField = $('.config__req-tab-padding');
        fieldVal = paddingField.val().replace(/\s/g, '');
        errorMsg = lang.Parameter + ' «' + lang.Table_rows_padding + '» ';
        if (fieldVal.length < 1) {
            errorMsg += lang.can_not_be_empty;
            displayInfoTipAboutRequiredField(requiredField, paddingField, configInfoMsg, errorMsg);
            return false;
        } else if (fieldVal === '0') {
            errorMsg += lang.can_not_be_null;
            displayInfoTipAboutRequiredField(requiredField, paddingField, configInfoMsg, errorMsg);
            return false;
        } else {
            setSuccessfulCheckStyle(requiredField, paddingField, configInfoMsg);
        }

        return true;
    });

    displayInfoTipAboutRequiredField = (row, field, configInfo, msg) => {
        row.addClass('warning');
        field.css('background', 'var(--color-misty-rose)').val('');
        configInfo.text(lang.Saving_error).css('color', 'red');
        displayNotification(msg, 2);
    }

    setSuccessfulCheckStyle = (row, field, configInfo) => {
        row.removeClass('warning');
        field.css('background', '').css('color', '');
        configInfo.text('').css('color', 'green');
    }

    $(document).ready(() => init_chosen());
</script>
