{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';
        }
    }
    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>
<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 $addittable_id eq -1}{$lang.Create_addittable}{else}{$lang.Edit_addittable} "{$name_addittable}"{/if} {$lang.of_table} "{$name_table}"
    {if $addittable_id != -1 && !$blocked}
        <div style="left: 10px; top: 2px; float: none;"
            onclick="switchOnObject('{$addittable_id}')"
            class="switch-btn {if !$disabled}switch-on{/if}"
            id="switch-on-btn_{$addittable_id}"></div>
        <script>
            let ajaxObj = new XMLHTTP("edit_addittable.php");
            ajaxObj.format = 0;
            ajaxObj.method = "POST";
            function switchOnObject(addittable_id) {
                $('#switch-on-btn_'+addittable_id).toggleClass('switch-on');
                $('#switch-on-btn_'+addittable_id).trigger('on.switch');
                ajaxObj.call('addittable_id='+addittable_id+'&sel=disable&enbl='+($('#switch-on-btn_'+addittable_id).hasClass('switch-on')?1:0)+'&csrf={$csrf}',()=>{});
            }
        </script>
    {/if}
    <br><br>
    <form action="edit_addittable.php?table={$table_id}&addittable={$addittable_id}" method="post" id="addittable_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 $addittable_id eq -1}
                <!--tr>
                    <td align=center colspan=2 style="padding-bottom:10px">{$lang.Choose_create_addittable}:</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_addittable'></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_addittable'></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-->
                <tr>
                    <td align=right><input type=hidden name="create_mode" value=1>
                        {$lang.addittable_name}<span class='help_bt' h_id='addittable_name'></span>:
                    </td>
                    <td><input class="form-control form-control-250" type=text name="name" value="" size=30></td>
                </tr>
            {elseif !$blocked}
                <tr>
                    <td align=right>{$lang.addittable_name}<span class='help_bt' h_id='addittable_name'></span>:</td>
                    <td><input class="form-control form-control-250" type=text name="name" value="{$name_addittable}" size=30></td>
                </tr>
                <tr>
                    <td align=right>{$lang.Link_table}<span class='help_bt' h_id='link_table_in_addittable'></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_addittable'></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">
                        {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 $addittable_id > 0}<a href="edit_log.php?st_date=00&f_text=edit_addittable.php?table%3D{$table_id}%26addittable%3D{$addittable_id}>" target="_blank" style="padding-left:10px">{$lang.Log}</a>{/if}
                    </td>
                </tr>
                <tr class="config_access_rules_window access_window_header">
                    <td></td>
                    <td>{$lang.Access}</td>
                </tr>
                <tr class="config_access_rules_window access_window_header">
                    <td align="left" valign="top" width="227px"></td>
                    <td>
                        <div style="float: left;margin:0 0 0 -1px;width: 31px; padding-top: 8px; padding-bottom: 6px; text-align: center;">
                            <img style="margin-top:4px;{if $acc_blocked}cursor:not-allowed;{/if}" id="all_read" src="images/check_off_m.png"
                                {if !$acc_blocked}onclick="check_all('read', 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 {if !$acc_blocked}access_checkbox_wrapper{/if}">
                        <td align="right">{$acc.name}:</td>
                        <td>
                            <div class="edit-button__access-rules-checkbox">
                                <input id="read_" name="ob_access[{$gid}]" value="{$acc.access}" data-o_value="{if $acc.access}1{else}0{/if}"
                                       data-type="access" data-gid="{$gid}" {if $acc.pid}data-parent="{$acc.parent}"
                                       data-pid='{$acc.pid.access}'{/if} type="hidden">
                                <img id="par_read_" {if $acc_blocked}style="cursor:not-allowed"{/if}
                                     src="images/check_{if $acc.access==2}dis_on{elseif $acc.access==3}dis_off{elseif $acc.access}on{else}off{/if}_m.png">
                            </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_access'></span></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_acc_all" {if !$protected}disabled{/if}
                                       onclick="$('[id^=pr_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_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;">
                                    <a href="edit_protect.php?group={$gid}&cat={$cat_id}&table={$table_id}&menu=addittables" 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 $addittable_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_addittable.php?table={$table_id}{/if}">{$lang.Return}</a> 
                </td>
            </tr>
        </table>
    </form>
    <br>
</div>
<script src="include/js/configuration.js?{$revision}"></script>