{include file="./index_top.tpl"}
<style type="text/css">
    #sync_form table {
        width: 100%;
    }

    td.left_col {
        text-align: right;
        width: 200px;
    }

    td.right_col {
        text-align: left;
        width: 200px;
    }
</style>
<script type="text/javascript">
    function checkFormData() {
        if (document.getElementById('sync_name').value == '') {
            jalert(lang.alert_sync_save);
            return false;
        }
        else if (document.getElementById("type_mode").value == "http") {
            http_pass = document.getElementById("http_pass").value;
            if (http_pass.length < 6) {
                jalert(lang.sync_pass_alert);
                return false;
            }
            else {
                checkPass();
                return false;
            }
        }
    }

    function checkPass() {
        http_pass = document.getElementById("http_pass").value;
        $.ajax({
            type: "POST",
            url: "edit_sync.php",
            data: { sync_id: {$one_sync.id}, check_password: http_pass, csrf: '{$csrf}' },
            success: function (msg) {
                if (msg == "1")
                    jalert(lang.sync_act_pass_alert);
                else
                    document.getElementById("sync_form").submit();
            }
        });
    }

    function changeMode() {
        typeMode = document.getElementById("type_mode").value;

        if (typeMode == "local" || typeMode == "ftp") document.getElementById("folder_block").style.display = 'table';
        else document.getElementById("folder_block").style.display = 'none';

        if (typeMode == "ftp") document.getElementById("ftp_mode_block").style.display = 'table';
        else document.getElementById("ftp_mode_block").style.display = 'none';

        if (typeMode == "http") document.getElementById("http_mode_block").style.display = 'table';
        else document.getElementById("http_mode_block").style.display = 'none';
    }

    function primarySync(syncId) {
        document.getElementById('primary_sync').innerHTML = lang.Running + '... <img src="images/indicator.gif">';
        document.getElementById('primary_sync').style.fontStyle = 'oblique';
        document.getElementById('primary_sync').style.color = '#777777';
        location.href = "edit_sync.php?full_sync={$one_sync.id}";
    }

    $(document).ready(function () {
        changeMode();
    });
