{include file="./index_top.tpl"}
<script>
    function add_settings() {
        if (document.getElementById('open_add_set').innerHTML == '{$lang.Additional}') {
            document.getElementById('open_add_set').innerHTML = '{$lang.Hide}';
            document.getElementById('additional_settings').style.display = '';
        }
        else {
            document.getElementById('open_add_set').innerHTML = '{$lang.Additional}';
            document.getElementById('additional_settings').style.display = 'none';
        }
    }
</script>
<div align=center>
    <br>
    {if $no_save_mode}<img src="images/lock.svg" style="width:20px; padding-right:5px;" title="{$lang.Element_no_edit_msg}">{/if}
    {if $subtable_id eq -1}{$lang.Create_subtable}{else}{$lang.Edit_subtable} "{$name_subtable}"{/if} {$lang.of_table} "{$name_table}"
    {if $subtable_id != -1 && !$blocked}
        <div style="left: 10px; top: 2px; float: none;"
            onclick="switchOnObject('{$subtable_id}')"
            class="switch-btn {if !$disabled}switch-on{/if}"
            id="switch-on-btn_{$subtable_id}"></div>
        <script>
            let ajaxObj = new XMLHTTP("edit_subtable.php");
            ajaxObj.format = 0;
            ajaxObj.method = "POST";
            function switchOnObject(subtable_id) {
                $('#switch-on-btn_'+subtable_id).toggleClass('switch-on');
                $('#switch-on-btn_'+subtable_id).trigger('on.switch');
                ajaxObj.call('subtable_id='+subtable_id+'&sel=disable&enbl='+($('#switch-on-btn_'+subtable_id).hasClass('switch-on')?1:0)+'&csrf={$csrf}',()=>{});
            }
        </script>
    {/if}
    <br><br>
    <form action="edit_subtable.php?table={$table_id}&subtable={$subtable_id}" method="post" id="subtable_form">
        <table border=0 cellpadding=4 cellspacing=0>
            {if $error}
                <tr>
                    <td align=center colspan=2 style="color:red;padding-bottom:15px">{$error}</td>
                </tr>
            {/if}
            {if $message}
                <tr>
                    <td colspan=2 align=center>
                        <span id="edit_config__info-maessage" style="color:green">{$message}</span><br><br>
                    </td>
                </tr>
            {/if}
            {if $subtable_id eq -1}
                <tr>
                    <td align=center colspan=2 style="padding-bottom:10px">{$lang.Choose_create_subtable}:</td>
                </tr>
                <tr>
                    <td align=right><input type=radio name="create_mode" value=1 id="create_mode1" checked>
                        <label for="create_mode1">{$lang.create_new_table_with_name}</label><span class='help_bt'
                                                                                                  h_id='create_new_table_in_subtable'></span>:
                    </td>
                    <td><input class="form-control form-control-250" type=text name="name" id="name" value="" size=40
                               onclick="$('#create_mode1').prop('checked', true)"></td>
                </tr>
                <tr>
                    <td align=right><input type=radio name="create_mode" value=2 id="create_mode2">
                        <label for="create_mode2">{$lang.bind_existing_table}</label><span class='help_bt'
                                                                                           h_id='bind_existing_table_in_subtable'></span>:
                    </td>
                    <td><select class="form-control form-control-250" name="link_table_id"
                                onclick="$('#create_mode2').prop('checked', true)">{$sel_link_table}</select></td>
                </tr>
            {elseif !$blocked}
                <tr>
                    <td align=right>{$lang.Subtable_name}<span class='help_bt' h_id='subtable_name'></span>:</td>
                    <td><input class="form-control form-control-250" type=text name="name" value="{$name_subtable}" size=30></td>
                </tr>
                <tr>
                    <td align=right>{$lang.Link_table}<span class='help_bt' h_id='link_table_in_subtable'></span>:</td>
                    <td><a href="edit_tabrep.php?table={$link_table_id}">{$link_table_name}</a></td>
                </tr>
                <tr>
                    <td align=right>{$lang.Link_on_field}<span class='help_bt' h_id='link_field_in_subtable'></span>:
                    </td>
                    <td><select class="form-control form-control-250" name="link_field_id">{$sel_link_field}</select></td>
                </tr>
                <tr>
                    <td></td>
                    <td colspan="3">
                        <a href="" id="open_add_set" style="width: 110px;display: inline-block">{$lang.Additional}</a>
                        {if $user.group_id == 1 || $user.sub_admin_rights.set_rights.tables[$table_id] == 2}<a href="" id="config_access_rules" style="width: 110px;display: inline-block">{$lang.Access_rules}</a>{/if}
                        {if $config.protect_of_elements && $user.id == 1}<a href="" id="protect_switch">{$lang.Protect}</a>{/if}
                        {if $subtable_id > 0}<a href="edit_log.php?st_date=00&f_text=edit_subtable.php?table%3D{$table_id}%26subtable%3D{$subtable_id}>" target="_blank" style="padding-left:10px">{$lang.Log}</a>{/if}
                    </td>
                </tr>
                <tbody id="additional_settings" style="display:none">
                <tr>
                    <td align=right>{$lang.Filter}<span class='help_bt' h_id='subtable_filter'></span>:</td>
                    <td><select class="form-control form-control-250 chosen" name="filter_id">{$sel_filter}</select></td>
                </tr>
                <tr>
                    <td align=right>{$lang.Autodelete_archiving}<span class='help_bt' h_id='subtable_autostatus'></span>:
                    </td>
                    <td><input class="" type=checkbox name="autostatus" {if $autostatus}checked{/if}></td>
                </tr>
                <tr>
                    <td align=right>{$lang.Autocopy}<span class='help_bt' h_id='subtable_autocopy'></span>:</td>
                    <td><input class="" type=checkbox name="autocopy" {if $autocopy}checked{/if}></td>
                </tr>
                </tbody>
                <tr class="config_access_rules_window access_window_header">
                    <td align=right></td>
                    <td>
                        <span class="access_param_wrapper">{$lang.Access}</span>
                        <span class="access_param_wrapper">{$lang.Add_lines}</span>
                        <span class="access_param_wrapper">{$lang.Delete_lines}</span>
                        <span class="access_param_wrapper">{$lang.Jumping_to_record}</span>
                    </td>
                </tr>
                <tr class="config_access_rules_window access_window_header">
                    <td></td>
                    <td>
                        <div style="float: left; margin:0 34px; width: 60px; padding-top: 8px; padding-bottom: 6px; text-align: center;">
                            <img style="margin-top:4px;{if $acc_blocked.read_acc}cursor:not-allowed;{/if}" id="all_read" src="images/check_off_m.png" 
                                {if !$acc_blocked.read_acc}onclick="check_all('read', this)"{/if} o_value="{if $parent}2{else}0{/if}">
                        </div>
                        <div style="float: left; margin:0 34px; width: 60px; padding-top: 8px; padding-bottom: 6px; text-align: center;">
                            <img style="margin-top:4px;{if $acc_blocked.add_acc}cursor:not-allowed;{/if}" id="all_add" src="images/check_off_m.png" 
                                {if !$acc_blocked.add_acc}onclick="check_all('add', this)"{/if} o_value="{if $parent}2{else}0{/if}">
                        </div>
                        <div style="float: left; margin:0 34px; width: 60px; padding-top: 8px; padding-bottom: 6px; text-align: center;">
                            <img style="margin-top:4px;{if $acc_blocked.del_acc}cursor:not-allowed;{/if}" id="all_del" src="images/check_off_m.png" 
                                {if !$acc_blocked.del_acc}onclick="check_all('del', this)"{/if} o_value="{if $parent}2{else}0{/if}">
                        </div>
                        <div style="float: left; margin:0 34px; width: 60px; padding-top: 8px; padding-bottom: 6px; text-align: center;">
                            <img style="margin-top:4px;{if $acc_blocked.jump_acc}cursor:not-allowed;{/if}" id="all_jump" src="images/check_off_m.png" 
                                {if !$acc_blocked.jump_acc}onclick="check_all('jump', this)"{/if} o_value="{if $parent}2{else}0{/if}">
                        </div>
                    </td>
                </tr>
                {foreach from=$access_data key=gid item=acc}
                    <tr class="config_access_rules_window access_window_eyes access_checkbox_wrapper">
                        <td align=right>{$acc.name}:</td>
                        <td>
                            {if $acc_blocked.read_acc}
                            <p style="float: left; margin:0 34px; width: 60px; padding-top: 8px; padding-bottom: 6px; text-align: center; cursor:not-allowed;">
                            {else}
                            <div style="float: left; margin:0 34px; width: 60px; padding-top: 8px; padding-bottom: 6px; text-align: center;">
                            {/if}
                                <input id="check_read_" name="ob_access[{$gid}][read_acc]" value="{$acc.access.read_acc}"
                                       data-type="read_acc" data-gid="{$gid}" {if $acc.pid}data-parent="{$acc.parent}" data-pid='{$acc.pid.read_acc}'{/if} 
                                       data-o_value="{if $acc.access.read_acc}1{else}0{/if}" type="hidden">
                                <img id="par_read_" src="images/check_{if $acc.access.read_acc==2}dis_on{elseif $acc.access.read_acc==3}dis_off{elseif $acc.access.read_acc}on{else}off{/if}_m.png">
                            {if $acc_blocked.read_acc}</p>{else}</div>{/if}
                            {if $acc_blocked.add_acc}
                            <p style="float: left; margin:0 34px; width: 60px; padding-top: 8px; padding-bottom: 6px; text-align: center; cursor:not-allowed;">
                            {else}
                            <div style="float: left; margin:0 34px; width: 60px; padding-top: 8px; padding-bottom: 6px; text-align: center;">
                            {/if}
                                <input id="check_add_" name="ob_access[{$gid}][add_acc]" value="{$acc.access.add_acc}"
                                       data-type="add_acc" data-gid="{$gid}" {if $acc.pid}data-parent="{$acc.parent}" data-pid='{$acc.pid.add_acc}'{/if} 
                                       data-o_value="{if $acc.access.add_acc}1{else}0{/if}" type="hidden">
                                <img id="par_add_" src="images/check_{if $acc.access.add_acc==2}dis_on{elseif $acc.access.add_acc==3}dis_off{elseif $acc.access.add_acc}on{else}off{/if}_m.png">
                            {if $acc_blocked.add_acc}</p>{else}</div>{/if}
                            {if $acc_blocked.del_acc}
                            <p style="float: left; margin:0 34px; width: 60px; padding-top: 8px; padding-bottom: 6px; text-align: center; cursor:not-allowed;">
                            {else}
                            <div style="float: left; margin:0 34px; width: 60px; padding-top: 8px; padding-bottom: 6px; text-align: center;">
                            {/if}
                                <input id="check_del_" name="ob_access[{$gid}][del_acc]" value="{$acc.access.del_acc}" 
                                       data-type="del_acc" data-gid="{$gid}" {if $acc.pid}data-parent="{$acc.parent}" data-pid='{$acc.pid.del_acc}'{/if} 
                                       data-o_value="{if $acc.access.del_acc}1{else}0{/if}" type="hidden">
                                <img id="par_del_" src="images/check_{if $acc.access.del_acc==2}dis_on{elseif $acc.access.del_acc==3}dis_off{elseif $acc.access.del_acc}on{else}off{/if}_m.png">
                            {if $acc_blocked.del_acc}</p>{else}</div>{/if}
                            {if $acc_blocked.jump_acc}
                            <p style="float: left; margin:0 34px; width: 60px; padding-top: 8px; padding-bottom: 6px; text-align: center; cursor:not-allowed;">
                            {else}
                            <div style="float: left; margin:0 34px; width: 60px; padding-top: 8px; padding-bottom: 6px; text-align: center;">
                            {/if}
                                <input id="check_jump_" name="ob_access[{$gid}][jump_acc]" value="{$acc.access.jump_acc}"
                                       data-type="jump_acc" data-gid="{$gid}" {if $acc.pid}data-parent="{$acc.parent}" data-pid='{$acc.pid.jump_acc}'{/if} 
                                       data-o_value="{if $acc.access.jump_acc}1{else}0{/if}" type="hidden">
                                <img id="par_jump_" src="images/check_{if $acc.access.jump_acc==2}dis_on{elseif $acc.access.jump_acc==3}dis_off{elseif $acc.access.jump_acc}on{else}off{/if}_m.png">
                            {if $acc_blocked.jump_acc}</p>{else}</div>{/if}
                            <div style="float: left; padding-top: 8px; padding-bottom: 6px; text-align: center;">
                                <a href="edit_group.php?group={$gid}&cat={$cat_id}&cat_item=t{$link_table_id}&menu=rules&subtable_id={$subtable_id}" target="_blanc" 
                                   style="width: 110px;display: inline-block">{$lang.Fields}</a>
                            </div>
                        </td>
                   </tr>
                {/foreach}
                {if $config.protect_of_elements && $user.id == 1}
                <tbody id="protect_block" style="display:none">
                    <tr>
                        <td align="right" style="vertical-align: bottom">{$lang.Protected}<span class='help_bt' h_id='Protect_of_element'></span>:</td>
                        <td>
                            <input type="checkbox" name="protected" {if $protected}checked{/if} onclick="$('[id^=pr_]').prop('disabled', !this.checked)">
                            <span class="access_param_wrapper">{$lang.View2}<span class='help_bt' h_id='protect_of_view'></span></span>
                            <span class="access_param_wrapper">{$lang.Edit3}<span class='help_bt' h_id='protect_of_edit'></span></span>
                            {if $config.access_protect_mode == 2}
                            <span class="access_param_wrapper">{$lang.Access}<span class='help_bt' h_id='protect_of_custom_access'></span></span>
                            <span class="access_param_wrapper">{$lang.Access}<span class='help_bt' h_id='protect_of_custom_access'></span><br>{$lang.Add_lines}</span>
                            <span class="access_param_wrapper">{$lang.Access}<span class='help_bt' h_id='protect_of_custom_access'></span><br>{$lang.Delete_lines}</span>
                            <span class="access_param_wrapper">{$lang.Access}<span class='help_bt' h_id='protect_of_custom_access'></span><br>{$lang.Jumping_to_record}</span>
                            {/if}
                        </td>
                    </tr>
                    <tr>
                        <td align="right"></td>
                        <td>
                            <div style="float: left; margin:0 34px; width: 60px; padding: 8px 0 6px 15px; text-align: center;">
                                <input type="checkbox" id="pr_view_all" {if !$protected}disabled{/if}
                                       onclick="$('[id^=pr_view]').prop('checked', this.checked); if (!this.checked) $('[id^=pr_edit]').prop('checked', 0);">
                            </div>
                            <div style="float: left; margin:0 34px; width: 60px; padding: 8px 0 6px 0; text-align: center;">
                                <input type="checkbox" id="pr_edit_all" {if !$protected}disabled{/if}
                                       onclick="$('[id^=pr_edit]').prop('checked', this.checked); if ( this.checked) $('[id^=pr_view]').prop('checked', 1);">
                            </div>
                            {if $config.access_protect_mode == 2}
                            <div style="float: left; margin:0 34px; width: 60px; padding: 8px 0 6px 0; text-align: center;">
                                <input type="checkbox" id="pr_read_acc_all" {if !$protected}disabled{/if}
                                       onclick="$('[id^=pr_read_acc]').prop('checked', this.checked);">
                            </div>
                            <div style="float: left; margin:0 34px; width: 60px; padding: 8px 0 6px 0; text-align: center;">
                                <input type="checkbox" id="pr_add_acc_all" {if !$protected}disabled{/if}
                                       onclick="$('[id^=pr_add_acc]').prop('checked', this.checked);">
                            </div>
                            <div style="float: left; margin:0 34px; width: 60px; padding: 8px 0 6px 0; text-align: center;">
                                <input type="checkbox" id="pr_del_acc_all" {if !$protected}disabled{/if}
                                       onclick="$('[id^=pr_del_acc]').prop('checked', this.checked);">
                            </div>
                            <div style="float: left; margin:0 34px; width: 60px; padding: 8px 0 6px 0; text-align: center;">
                                <input type="checkbox" id="pr_jump_acc_all" {if !$protected}disabled{/if}
                                       onclick="$('[id^=pr_jump_acc]').prop('checked', this.checked);">
                            </div>
                            {/if}
                        </td>
                    </tr>
                    {foreach from=$protect key=gid item=acc}
                        <tr>
                            <td align="right" style="padding-top: 11px;">{$acc.name}:</td>
                            <td style="display: flex; flex-wrap: nowrap;">
                                <div style="float: left; margin:0 34px; width: 60px; padding: 8px 0 6px 15px; text-align: center;">
                                    <input type="checkbox" name="protect_rights[{$gid}][view]" id="pr_view{$gid}" value="1"
                                           {if $acc.view}checked{/if} {if !$protected}disabled{/if}
                                           onclick="if (!this.checked) $('#pr_edit{$gid}').prop('checked', 0)">
                                </div>
                                <div style="float: left; margin:0 34px; width: 60px; padding: 8px 0 6px 0; text-align: center;">
                                    <input type="checkbox" name="protect_rights[{$gid}][edit]" id="pr_edit{$gid}" value="1"
                                           {if $acc.edit}checked{/if} {if !$protected}disabled{/if}
                                           onclick="if ( this.checked) $('#pr_view{$gid}').prop('checked', 1)">
                                </div>
                                {if $config.access_protect_mode == 2}
                                <div style="float: left; margin:0 34px; width: 60px; padding: 8px 0 6px 0; text-align: center;">
                                    <input type="checkbox" name="protect_rights[{$gid}][acc]" id="pr_read_acc{$gid}" value="1"
                                           {if $acc.acc}checked{/if} {if !$protected}disabled{/if}>
                                </div>
                                <div style="float: left; margin:0 34px; width: 60px; padding: 8px 0 6px 0; text-align: center;">
                                    <input type="checkbox" name="protect_rights[{$gid}][ext][add_acc]" id="pr_add_acc{$gid}" value="1"
                                           {if $acc.ext.add_acc}checked{/if} {if !$protected}disabled{/if}>
                                </div>
                                <div style="float: left; margin:0 34px; width: 60px; padding: 8px 0 6px 0; text-align: center;">
                                    <input type="checkbox" name="protect_rights[{$gid}][ext][del_acc]" id="pr_del_acc{$gid}" value="1"
                                           {if $acc.ext.del_acc}checked{/if} {if !$protected}disabled{/if}>
                                </div>
                                <div style="float: left; margin:0 34px; width: 60px; padding: 8px 0 6px 0; text-align: center;">
                                    <input type="checkbox" name="protect_rights[{$gid}][ext][jump_acc]" id="pr_jump_acc{$gid}" value="1"
                                           {if $acc.ext.jump_acc}checked{/if} {if !$protected}disabled{/if}>
                                </div>
                                <div style="float: left; margin:0 34px; width: 60px; padding: 8px 0 6px 0; text-align: center;">
                                    <a href="edit_protect.php?group={$gid}&cat={$cat_id}&table={$table_id}&menu=subtables" target="_blanc">{$lang.Go}</a>
                                </div>
                                {/if}
                            </td>
                        </tr>
                    {/foreach}
                    <tr><td></td></tr>
                    <tr>
                        <td align=right>{$lang.Comment}<span class='help_bt' h_id='protect_comment'></span>:</td>
                        <td><textarea class="form-control form-control-250" name="protect_comment" rows=5>{$protect_comment}</textarea></td>
                    </tr>
                </tbody>
                {/if}
            {/if}
            <tr>
                <td colspan=2 align=center>
                    {if !$blocked}
                    <br>
                    {if $subtable_id neq -1}<input type=hidden name="link_table_id" value="{$link_table_id}">{/if}
                    <input type=hidden name=back_url value="{$base64_back_url}">
                    <input type=hidden name=csrf value="{$csrf}">
                    <input class="btn btn-default btn-sm" type=submit name='save' value="{$lang.Save}"
                           onclick="if ($('#name').val()=='' && $('#create_mode2').prop('checked')==false) { jalert('{$lang.must_enter_name_table}'); return false; }"
                           {if $no_save_mode == 2}disabled title="{$lang.Element_no_edit_msg}"{/if}>
                    <br/><br/>
                    {/if}
                    <a href="{if $back_url}{$back_url}{else}edit_subtable.php?table={$table_id}{/if}">{$lang.Return}</a> 
                </td>
            </tr>
        </table>
    </form>
    <br>
