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

    var demo_mode = {if $demo_base_id || $client_demo_id}1{else}0{/if};
    {if $list_files}var files = [{$list_files}];{/if}
    function message(type) {
        if (demo_mode) {
            jalert('{$lang.oper_prohib_trial_alert}');
            return false;
        }
        if (type == 'copy') {
            if (!document.getElementById('file_name').value) {
                message_show('copy', 'red', 'normal', '{$config.No_filename}');
                return false;
            }
            else {
                if (document.getElementById('int').checked)
                    for (i = 0; i < files.length; i++) if (files[i] == (document.getElementById('file_name').value + '.' + document.getElementById('file_type').value)) var file_exist = true;
                if (file_exist && !confirm('{$lang.File_overwrite_confirm}')) {
                    document.getElementById('copy_message').style.display = 'none';
                    return false;
                }
                message_show('copy', 'orange', 'italic', lang.checking_backup_start + "...");
                return true;
            }
        }
        else if (type == 'restore') {
            if (document.getElementById('source_restore2').checked && !document.getElementById('load_file2').value) {
                message_show('restore', 'red', 'normal', '{$lang.no_file_selected}');
                return false;
            }
            else if (document.getElementById('source_restore3').checked && !document.getElementById('backup_site').value) {
                message_show('restore', 'red', 'normal', '{$lang.no_site_selected}');
                return false;
            }
            else {
                message_show('restore', 'orange', 'italic', lang.checking_backup_start + "...");
                return true;
            }
        }
        else if (type == 'schedule') {
            message_show('schedule', 'orange', 'italic', '{$lang.Saving}...');
            return true;
        }
    }

    function message_show(type, mess_color, mess_style, mess_text) {
        document.getElementById('copy_message').style.display = (type == 'copy') ? 'inline' : 'none';
        document.getElementById('restore_message').style.display = (type == 'restore') ? 'inline' : 'none';
        document.getElementById('schedule_message').style.display = (type == 'schedule') ? '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>';
    }

    function checkBackupStatus(type) {
        $.ajax({
            type: "POST",
            url: "backup.php",
            data: { check_backup_status: 1, csrf: '{$csrf}' },
            success: function (msg) {
                document.getElementById("copy_message").innerHTML = '';
                document.getElementById("restore_message").innerHTML = '';
                if (msg == "1")
                    jconfirm(lang.backup_create_confirm, function () {
                        submitBackupForm(type);
                    }, function () {
                    });
                else if (msg == "2")
                    jconfirm(lang.backup_restore_confirm, function () {
                        submitBackupForm(type);
                    }, function () {
                    });
                else
                    submitBackupForm(type);
            }
        });
    }

    function submitBackupForm(type) {
        if (type == 'create') {
            document.getElementById("copy_message").innerHTML = lang.auto_backup_started + "...<br /><br />";
            document.getElementById("create_backup_form").submit();
        }
        if (type == 'restore') {
            document.getElementById("restore_message").innerHTML = lang.Restoring + "...<br /><br />";
            jconfirm('{if $config.no_auto_save_backup}{$lang.confirm_restore1}{else}{$lang.confirm_restore2}{/if}', function () {
                document.getElementById("restore_backup_form").submit();
            }, function () {
                document.getElementById("restore_message").innerHTML = '';
            });
        }
    }

</script>
<style>
    #load_file {
        width: auto;
        min-width: 160px;
    }
    #archive_file {
        width: auto;
        min-width: 160px;
    }
</style>

