{include file="./index_top.tpl"}

{include file="./acc_top.tpl"}

{include file="./acc_condition.tpl"}

<style>
    .pseudo_link {
        color: blue;
        text-decoration: underline;
        cursor: pointer;
    }

    .pseudo_link:hover {
        color: white;
        background-color: blue;
        text-decoration: none;
    }
</style>

<script>
    var change_flag = {};
    function mouseBlue(obj, val) {
        if (val == 1) $(obj).css('backgroundColor', '#C0DDF3');
        else $(obj).css('backgroundColor', 'transparent');
    }
    function set_val(obj_id) {
        if ($('#' + obj_id).val() == 0) {
            $('#' + obj_id).css('display', 'none');
            $('#par_' + obj_id).css('display', '');
            if ($('#par_' + obj_id).attr('o_value') == 2) {
                $('#' + obj_id).val('2');
                $('#par_' + obj_id).attr('src', 'images/check_dis_on_m.png');
                $('#bor_' + obj_id).css('display', 'none');
            }
            if ($('#par_' + obj_id).attr('o_value') == 3) {
                $('#' + obj_id).val('3');
                $('#par_' + obj_id).attr('src', 'images/check_dis_off_m.png');
                $('#bor_' + obj_id).css('display', 'none');
            }
        }
        else if ($('#' + obj_id).val() == 2 || $('#' + obj_id).val() == 3 || $('#' + obj_id).val() == 0) {
            $('#' + obj_id).val('1');
            if ($('#par_' + obj_id).attr('src') == 'images/radio_on_m.png' || $('#par_' + obj_id).attr('src') == 'images/radio_off_m.png') $('#par_' + obj_id).attr('src', 'images/radio_on_m.png');
            else $('#par_' + obj_id).attr('src', 'images/check_on_m.png');
            $('#bor_' + obj_id).css('display', '');
        }
        else {
            $('#' + obj_id).val('0');
            if ($('#par_' + obj_id).attr('src') == 'images/radio_on_m.png' || $('#par_' + obj_id).attr('src') == 'images/radio_off_m.png') $('#par_' + obj_id).attr('src', 'images/radio_off_m.png');
            else $('#par_' + obj_id).attr('src', 'images/check_off_m.png');
            $('#bor_' + obj_id).css('display', '');
        }
        if ($('#' + obj_id).val() != $('#' + obj_id).attr('o_value')) {
            change_flag[obj_id] = 1;
            $('#yellow_' + obj_id).css('backgroundColor', 'rgb(255, 255, 153)');
        }
        else {
            change_flag[obj_id] = 0;
            $('#yellow_' + obj_id).css('backgroundColor', 'transparent');
        }

        if ($('#' + obj_id).attr('o_set')) {
            if ($('#' + obj_id).val() != $('#' + obj_id).attr('o_set')) {
                $('#yellow_' + obj_id).css('backgroundColor', 'rgb(255, 255, 153)');
            }
            else {
                $('#yellow_' + obj_id).css('backgroundColor', 'transparent');
            }
        }

        set_name($('#' + obj_id));
    }
    function check_all(obj) {
        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 {
            $(obj).attr("src", "images/check_dis_off_m.png");
            $(obj).attr("o_value", "2");
            on_off = "dis_on";
        }

        $('[id^="par_acc_"]').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^="bor_acc_"]').each(function () {
            if (on_off == "dis_on") $(this).css('display', 'none');
            else  $(this).css('display', '');
        });

        $('[id^="acc_"]').each(function () {
            if (on_off == 'on') $(this).val('1');
            else if (on_off == 'dis_on') {
                if ($('#par_' + this.id).attr('o_value') == '2') $(this).val('2');
                if ($('#par_' + this.id).attr('o_value') == '3') $(this).val('3');
            }
            else $(this).val('0');

            if ($(this).val() != $(this).attr('o_value')) {
                change_flag[obj] = 1;
                $('#yellow_' + this.id).css('backgroundColor', 'rgb(255, 255, 153)');
            }
            else {
                change_flag[obj] = 0;
                $('#yellow_' + this.id).css('backgroundColor', 'transparent');
            }
            set_name($(this));
        });
    }
    function set_name(obj) {
        if (obj.attr('data-name') != '') {
            if (obj.val() != obj.attr('data-value'))
                obj.attr('name', obj.attr('data-name'));
            else
                obj.attr('name', '');
        }
    }
    $(function () {
        $("a").click(function (event) {
            var a_href = this;
            for (var temp_flag in change_flag) {
                if (change_flag[temp_flag] == 1) {
                    jconfirm('{$lang.lost_page}', function () {
                        location.href = a_href;
                    }, function () {
                    });
                    event.preventDefault();
                    break;
                }
            }
        });
    });