</div>
<script>
function check_all(type, obj) {
    {if $parent}
        par = 1;
    {else}
        par = 0;
    {/if}

    if ($(obj).attr("o_value") == "2") {
        $(obj).attr("src", "images/check_on_m.png");
        $(obj).attr("o_value", "1");
        on_off = "on";
    }
    else if ($(obj).attr("o_value") == "1") {
        $(obj).attr("src", "images/check_off_m.png");
        $(obj).attr("o_value", "0");
        on_off = "off";
    }
    else if (par) {
        $(obj).attr("src", "images/check_dis_off_m.png");
        $(obj).attr("o_value", "2");
        on_off = "dis_on";
    }
    else {
        $(obj).attr("src", "images/check_on_m.png");
        $(obj).attr("o_value", "1");
        on_off = "on";
    }

    $('[id^="par_' + type + '_"]').each(function () {
        $(this).attr("src", "images/check_" + on_off + "_m.png");
        if ($(this).attr("o_value") == 3 && on_off == "dis_on")  $(this).attr("src", "images/check_dis_off_m.png");
    });
    $('[id^="' + type + '_"]').each(function () {
        if (on_off == 'on') {
            $(this).val('1');
        }
        else if (on_off == 'dis_on') {
            if ($(this).attr('o_value') == '2') $(this).val('2');
            if ($(this).attr('o_value') == '3') $(this).val('3');
        }
        else {
            $(this).val('0');
        }
        if ($(this).val() != $(this).attr('data-o_value')) {
            $(this).parent().css('backgroundColor', 'rgb(255, 255, 153)');
        }
        else {
            $(this).parent().css('backgroundColor', 'transparent');
        }
    });
}
</script>
<script src="include/js/configuration.js?{$revision}"></script>