{include file="./index_top.tpl"}
<div align=center><br>{$lang.Update_configuration}<span class='help_bt' h_id='Update_configuration'></span><br><br></div>
{if $restore_msg}
    <div style="text-align: center">{$lang.Restore_backup} &quot;{$backup_filename}&quot;<br/><br/></div>
    <div id="backup_status" style="font-size: 13px; text-align: center">
        {if $restore_msg}
            <img src='images/indicator.gif' alt='' style='vertical-align: middle'/>
            {$restore_msg}...
            <br/>
            <br/>
        {/if}
    </div>
    <div id="backup_process" style="width: 200px; text-align: left; border: 1px solid black; margin: 3px auto 40px">
        <div id="backup_bar" style="height: 18px; width: {$part_done}%; background: #85C51F"></div>
        <div id="backup_progress"
             style="position: absolute; margin-top: -18px; text-align: center; width: 200px; font-size: 13px;">{$part_done}
            %
        </div>
    </div>
    <script type="text/javascript">
        var backupError = true;
        $(document).ready(function () {
            if (backupError) {
                document.getElementById("backup_process").style.display = "none";
                document.getElementById("backup_status").style.display = "none";
            }
        });
    </script>
{else}
    <script language="JavaScript" type="text/javascript">
        // values for system help vars
        var help_section = 'Settings';
        var help_sub_section = 'Additional';

        function display_rows(blok, on) {
            document.getElementById(blok).style.display = on ? '' : 'none';
            document.getElementById(blok + '_off').style.display = on ? '' : 'none';
            document.getElementById(blok + '_on').style.display = on ? 'none' : '';
        }

        function display_additional() {
            if (document.getElementById('additional_tables').style.display == 'none') {
                document.getElementById('additional_tables').style.display = '';
                jalert("{$lang.upd_addit_tables_alert}");
            }
            else
                document.getElementById('additional_tables').style.display = 'none';
        }
    </script>
    {if $create_changes}
        <script language="JavaScript" type="text/javascript">
            function changeAddTableSettings(mode, el){
                if(mode){
                    $(`[class^='addt_main_changes_']`).prop('checked', el.checked);
                    $(`[id^='addt_changes_']`).prop('checked', el.checked);
                }
                else {
                    $(`[class^='find_main_changes_']`).prop('checked', el.checked);
                    $(`[id^='main_changes_']`).prop('checked', el.checked);
                }
            }
            function checkState(mode, el){
                if(mode){
                    if(el.checked != true && $('[name=all_add_table_changes]').prop('checked') == true){
                        $('[name=all_add_table_changes]').prop('checked', false);
                    }
                }
                else {
                    if(el.checked != true && $('[name=all_find_changes]').prop('checked') == true){
                        $('[name=all_find_changes]').prop('checked', false);
                    }
                }
            }
            $("body").on('submit', '#select_update_form', function(e) {
                e.preventDefault();
                $('[name="select_data_json"]').val(JSON.stringify($("#select_update_form").serializeArray()));
                $("#select_update_form_for_send").submit();
            });
        </script>
        <div align=center style="font-size:15px; padding-bottom:5px;"><b>{$lang.Found_changes}:</b><br></div>
        <table align=center>
            <tr>
                <td nowrap>
                    <form action="edit_update.php" method="post" id="select_update_form">
                        <input type="checkbox" name="all_find_changes" onchange="changeAddTableSettings(false, this)" value=1 checked>
                        Все изменения
                        <br><br>
                        {foreach from=$create_changes key=type item=array_changes}
                            {if $type=="additional"}
                                <div style="margin-top:5px"><a href=""
                                                               onclick="display_additional(); return false;">{$lang.Additional_tables}</a>
                                </div>{/if}
                            <div id="{$type}_tables" {if $type=="additional"}style="display:none; margin-top:5px"{/if}>
                                {if $type=="additional"}
                                    <input type="checkbox" name="all_add_table_changes" onchange="changeAddTableSettings(true, this)">
                                    Все изменения в доп. таблицах
                                    <br><br>
                                {/if}
                                {foreach from=$array_changes key=table item=table_changes}
                                    {foreach from=$table_changes key=action item=changes}
                                        <input  type="checkbox" 
                                            {if $type=="additional"}
                                                class='addt_main_changes_{$table}'
                                            {else}
                                                class='find_main_changes_{$table}'
                                            {/if}
                                               onchange="$('.changes_{$table}_{$action}').prop('checked', this.checked);"
                                               {if $type=="main"}checked{/if}>
                                        {$lang.In_the_table}
                                        <b>{$table}</b>
                                        {$act_desc[$action]}
                                        <b>{$changes|count_cb}</b>
                                        {$lang.records}.
                                        <a href="" id="{$table}_{$action}_on"
                                           onclick="display_rows('{$table}_{$action}', 1); return false;">>><br></a>
                                        <a href="" id="{$table}_{$action}_off"
                                           onclick="display_rows('{$table}_{$action}', 0); return false;"
                                           style="display:none"><<<br></a>
                                        <div id="{$table}_{$action}" style="display:none">
                                            {foreach from=$changes key=num item=change}
                                                <input type="checkbox" name="changes[{$change.num}]"
                                                       {if $type=="additional"}
                                                            id="addt_changes_{$table}_{$action}"
                                                            onclick="checkState(true, this)"
                                                        {else}
                                                            id="main_changes_{$table}_{$action}"
                                                            onclick="checkState(false, this)"
                                                       {/if}
                                                       class="changes_{$table}_{$action}" value=1
                                                       {if $type=="main"}checked{/if}>
                                                {$num+1}:
                                                <br>
                                                {foreach from=$change.fields key=field item=value}
                                                    {if $change.new_fields[$field]!==null}
                                                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                                        <span style="color:green">{$field}
                                                            : <b>{$lang.upd_from}</b> "{$change.fields[$field]|htmlspecialchars|truncate:50}
                                                            " <b>{$lang.upd_to}</b> "{$change.new_fields[$field]|htmlspecialchars|truncate:50}
                                                            "</span>
                                                        <br>
                                                    {else}
                                                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{$field}: "{$value|htmlspecialchars|truncate:50}"
                                                        <br>
                                                    {/if}
                                                {/foreach}
                                            {/foreach}
                                            <br>
                                        </div>
                                    {/foreach}
                                {/foreach}
                            </div>
                            {if $ext_files_exists && $type!=="additional"}
                                <input type="checkbox" name="ext_files_export" value=1 checked>
                                {$lang.Ext_files}{/if}
                        {/foreach}
                        <input type=hidden name="select_update">
                        <input type=hidden name=csrf value='{$csrf}'>
                        <div align=center><br>
                            <input type="submit" value="{$lang.Create}" name="agreement">
                            <input type="button" value="{$lang.Cancel}" onclick="location.href='edit_update.php'">
                        </div>
                    </form>
                    <form action="edit_update.php" method="post" id="select_update_form_for_send" style="display: none;">
                        <textarea name="select_data_json"></textarea>
                        <input type=hidden name="select_update">
                        <input type=hidden name=csrf value='{$csrf}'>
                        <div align=center><br>
                            <input type="submit" value="{$lang.Create}" name="agreement">
                            <input type="button" value="{$lang.Cancel}" onclick="location.href='edit_update.php'">
                        </div>
                    </form>
                </td>
            </tr>
        </table>
    {elseif $upload_changes}
        <div align=center style="font-size:15px; padding-bottom:5px;"><b>{$lang.Found_changes}:</b><br></div>
        <table align=center>
            <tr>
                <td nowrap>
                    {foreach from=$upload_changes key=table item=table_changes}
                        {foreach from=$table_changes key=action item=changes}
                            {if $action == "insert"}
                                {$lang.In_the_table}
                                <b>{$table}</b>
                                {$lang.upd_added}
                                <b>{$changes|count_cb}</b>
                                {$lang.records}.
                                <a href="" id="{$table}_{$action}_on"
                                   onclick="display_rows('{$table}_{$action}', 1); return false;">>><br></a>
                                <a href="" id="{$table}_{$action}_off"
                                   onclick="display_rows('{$table}_{$action}', 0); return false;" style="display:none"><<<br></a>
                                <div id="{$table}_{$action}" style="display:none">
                                    {foreach from=$changes key=num item=change}
                                        {$num+1}:
                                        <br>
                                        {foreach from=$change.fields key=field item=value}
                                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{$field}: "{$change.fields[$field]|htmlspecialchars|truncate:50}"
                                            <br>
                                        {/foreach}
                                    {/foreach}
                                    <br>
                                </div>
                            {/if}
                            {if $action == "delete"}
                                {$lang.In_the_table}
                                <b>{$table}</b>
                                {$lang.upd_deleted}
                                <b>{$changes|count_cb}</b>
                                {$lang.records}.
                                {if $confilcts.delete[$table]}
                                    <br>
                                    <span style="color:darkorange">{$lang.Found}
                                    <b>{$confilcts.delete[$table]}</b>
                                    {$lang.possible_conflicts}.</span>{/if}
                                <a href="" id="{$table}_{$action}_on"
                                   onclick="display_rows('{$table}_{$action}', 1); return false;">>><br></a>
                                <a href="" id="{$table}_{$action}_off"
                                   onclick="display_rows('{$table}_{$action}', 0); return false;" style="display:none"><<<br></a>
                                <div id="{$table}_{$action}" style="display:none">
                                    {foreach from=$changes key=num item=change}
                                        {$num+1}:
                                        <br>
                                        {if $change.id}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;id: "{$change.id}"<br>{/if}
                                        {foreach from=$change.old_fields key=field item=value}
                                            {if $change.id and $change.cur_fields[$field]!=$change.old_fields[$field]}
                                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                                <span style="color:darkorange">{$field}
                                                    : <b>{$lang.upd_from}</b> "{$change.old_fields[$field]|htmlspecialchars|truncate:50}
                                                    " <b>{$lang.upd_to}</b> "{$change.cur_fields[$field]|htmlspecialchars|truncate:50}
                                                    "</span>
                                                <br>
                                            {else}
                                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{$field}: "{$change.old_fields[$field]|htmlspecialchars|truncate:50}"
                                                <br>
                                            {/if}
                                        {/foreach}
                                    {/foreach}
                                    <br>
                                </div>
                            {/if}
                            {if $action == "update"}
                                {$lang.In_the_table}
                                <b>{$table}</b>
                                {$lang.upd_changed}
                                <b>{$changes|count_cb}</b>
                                {$lang.records}.
                                {if $confilcts.update[$table]}
                                    <br>
                                    <span style="color:darkorange">{$lang.Found}
                                    <b>{$confilcts.update[$table]}</b>
                                    {$lang.possible_conflicts}.</span>{/if}
                                <a href="" id="{$table}_{$action}_on"
                                   onclick="display_rows('{$table}_{$action}', 1); return false;">>><br></a>
                                <a href="" id="{$table}_{$action}_off"
                                   onclick="display_rows('{$table}_{$action}', 0); return false;" style="display:none"><<<br></a>
                                <div id="{$table}_{$action}" style="display:none">
                                    {foreach from=$changes key=num item=change}
                                        {$num+1}:
                                        <br>
                                        {if !$change.cur_fields}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                            <span style="color:red">{$lang.Record_has_deleted}</span>
                                            <br>
                                        {/if}
                                        {if $change.id}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{if !$change.cur_fields}<span style="color:darkorange">{/if}
                                            id: "{$change.id}"{if !$change.cur_fields}</span>{/if}
                                            <br>
                                        {/if}
                                        {foreach from=$change.old_fields key=field item=value}
                                            {if $change.new_fields[$field]!=$change.old_fields[$field]}
                                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                                <span style="color:green">{$field}
                                                    : <b>{$lang.upd_from}</b> "{$change.old_fields[$field]|htmlspecialchars|truncate:50}
                                                    " <b>{$lang.upd_to}</b> "{$change.new_fields[$field]|htmlspecialchars|truncate:50}
                                                    "</span>
                                                <br>
                                            {elseif !$change.cur_fields}
                                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                                <span style="color:darkorange">{$field}
                                                    : "{$change.old_fields[$field]|htmlspecialchars|truncate:50}"</span>
                                                <br>
                                            {elseif $change.cur_fields[$field]!=$change.old_fields[$field] and $change.cur_fields[$field]!=$change.new_fields[$field]}
                                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                                <span style="color:darkorange">{$field}
                                                    : <b>{$lang.upd_from}</b> "{$change.old_fields[$field]|htmlspecialchars|truncate:50}
                                                    " <b>{$lang.upd_to}</b> "{$change.cur_fields[$field]|htmlspecialchars|truncate:50}
                                                    "</span>
                                                <br>
                                            {else}
                                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{$field}: "{$change.old_fields[$field]|htmlspecialchars|truncate:50}"
                                                <br>
                                            {/if}
                                        {/foreach}
                                    {/foreach}
                                    <br>
                                </div>
                            {/if}
                        {/foreach}
                    {/foreach}
                    {if $diff_ext_files}
                        {$lang.Ext_files}
                        <a href=''
                           onclick="if (document.getElementById('ext_files_list').style.display=='block') document.getElementById('ext_files_list').style.display='none'; else document.getElementById('ext_files_list').style.display='block';return false;">>></a>
                        <br>
                        <div style='display:none' id=ext_files_list>
                            {foreach $diff_ext_files as $ext_file=>$ext_id}
                                {$ext_file}
                                <br>
                            {/foreach}
                        </div>
                    {/if}
                    <br>
                    <form action="edit_update.php" method="post">
                        <input type=hidden name="apply_update">
                        <input type=hidden name=csrf value='{$csrf}'>
                        <div align=center><br>
                            <input type="submit" value="{$lang.Apply}" name="agreement">
                            <input type="button" value="{$lang.Cancel}" onclick="location.href='edit_update.php'">
                        </div>
                    </form>
                </td>
            </tr>
        </table>
    {else}
        <script language="JavaScript" type="text/javascript">
            function message(type) {
                if (type == 'create') {
                    if (!document.getElementById('load_file').value) {
                        message_show('create', 'red', 'normal', '{$lang.no_file_selected}');
                        return false;
                    }
                    else {
                        message_show('create', 'orange', 'italic', '{$lang.Uploading_backup}...');
                        return true;
                    }
                }
                else if (type == 'upload') {
                    if (!document.getElementById('upload_file').value) {
                        message_show('upload', 'red', 'normal', '{$lang.no_file_selected}');
                        return false;
                    }
                    else {
                        message_show('upload', 'orange', 'italic', '{$lang.Uploading_update}...');
                        return true;
                    }
                }
            }

            function message_show(type, mess_color, mess_style, mess_text) {
                document.getElementById('create_message').style.display = (type == 'create') ? 'inline' : 'none';
                document.getElementById('upload_message').style.display = (type == 'upload') ? 'inline' : 'none';
                document.getElementById(type + '_message').style.color = mess_color;
                document.getElementById(type + '_message').style.fontStyle = mess_style;
                document.getElementById(type + '_message').innerHTML = mess_text + '<br><br>';
            }
        </script>
        <div {if $config.dev_channel==0}style="display:none"{/if}>
            <form action="edit_update.php" method="post" enctype="multipart/form-data">
                <input type=hidden name="create_update">
                <input type=hidden name=csrf value='{$csrf}'>
                <div align=center style="padding:3px">
                    <div style="font-size:14px;">{$lang.Create_update_file}<span class='help_bt'
                                                                                 h_id='Create_update_file'></span><br><br>
                    </div>
                    <div style="display:{if $create_message}inline{else}none{/if}; color:{$create_message_color};"
                         id="create_message">{$create_message}<br><br></div>
                    {$lang.Original_configuration}<span class='help_bt' h_id='Original_configuration'></span>:
                    <select class="form-control form-control-160" name="load_file" id="load_file" {if !$sel_file}disabled{/if}>
                        {if $sel_file}{$sel_file}{else}
                            <option value=''>{$lang.no_arhives}</option>{/if}
                    </select><br><br>
                    {$lang.File_name}: <input class="form-control form-control-160" type=text name="file_name" id="file_name" size=17
                                              value="{$config.file_name}">.upd.zip<br><br>
                    <div style="padding:3px"><input class="btn btn-default btn-sm" type="submit" value="{$lang.Generate}"
                                                    onclick="if (!message('create')) return false;"></div>
                </div>
            </form>
            <hr style="width:300px">
        </div>
        <form action="edit_update.php" method="post" enctype="multipart/form-data">
            <input type=hidden name="upload_update">
            <input type=hidden name=csrf value='{$csrf}'>
            <div align=center style="padding:3px">
                <div style="font-size:14px;">{$lang.Upload_update_file}<span class='help_bt'
                                                                             h_id='Upload_update_file'></span><br><br>
                </div>
                <div style="display:{if $upload_message}inline{else}none{/if}; color:{$upload_message_color};"
                     id="upload_message">{$upload_message}<br><br></div>
                <table align="center" border=0 cellpadding=3 style="margin:-5px 0 12px">
                    <tr>
                        <td align=right>{$lang.Upload_file}:</td>
                        <td><input type=file name="upload_file" id="upload_file" size=25 class="jfilestyle" data-input="false"></td>
                    </tr>
                    <tr>
                        <td align=center colspan=2 style="color:gray;font-style:italic;">{$lang.max_filesize}
                            : {$config.maxfilesize}B
                        </td>
                    </tr>
                </table>
                <div style="padding:3px"><input class="btn btn-default btn-sm" type="submit" value="{$lang.Upload}"
                                                onclick="if (!message('upload')) return false;"></div>
            </div>
        </form>
        <hr style="width:200px">
        <form action="edit_update.php" method="post" enctype="multipart/form-data">
            <input type=hidden name="save_channel">
            <input type=hidden name=csrf value='{$csrf}'>
            <div align=center style="padding:3px">
                <div style="font-size:14px;">{$lang.Development_channel}<span class='help_bt'
                                                                              h_id='Development_channel'></span><br><br>
                </div>
                <select class="form-control form-control-160" name="dev_channel"
                        onchange="if (this.value==9) jconfirm('{$lang.reserve_channel_alert}', function () {}, function () { this.value='{$config.dev_channel}'; });">
                    <option value=0 {if $config.dev_channel==0}selected{/if}>{$lang.For_user}</option>
                    <option value=1 {if $config.dev_channel==1}selected{/if}>{$lang.Development} 1</option>
                    <option value=2 {if $config.dev_channel==2}selected{/if}>{$lang.Development} 2</option>
                    <option value=3 {if $config.dev_channel==3}selected{/if}>{$lang.Development} 3</option>
                    <option value=4 {if $config.dev_channel==4}selected{/if}>{$lang.Development} 4</option>
                    <option value=5 {if $config.dev_channel==5}selected{/if}>{$lang.Development} 5</option>
                    <option value=6 {if $config.dev_channel==6}selected{/if}>{$lang.Development} 6</option>
                    <option value=7 {if $config.dev_channel==7}selected{/if}>{$lang.Development} 7</option>
                    <option value=8 {if $config.dev_channel==8}selected{/if}>{$lang.Development} 8</option>
                    <option value=9 {if $config.dev_channel==9}selected{/if}>{$lang.Reserve}</option>
                </select><br><br>
                <div style="padding:3px"><input class="btn btn-default btn-sm" type="submit" value="{$lang.Save}"></div>
            </div>
        </form>
    {/if}
    <br>
    <div align=center style="padding:3px"><a href="edit_add.php">{$lang.return_to_add_settings}</a></div>
    <br>
{/if}