{if $create_msg or $restore_msg or $snoopy_error}
    <div style="text-align: center">{if $create_msg}{$lang.Create_backup} &quot;{$backup_filename}&quot;{elseif $restore_msg} {$lang.Restore_backup}&quot;{$backup_filename}&quot;{else}{/if}
        <br/><br/></div>
    <div id="backup_status" style="font-size: 13px; text-align: center">
        {if $create_msg}
            <img src='images/indicator.gif' alt='' style='vertical-align: middle'/>
            {$create_msg}...
            <br/>
            <br/>
        {/if}
        {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}
    <div align=center><br>{$lang.backup_title}<span class='help_bt' h_id='Backup_title'></span><br><br></div>
    <form action="backup.php" method="post" onsubmit="checkBackupStatus('create'); return false"
          id="create_backup_form">
        <input type=hidden name=csrf value="{$csrf}">
        <div align=center style="padding:3px">
            <div style="font-size:14px;">{$lang.Create_backup}<br><br></div>
            <div style="display:{if $copy_message}inline{else}none{/if}; color:{$copy_message_color};" id="copy_message">{$copy_message}<br><br></div>
            <div class="flex-wrap flex-wrap--center">
                <div class="flex-wrap">
            <input type=radio name="target_copy" value=1 onclick="if ({$storage_status}) { 
                                    document.getElementById('save_all_backup').style.display = 'block';
                                    document.getElementById('all_backup').checked = true;
                                }"checked id="int">&nbsp;{if $storage_status}{$lang.storage_backup}{else}{$lang.internal_backup}{/if}
                    <span class='help_bt' h_id='internal_{if $storage_status}storage_{/if}backup'></span>
                </div>&nbsp;&nbsp;
                {if $user.group_id == 1}
                <div class="flex-wrap">
                    <input type=radio name="target_copy" value=2
                            {if !$full_version || $tariff_id == 1}
                                onclick="console.log('this');jalert('{$lang.oper_prohib_trial_alert}'); document.getElementById('int').checked=1; {$storage_status} ? document.getElementById('save_all_backup').style = 'display: none;':''"
                            {else}
                                onclick="if ({$storage_status}) { 
                                    document.getElementById('save_all_backup').style.display = 'none';
                                    document.getElementById('data_backup').checked = true;
                                }"
                            {/if}
                    >&nbsp;{$lang.external_backup}
                    <span class='help_bt' h_id='external_backup'></span>
                </div>
                {/if}
            </div>
            <br>
            <div class="flex-wrap flex-wrap--center">
                <div class="flex-wrap" id="save_all_backup" style = "display: flex">
                    <input id='all_backup' type=radio name="backup_type" value='0' checked="checked">&nbsp;{$lang.save_all_backup}
                    <span class='help_bt' h_id='save_all_backup'></span>&nbsp;
                </div>
                <div class="flex-wrap">
                    <input id='data_backup' type=radio name="backup_type" value='1'>&nbsp;{if $storage_status}{$lang.save_data_olny_storage}{else}{$lang.save_data_olny}{/if}
                    <span class='help_bt' h_id='save_data_olny{if $storage_status}_storage{/if}'></span>&nbsp;
                </div>
                <div class="flex-wrap">
                    <input type=radio name="backup_type" value='2'>&nbsp;{if $storage_status}{$lang.save_structure_storage}{else}{$lang.save_structure}{/if}
                    <span class='help_bt' h_id='save_structure{if $storage_status}_storage{/if}'></span><br><br>
                </div>
            </div>
            <br>
            {$lang.File_name}<span class='help_bt' h_id='File_name'></span>:
            <input class="form-control form-control-160" type=text name="file_name" id="file_name" value="{$config.file_name}"> .
            <select class="form-control form-control-35" name="file_type" id="file_type">
                <option value="zip" {if $config.file_type == 'zip'}selected{/if}>zip</option>
                <option value="tar.gz" {if $config.file_type == 'tar.gz'}selected{/if}>tar.gz</option>
            </select><br><br>
            <input type="hidden" name="archive" value="1"/>
            <div style="padding:3px"><input class="btn btn-default btn-sm" type="submit" name="archive" value="{$lang.Create}"
                                            onclick="if (!message('copy')) return false;"></div>
        </div>
    </form>
    <hr style="width:200px">
    <form action="backup.php" method="post" enctype="multipart/form-data"
          onsubmit="checkBackupStatus('restore'); return false" id="restore_backup_form">
        <input type=hidden name=csrf value="{$csrf}">
        <div align=center style="padding:3px">
            <div style="font-size:14px;">{$lang.Restore_backup}<br><br></div>
            <div style="display:{if $restore_message}inline{else}none{/if}; color:{$restore_message_color};"
                 id="restore_message">{$restore_message}<br><br></div>
            <table align="center" border=0 cellpadding=3 style="margin:-5px 0 12px">
                <tr>
                    <td align=right>
                        <div class="flex-wrap flex-wrap--flex-end">
                            <input type=radio name="source_restore" id="source_restore1" value=1
                                {if $sel_file}
                                    checked
                                {else}
                                    disabled
                                {/if}
                            >&nbsp;{if $storage_status}{$lang.storage_restore_from_external}{else}{$lang.restore_from_internal}{/if}
                            <span class='help_bt' h_id='restore_from_internal{if $storage_status}_storage{/if}'></span>:
                        </div>
                    </td>
                    <td>
                        <table cellspacing=0 cellpadding=0>
                            <tr>
                                <td><select class="form-control form-control-160" name="load_file" id="load_file"
                                            onchange="document.getElementById('source_restore1').checked=1"
                                            {if !$sel_file}disabled{/if}>{if $sel_file}{$sel_file}{else}
                                            <option value=''>{$lang.no_arhives}</option>{/if}</select>&nbsp;</td>
                            </tr>
                        </table>
                    </td>
                </tr>
                {if $user.group_id==1}
                    <tr>
                        <td align=right>
                            <div class="flex-wrap flex-wrap--flex-end">
                                <input type=radio name="source_restore" id="source_restore2" value=2
                                    {if !$sel_file}
                                        checked
                                    {/if}
                                    {if !$full_version || $tariff_id == 1}
                                        onclick="jalert('{$lang.oper_prohib_trial_alert}'); document.getElementById('source_restore1').checked=1;"
                                    {/if}
                                >&nbsp;{$lang.restore_from_external}
                                <span class='help_bt' h_id='restore_from_external'></span>:
                            </div>
                        </td>
                        <td><input type=file name="load_file" id="load_file2" size=25 class="jfilestyle" data-input="false"
                                   onchange="{if !$full_version || $tariff_id == 1}jalert('{$lang.oper_prohib_trial_alert}'); this.value='';{else}document.getElementById('source_restore2').checked=1{/if}">
                        </td>
                    </tr>
                    <tr>
                        <td></td>
                        <td align=center style="color:gray;font-style:italic;">{$lang.max_filesize}: {$config.maxfilesize}B</td>
                    </tr>
                    <!--tr>
                        <td align=right>
                            <div class="flex-wrap flex-wrap--flex-end">
                                <input type=radio name="source_restore" id="source_restore3" value=3 onclick="
                                    {if !$full_version || $tariff_id == 1}
                                        jalert('{$lang.oper_prohib_trial_alert}'); document.getElementById('source_restore1').checked=1;
                                    {else}
                                        document.getElementById('site_connect').style.display='';
                                    {/if}"
                                >&nbsp;{$lang.restore_from_url}
                                <span class='help_bt' h_id='restore_from_url'></span>:
                            </div>
                        </td>
                        <td>
                          <input
                            class="form-control form-control-160"
                            type=text
                            name="backup_site"
                            id="backup_site"
                            size=30
                            value="{$config.backup_site}"

                            {if !$full_version || ($full_version && $tariff_id != 1)}
                              onclick="document.getElementById('source_restore3').checked=1; document.getElementById('site_connect').style.display='';"
                            {else}
                              onchange="jalert('{$lang.oper_prohib_trial_alert}'); this.value='';"
                            {/if}
                          >
                        </td>
                    </tr>
                    <tbody id="site_connect" style="display:none">
                    <tr>
                        <td align=right width=39%>{$lang.login}:</td>
                        <td><input class="form-control form-control-160" type=text name="backup_login" size=20 value="{$config.backup_login}"></td>
                    </tr>
                    <tr>
                        <td align=right width=39%>{$lang.password}:</td>
                        <td><input class="form-control form-control-160" type=password name="backup_password" size=20></td>
                    </tr>
                    </tbody-->
                    <tr>
                        <td align=center colspan=2 style="padding-top: 10px">
                            <input type=checkbox name="disable_crons" value="1">&nbsp;{$lang.disable_crons}</span>
                            <span class='help_bt' h_id='disable_crons'></span>
                        </td>
                    </tr>
                {/if}
            </table>
            <input type="hidden" name="restore" value="1"/>
            <div style="padding:3px"><input class="btn btn-default btn-sm" type="submit" id="backup_form_restore_btn" value="{$lang.Restore}"
                                            onclick="if (!message('restore')) return false;"></div>
        </div>
    </form>
    <hr style="width:300px">
    <form action="backup.php" method="post">
        <input type=hidden name=csrf value="{$csrf}">
        <div align=center style="padding:3px">
            <div style="font-size:14px;">{$lang.Schedule}<br><br></div>
            {if $config.type == 'SAAS' && !$tariff_maximum && !$demo_base_id && !$client_demo_id}
                {$lang.saas_bcp_schedule_msg} <a href="edit_saas.php">{$lang.Saas_cabinet}</a>
            {else}
            <div style="display:{if $schedule_message}inline{else}none{/if}; color:{$schedule_message_color};"
                 id="schedule_message">{$schedule_message}<br><br></div>
            <div class="flex-wrap flex-wrap--center">
                <span class="flex-wrap">
                    <input type=radio name="schedule_type" value="0" {if $config.schedule_type == 0}checked{/if}
                           onclick="$('#simple_schedule').show(); $('#extended_schedule').hide();">&nbsp;{$lang.simple_s}</span>&nbsp;&nbsp;&nbsp;
                <span class="flex-wrap">
                    <input type=radio name="schedule_type" value="1" {if $config.schedule_type == 1}checked{/if}
                           onclick="$('#simple_schedule').hide(); $('#extended_schedule').show();">&nbsp;{$lang.extended_s}</span>
            </div>
            <br>
            <div id="simple_schedule" {if $config.schedule_type != 0}style="display:none"{/if}>
                {$lang.create_auto_backup_period}
                <input class="form-control form-control-25" type=text name="rate_copy[0]" value="{if $rate_copy}{$rate_copy}{/if}"> {$lang.days}
                <span class='help_bt'h_id='create_auto_backup_period'></span><br><br>
                {$lang.delete_backup_older}
                <input class="form-control form-control-25" type=text name="del_copy[0]" value="{if $config.del_copy}{$config.del_copy}{/if}"> {$lang.days}
                <span class='help_bt' h_id='delete_backup_older'></span>
            </div>
            <div id="extended_schedule" {if $config.schedule_type != 1}style="display:none"{/if}>
                {$lang.create_auto_backup_period}<br>
                <input class="form-control form-control-25" type=hidden name="rate_copy[1]" value="1">
                1 {$lang.day_during}
                <input class="form-control form-control-25" type=text name="del_copy[1]" value="{if $config.del_copy}{$config.del_copy}{else}3{/if}"> {$lang.days}
                <span class='help_bt' h_id='delete_backup_older'></span><br>
                1 {$lang.week_backup}<br>
                1 {$lang.month_backup}<br>
                1 {$lang.quarter_backup}<br>
                1 {$lang.year_backup}<br>
            </div>
            <br>
            <div class="flex-wrap flex-wrap--center">
                <span class="flex-wrap" title='{$lang.save_all_backup_help}'>
                    <input type=radio name="auto_backup_type" value="0" {if $auto_backup_type == 0}checked{/if}>&nbsp;{$lang.save_all_backup}</span>&nbsp;&nbsp;&nbsp;
                <span class="flex-wrap" title='{$lang.save_data_olny_help}'>
                    <input type=radio name="auto_backup_type" value="1" {if $auto_backup_type == 1}checked{/if}>&nbsp;{$lang.save_data_olny}</span>
            </div>
            <br>
            <div style="padding:3px">
                <input class="btn btn-default btn-sm" type="submit" name="save_schedule" value="{$lang.save_schedule}" onclick="if (!message('schedule')) return false;">
            </div>
            {/if}
        </div>
    </form>
    <hr style="width:300px">
    <form action="backup.php" method="post">
        <input type=hidden name=csrf value="{$csrf}">
        <div align=center style="padding:3px;padding-bottom:70px;">
            <div style="font-size:14px;">{$lang.backup_archive}<br><br></div>
            <div style="display:none; color:;" id="archive_message"><br><br></div>
                <select class="form-control form-control-160" name="archive_file" id="archive_file"
                    {if !$archive_file}disabled{/if}>{if $archive_file}{$archive_file}{else}
                    <option value=''>{$lang.no_arhives}</option>{/if}</select><br><br>
            <div style="padding:3px">
                {if $full_version && $tariff_id != 1}
                    <input class="btn btn-default btn-sm" type="submit" name="download_archive" onclick="location.href='backup.php?archive_download=&download='+document.getElementById('archive_file').value; return false;" value="{$lang.download_backup_archive}">
                {/if}
                <input class="btn btn-default btn-sm" type="submit" name="delete_archive" value="{$lang.delete_backup_archive}">
            </div>
        </div>
    </form>
    {* if $full_version}
    <div id="vnesh_form" style="display: none">
    <hr style="width:300px">
    <form action="backup.php" method="post">
      <input type=hidden name=csrf value="{$csrf}">
      <div align=center style="padding:3px">

        <div style="font-size:14px;">{$lang.External_storage}</div>

        {if !$ext_storage}
        <br>
        <div id="ext_storage_create">
          <input type="button"  value="{$lang.Create}" onclick="create_ext_storage()">
        </div>
        {/if}
        {if $ext_storage}
            <br>
            {$lang.Connected}:
            <br>
            <table>
              <tr>
                <td><span style="color:blue;text-decoration: underline;" onclick="edit_ext_storage('dropbox')">Dropbox</span></td>
                <td><a class="href_post" href="backup.php?delete_ext_storage=dropbox"><img alt="{$lang.Delete}" title="{$lang.Delete} DropBox" src="images/b_drop.png" border=0></a></td>
                  {if isset($ext_storage.dropbox_send_status)}
                  {if $ext_storage.dropbox_send_status ==1}
                    <td><font color="green">{$lang.Backup_send}</font></td>
                  {else}
                    <td><font color="red">{$lang.Backup_not_send}</font></td>
                  {/if}
                {/if}
              </tr>
            </table>
        <br>
        {/if}

        <div id="ext_storage_info" style="display:none">
          <table>
          <thead id="ext_storage_thead" style="display:none">
            <tr>
              <td> {$lang.External_storage}<span class='help_bt' h_id='ext_storage_list'></span>: </td>
              <td>
                <select name="ext_storage_name" id="ext_storage_name" onchange="switch_ext_storage('dropbox')">
                  <option></option>
                  <option value = "dropbox">Dropbox</option>
                </select>
              </td>
            </tr>
           </thead>
           <tbody id="dropbox_info" style="display:none">
            <tr>
              <td>{$lang.Your_login} <span class='help_bt' h_id='ext_storage_dropbox_name'></span>:</td>
              <td><input type=text name="dropbox_name" id="dropbox_name" {if $ext_storage.dropbox_name}value="{$ext_storage.dropbox_name}"{else}value=""{/if} size=25></td>
            </tr>
            <tr>
              <td> {$lang.Your_password} <span class='help_bt' h_id='ext_storage_dropbox_pswd'></span>:</td>
              <td><input type=password name="dropbox_pswd" id="dropbox_pswd" {if $ext_storage.dropbox_pswd}value="{$ext_storage.dropbox_pswd}"{else}value=""{/if} size=25></td>
            </tr>
            <tr>
              <td>{$lang.Path} <span class='help_bt' h_id='ext_storage_dropbox_path'></span>:</td>
              <td><input type=text name="dropbox_path" id="dropbox_path" {if $ext_storage.dropbox_path}value="{$ext_storage.dropbox_path}"{else}value=""{/if} size=25></td>
            </tr>
            </tbody>

        </table>
        <div style="padding:3px"><input type="submit" name="save_dropbox" value="{$lang.Save}" onclick="return verify_data('dropbox');"></div>
        <br>
        </div>
        {if $ext_storage}
            <input type=button onclick="doPopup('backup.php?ext_storage_manual_run=1');return false" value="{$lang.Upload_now}">
        {/if}
      </div>

    </form>
    </div>
    {/if *}
    <br>
{/if}
<script>
    if (location.protocol.indexOf("https:") >= 0)
        $("#vnesh_form").css('display', 'block');

    var drop_name = {if $ext_storage.dropbox_name}'{$ext_storage.dropbox_name}'
    {else}''{/if};
    var drop_pswd = {if $ext_storage.dropbox_pswd}'{$ext_storage.dropbox_pswd}'
    {else}''{/if};
    if (drop_name && drop_pswd)
        document.getElementById('ext_storage_create').style.display = 'none';


    function create_ext_storage() {
        if (document.getElementById('ext_storage_info').style.display == 'inline')
            document.getElementById('ext_storage_info').style.display = 'none';
        else
            document.getElementById('ext_storage_info').style.display = 'inline';
        if (document.getElementById('ext_storage_thead').style.display == 'inline')
            document.getElementById('ext_storage_thead').style.display = 'none';
        else
            document.getElementById('ext_storage_thead').style.display = 'inline';
    }

    function switch_ext_storage(param) {
        var choosen_ext_storage = document.getElementById('ext_storage_name').value;
        if (choosen_ext_storage == 'dropbox')
            document.getElementById('dropbox_info').style.display = 'inline';
        if (choosen_ext_storage == '' || choosen_ext_storage != 'dropbox')
            document.getElementById('dropbox_info').style.display = 'none';
    }

    function edit_ext_storage(param) {
        if (param == 'dropbox') {
            if (document.getElementById('ext_storage_info').style.display == 'none')
                document.getElementById('ext_storage_info').style.display = 'inline';
            else
                document.getElementById('ext_storage_info').style.display = 'none';

            if (document.getElementById('dropbox_info').style.display == 'none')
                document.getElementById('dropbox_info').style.display = 'inline';
            else
                document.getElementById('dropbox_info').style.display = 'none';
        }
    }

    function verify_data(param) {
        var login = $("#" + param + "_name").val();
        var pswd = $("#" + param + "_pswd").val();

        if (!login || !pswd) {
            jalert('Необходимо заполнить поле логин и пароль');
            return false;
        }

        //проерка на существование введенных данных
        if (login && pswd) {
            $.ajax({
                type: "POST",
                url: "backup.php",
                data: { login: login, pswd: pswd, check: 1, csrf: '{$csrf}' },
                success: function (msg) {
                    if (msg != 'ok') {
                        alert('Логин или пароль неверны!');
                        return false;
                    }
                }
            });
        }
    }

    function doPopup(popupPath) {
        window.open(popupPath, 'name', 'width=500,height=400,scrollbars=YES');
    }
</script>

<script>
    //предотвращение открытия dropdown за пределы экрана (если select находится внизу страницы и у него много опций)
    $('#archive_file').on('chosen:showing_dropdown', function(event, params) {
      var chosen_container = $( event.target ).next( '.chosen-container' );
      var dropdown = chosen_container.find( '.chosen-drop' );
      var dropdown_top = dropdown.offset().top - $(window).scrollTop();
      var dropdown_height = dropdown.height();
      var viewport_height = $(window).height();

      if ( dropdown_top + dropdown_height > viewport_height ) {
          chosen_container.addClass( 'chosen-drop-up' );
      }

    });
    $('#archive_file').on('chosen:hiding_dropdown', function(event, params) {
      $( event.target ).next( '.chosen-container' ).removeClass( 'chosen-drop-up' );
    });
</script>