</script>
<div align=center>
    <br>{$lang.Sync_settings}<br><br>
    <form action="edit_sync.php?sel=save&sync_id={$one_sync.id}" method="post" id="sync_form" class="edit_sync_block"
          onsubmit="return checkFormData()">
        {if $sync_error}
            <div style="text-align: center; color: #f00">{$lang.{$sync_error}} {if $sync_error eq '1c_integration'} {$lang['1c_not_aсtived']} {/if} {if $sync_error eq 'no_write_to_folder'}&quot;temp&quot;{/if}{if $sync_error eq 'invalid_write_rights'}&quot;{$one_sync.upload_dir}&quot;{/if}
            <br/>
            <br/>
            </div>{/if}
        <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 class="left_col">{$lang.Sync_name}<span class='help_bt' h_id='sync_name'></span></td>
                <td class="right_col">
                    <input class="form-control form-control-160" type="text" id="sync_name" name="sync_name"
                           value="{$one_sync.name}" maxlength="255"/>
                </td>
            </tr>
            <tr>
                <td class="left_col">{$lang.Sync_mode}<span class='help_bt' h_id='sync_modes'></span></td>
                <td class="right_col">
                    <select class="form-control form-control-150" id="sync_mode" name="sync_mode" onchange="changeMode()">
                        <option value="1" {if $one_sync.sync_mode eq 1}selected{/if}>{$lang.Sync_mode_act}</option>
                        <option value="0" {if $one_sync.sync_mode eq 0}selected{/if}>{$lang.Sync_mode_pas}</option>
                        {if $config.modules.1csync}
                            <option value="3"
                                    {if $one_sync.sync_mode eq 3}selected{/if}>{$lang.1c_integration}</option>{/if}
                    </select>
                </td>
            </tr>
            <tr>
                <td class="left_col">{$lang.Sync_type}<span class='help_bt' h_id='sync_types'></span></td>
                <td class="right_col">
                    <select class="form-control form-control-150" id="type_mode" name="type_mode" onchange="changeMode()">
                        <option value="http"{if $one_sync.type_mode eq 2} selected{/if}>HTTP</option>
                        <option value="local" {if $one_sync.type_mode eq 0} selected{/if}>{$lang.Sync_type_local}</option>
                        <option value="ftp"{if $one_sync.type_mode eq 1} selected{/if}>FTP</option>
                    </select>
                </td>
            </tr>
        </table>
        <table border="0" cellpadding="3" id="ftp_mode_block" {if $one_sync.type_mode neq 1}style="display: none"{/if}>
            <tr>
                <td class="left_col">{$lang.Sync_ftp_server}<span class='help_bt' h_id='sync_ftp_server'></span></td>
                <td class="right_col">
                    <input class="form-control form-control-160" type="text" id="ftp_server" name="ftp_server"
                           value="{$one_sync.ftp_server}" maxlength="240"/>
                </td>
            </tr>
            <tr>
                <td class="left_col">{$lang.Sync_ftp_port}<span class='help_bt' h_id='sync_ftp_port'></span></td>
                <td class="right_col">
                    <input class="form-control form-control-160" type="text" id="ftp_port" name="ftp_port"
                           value="{$one_sync.ftp_port}"maxlength="5"/>
                </td>
            </tr>
            <tr>
                <td class="left_col">{$lang.Sync_ftp_login}<span class='help_bt' h_id='sync_ftp_login'></span></td>
                <td class="right_col">
                    <input class="form-control form-control-160" type="text" id="ftp_login" name="ftp_login"
                           value="{$one_sync.ftp_login}" maxlength="125"/>
                </td>
            </tr>
            <tr>
                <td class="left_col">{$lang.Sync_ftp_pass}<span class='help_bt' h_id='sync_ftp_pass'></span></td>
                <td class="right_col">
                    <input class="form-control form-control-160" type="text" id="ftp_pass" name="ftp_pass"
                           value="{$one_sync.ftp_pass}" maxlength="125"/>
                </td>
            </tr>
        </table>
        <table border="0" cellpadding="3" id="http_mode_block" {if $one_sync.type_mode neq 2}style="display: none"{/if}>
            <tr>
                <td class="left_col">{$lang.Password}<span class='help_bt' h_id='sync_http_pass'></span></td>
                <td class="right_col">
                    <input class="form-control form-control-160" type="text" id="http_pass" name="http_pass"
                           value="{$one_sync.http_pass}" maxlength="125"/></td>
            </tr>
        </table>
        <table border="0" cellpadding="3" id="folder_block" {if $one_sync.type_mode eq 2}style="display: none"{/if}>
            <tr>
                <td class="left_col">{$lang.Sync_upload_dir}<span class='help_bt' h_id='sync_upload_dir'></span></td>
                <td class="right_col">
                    <input class="form-control form-control-160" type="text" id="upload_dir" name="upload_dir"
                           value="{$one_sync.upload_dir}" maxlength="255"/>
                </td>
            </tr>
            <tr>
                <td class="left_col">{$lang.Sync_download_dir}<span class='help_bt' h_id='sync_download_dir'></span>
                </td>
                <td class="right_col">
                    <input class="form-control form-control-160" type="text" id="download_dir" name="download_dir"
                           value="{$one_sync.download_dir}" maxlength="255"/>
                </td>
            </tr>
        </table>
        <table border="0" cellpadding="3">
            <tr>
                <td class="left_col">{$lang.sync_remote_access}<span class='help_bt' h_id='http_acc_net'></span></td>
                <td class="right_col">
                    <input type="checkbox" {if $one_sync.http_type} checked{/if} name="http_type" value="1"
                           onclick="$('#http_mode_client').toggle()">
                </td>
            </tr>
            <tbody id="http_mode_client"{if $one_sync.http_type eq 0} style="display: none"{/if}>
            <tr>
                <td class="left_col">{$lang.Sync_remote_address}<span class='help_bt' h_id='sync_http_server'></span>
                </td>
                <td class="right_col">
                    <input class="form-control form-control-160" type="text" name="http_server"
                           value="{$one_sync.http_server}" maxlength="240"></td>
            </tr>
            </tbody>
        </table>
        <table border="0" cellpadding="3">
            <tr>
                <td class="left_col">{$lang.Log}<span class='help_bt' h_id='sync_log_on'></span></td>
                <td class="right_col">
                    <input type="checkbox" name="log_on" value="1" {if $one_sync.log_on} checked{/if} />
                    {if $one_sync.id neq -1}&nbsp;&nbsp;<a
                        href="edit_log.php?sync={$one_sync.id}">{$lang.View_log} &raquo;</a>{/if}
                </td>
            </tr>
        </table>
        <div>
            <a href="" onclick="$('#additional_block').show(); return false;">{$lang.Additional}</a>
        </div>
        <table border="0" cellpadding="3" id="additional_block" style="display:none">
            <tr>
                <td class="left_col">{$lang.User_sync_mode}<span class='help_bt' h_id='user_sync_mode'></span></td>
                <td class="right_col">
                    <select class="form-control form-control-150" name="user_sync_mode">
                        <option value="0"{if $one_sync.user_sync_mode eq 0} selected{/if}>ID</option>
                        <option value="1"{if $one_sync.user_sync_mode eq 1} selected{/if}>{$lang.Login}</option>
                        <option value="2"{if $one_sync.user_sync_mode eq 2} selected{/if}>{$lang.Fillot}</option>
                    </select>
                </td>
            </tr>
        </table>
        <div id="primary_sync">
            {if $one_sync.id neq -1}
                <a href="edit_sync.php?full_sync={$smarty.get.sync_id}"
                   onclick="{if $smarty.get.sync_id eq -1}jalert('{$lang.sync_no_fields}'); return false{elseif $sync_error}jalert('{$lang.{$sync_error}} {if $sync_error eq 'invalid_write_rights'}&quot;{$one_sync.upload_dir}&quot;{/if}'); return false{else}return jconfirm('{$lang.alert_sync_primary}', function () { primarySync() });{/if}">{$lang.Sync_primary}</a>
            {/if}
            {if $smarty.get.primary_sync eq 'done'}
                <span style="color: #35B51A"> - {$lang.Done} <img src="images/well.gif"></span>
            {/if}
            {if $smarty.get.primary_sync && $smarty.get.primary_sync neq 'done'}
                <span style="color: #FF3300">{$lang.{$smarty.get.primary_sync}} {if $smarty.get.primary_sync eq 'invalid_write_rights'}&quot;{if $one_sync.type_mode}temp/sync_{$one_sync.id}/export{else}{$one_sync.upload_dir}{/if}&quot;{/if}</span>
            {/if}
        </div>
        <div>
            {if $one_sync.id neq -1}
                <a href="edit_sync.php?sync_id={$smarty.get.sync_id}&sel=manual">{$lang.Sync_manual}</a>
            {/if}
        </div>
        <br/>
        <input type=hidden value="{$back_url_encoded}" name="back_url">
        <input type=hidden value="{$csrf}" name="csrf">
        <input class="btn btn-default btn-sm" type=submit value="{$lang.Save}" name="save">
        <br/><br/>
        <a href="edit_sync.php">{$lang.Return}</a> 
    </form>
</div>