</script>

{if $error}
    <div style="text-align:center;font-size: 13px;">
        <span id="edit_config__info-maessage" style="color:red">{$error}</span><br><br>
    </div>
{/if}
{if !$blocked_rule}
<form action="edit_group.php?group={$group_id}&cat={$cat_id}&cat_item={$cat_item}" method="post" id="comm_rights_form">
    <table>
        <tr>
            <td align="right">{$lang.Name}:</td>
            <td>
                <input class="form-control form-control-250" name="name" value="{$add_name}" disabled/>
            </td>
        </tr>
        <!--tr>
            <td align="right">{$lang.Rule_global}:</td>
            <td>
                <input type="checkbox" name="global" {if $global}checked{/if}/>
            </td>
        </tr-->
    </table>
    <br/>

    <table border=0 cellpadding=0 align="center" style="border-collapse:collapse;" width="300px">
        <tr>
            <td align="left" style="border-bottom:1px solid #BBBBBB"></td>
            <td align="center" style="border-bottom:1px solid #BBBBBB;padding-bottom:4px">
                <img style="margin-top:4px" id="all_acc" src="images/check_off_m.png"
                     onclick="check_all(this)" o_value="2">
            </td>
        </tr>

        {foreach from=$rules_fields item=one_rule_field}
            {if $one_rule_field.name_field}
                <tr onmouseover="mouseBlue(this,1);" onmouseout="mouseBlue(this,0);">
                    <td align="left" style="border-bottom:1px solid #BBBBBB; {if $one_rule_field.blocked}background-color:#f3f3f3;{/if}">{$one_rule_field.name_field}</td>
                    <td align="center" style="border-bottom:1px solid #BBBBBB; {if $one_rule_field.blocked}background-color:#f3f3f3;{/if}">
                        <div id="yellow_acc_{$one_rule_field.id}"
                             style="width:31px;padding-top:8px;padding-bottom:6px;text-align:center;background-color:{if $set_group and $one_rule_field.acc != $one_rule_field.acc_o}rgb(255, 255, 153){/if}">
                            {if !$one_rule_field.blocked}<input type="hidden"
                                   id="acc_{$one_rule_field.id}"
                                   name="{if $set_group and $one_rule_field.acc != $one_rule_field.acc_o}access[{$one_rule_field.id}]{/if}"
                                   value="{$one_rule_field.acc}"
                                   data-name='access[{$one_rule_field.id}]'
                                   data-value='{$one_rule_field.acc}'
                                   {if $one_rule_field.acc}checked{/if}
                                   o_value='{$one_rule_field.acc_o}'/>{/if}
                            <div {if !$one_rule_field.blocked}id="bor_acc_{$one_rule_field.id}"{/if}
                                 {if !$one_rule_field.blocked}onclick="set_val('acc_{$one_rule_field.id}')"{/if}
                                 style="{if !$one_rule_field.acc_flag}display:none;{/if}border: 1px solid #FF7F7F;width:11px;height:11px;position:absolute;margin:5px 0 0 9px"></div>
                            <img {if !$one_rule_field.blocked}id="par_acc_{$one_rule_field.id}"{/if}
                                 src="images/check_{if $one_rule_field.acc==2}dis_on{elseif $one_rule_field.acc==3}dis_off{elseif $one_rule_field.acc}on{else}off{/if}_m.png"
                                 {if !$one_rule_field.blocked}onclick="set_val('acc_{$one_rule_field.id}')"{/if}
                                 o_value='{$one_rule_field.acc_p}'/>
                        </div>
                        {if $set_group and $one_rule_field.acc != $one_rule_field.acc_o}
                        <script>change_flag['acc_{$one_rule_field.id}'] = 1</script>
                        {/if}
                    </td>
                </tr>
            {/if}
        {/foreach}
    </table>
    
    {if !$no_save_mode}
    <br/><br/>
    <div style="text-align:center; color:gray">
        {$lang.Copy_rule_from_group}: &nbsp;
        <select class="form-control form-control-160" onchange="location.href='edit_group.php?group={$group_id}&cat={$cat_id}&cat_item={$cat_item}&menu=rules&subtable_id={$subtable_id}&set_group='+this.value">
            <option></option>
            {foreach from=$group_to_copy item=one_group}
                <option value="{$one_group.id}">{$one_group.name}</option>
            {/foreach}
        </select>
    </div>
    <br/>
    <div>
        <span class="pseudo_link"
              onclick="jconfirm(apply_right_to_all_text, apply_right_to_all); return false;">{$lang.apply_editable_to_all}</span>
        <script>
            var apply_right_to_all_text = "{$lang.confirm_apply_editable_to_all}";
            function apply_right_to_all() {
                document.getElementById('apply_right_to_all').value = 1;
                document.getElementById('comm_rights_form').submit();
            }
        </script>
        <input type="hidden" name="apply_right_to_all" id="apply_right_to_all" value=0>
    </div>
    {/if}
    
    {if $config.protect_of_elements && $user.id == 1}
    <br/>
    <div>
        <span class="pseudo_link"
              onclick="$('#protect_block').toggle();
                       this.innerHTML = this.innerHTML == '{$lang.Protect}' ? '{$lang.Hide}' : '{$lang.Protect}';
                       return false;">{$lang.Protect}</span>
    </div>
    <table id="protect_block" style="display:none">
        <tr><td><br></td></tr>
        <tr>
            <td align="right">{$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>
            </td>
        </tr>
        <tr>
            <td align="right"></td>
            <td>
                <div style="float: left; margin:0 34px; width: 31px; 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: 31px; 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>
            </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: 31px; 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: 31px; 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>
                </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>
    </table>
    {/if}
    <br/><br/>

    <div style="text-align:center">
        <input type="hidden" value="{$selected_right_info}" name="selected_right_info"/>
        <input type="hidden" value="{$subtable_id}" name="subtable_id">
        <input type="hidden" value="{$table_id}" name="table_id"/>
        <input type="hidden" value="{$csrf}" name="csrf"/>
        <input type="hidden" value="subtable_rules" name="save"/>
        <input class="btn btn-default btn-sm" type="submit" value="{$lang.Save}" style="margin-right:10px"/
               {if $no_save_mode}disabled title="{$lang.Element_no_edit_msg}"{/if}/>
        <input class="btn btn-default btn-sm" type="button" value="{$lang.Cancel}"
               onclick="document.location.href='edit_group.php?group={$group_id}&cat={$cat_id}&cat_item={$cat_item}&menu=rules&subtable_id={$subtable_id}'"/>
    </div>
</form>
<br>
{/if}
<a href="edit_group.php?group={if $to_back}{$to_back}{else}{$group_id}{/if}&cat={$cat_id}&cat_item=t{$table_id}&menu=rules">{$lang.return_to_rules}</a>
<br/>
<br/>