{include file="./index_top.tpl"}
{include file="./acc_top.tpl"}
<script>
    function send_cat_acc(group, cat, obj) {
        acc_img = $(obj).attr("src");
        if (acc_img == "images/acc_allow.png") {
            acc = "2";
        }
        else if (acc_img == "images/acc_invisibe.png") {
            acc = "0";
        }
        else {
            acc = "1";
        }

        $(obj).parent().parent().css('backgroundColor', '#fff6ad');
        $(obj).parent().parent().attr('b_col', acc);

        $.ajax({
            type: "POST",
            url: "edit_group.php",
            data: { save: '1', group: group, cat: cat, access: acc, csrf: '{$csrf}' },
            success: function (msg) {
                if ($(obj).parent().parent().attr('b_col') == msg) {
                    $(obj).parent().parent().css('backgroundColor', 'transparent');
                    $(obj).parent().parent().attr('b_col', '');
                }
            }
        });
    }
    function change_eye(obj) {
        acc = $(obj).attr("src");
        if (acc == "images/acc_allow.png") {
            acc_img = "invisibe";
        }
        else if (acc == "images/acc_invisibe.png") {
            acc_img = "close";
        }
        else {
            acc_img = "allow";
        }
        $(obj).attr("src", "images/acc_" + acc_img + ".png");
    }
    function change_pid(obj) {
        var o_value = "{$group.pid}";
        if ($(obj).val() != o_value) {
            $('#set_parent').val("1");
        }
        else {
            $('#set_parent').val("0");
        }
    }
    function send_submit(add_rule) {
        if (add_rule) {
            $('#subadmin_add_rule').val(1);
        }

        if ($("[name=scheduled_access_on]").prop('checked')) {
            var sch_error;
            $("#scheduled_access input").each(function(indx, element){
                if (!$(element).val().match(/\d\d:\d\d\-\d\d:\d\d/) && !sch_error) {
                    sch_error = $(element).val();
                }
            });
            if (sch_error) {
                jalert('{$lang.scheduled_access_error}: ' + sch_error);
                return;
            }
        }

        var o_value = "{$group.pid}";
        if ((!o_value && $('#pid').val() == 0) || o_value == $('#pid').val() || "{$group_id == 1}") {
            $('*').css('cursor', 'wait');
            $('#send_form').submit();
        }
        else {
            jconfirm('{$lang.Subordinate_group_alert}',
                    function () {
                        $('*').css('cursor', 'wait');
                        $('#send_form').submit();
                    },
                    function () {
                        obj.selectedIndex = 0;
                    });
        }
    }

    function open_subadmin() {
        if ($('#sub_admin_checkbox').prop('checked')) {
            $('#subadmin_settings_title').css('display', '');
            $('#subadmin_settings').css('display', '');
        }
        else {
            $('#subadmin_settings_title').css('display', 'none');
            $('#subadmin_settings').css('display', 'none');
        }
    }

    function set_val(type, max_val) {
        if (max_val == 1 && $('#' + type).val() == 1) $('#' + type).val(2);

        if ($('#' + type).val() == 2) {
            $('#' + type + '_pen').attr('src', 'images/acc_pencil_red.png');
            $('#' + type).val(0);
        }
        else if ($('#' + type).val() == 1) {
            $('#' + type + '_pen').attr('src', 'images/acc_pencil_green.png');
            $('#' + type).val(2);
        }
        else {
            $('#' + type + '_pen').attr('src', 'images/acc_pencil_yellow.png');
            $('#' + type).val(1);
        }
    }

    function set_val_box(type) {
        if ($('#' + type).val() == 2) {
            $('#' + type).val(1);
        }
        else if ($('#' + type).val() == 1) {
            $('#' + type).val(0);
        }
        else {
            $('#' + type).val(2);
        }
    }

    function mouseBlue(obj, val) {
        if (val == 1) {
            $(obj).css('backgroundColor', '#C0DDF3');
        }
        else {
            $(obj).css('backgroundColor', 'transparent');
        }
    }

    function radio_off(obj_name, val) {
        $('#par_' + obj_name + '_0').attr('src', 'images/radio_off_m.png');
        $('#par_' + obj_name + '_1').attr('src', 'images/radio_off_m.png');
        $('#par_' + obj_name + '_2').attr('src', 'images/radio_off_m.png');

        $('#' + obj_name + '_0').attr('checked', false);
        $('#' + obj_name + '_1').attr('checked', false);
        $('#' + obj_name + '_2').attr('checked', false);

        $('#par_' + obj_name + '_' + val).attr('src', 'images/radio_on_m.png');
        $('#' + obj_name + '_' + val).attr('checked', true);
    }

    function set_all_conf(val) {
        $('[id^="conf0_"]').each(function () {
            this.checked = val == 0 ? true : false;
        });

        $('[id^="conf1_"]').each(function () {
          if (this.name.indexOf('conf_reports') !== -1) {
            let element = $('[name="' + this.name + '"][value="2"]')[0];
            document.getElementById(element.id).checked = true;
          } else {
            this.checked = val == 1 ? true : false;
          }
        });

        $('[id^="conf3_"]').each(function () {
          if (this.name.indexOf('conf_reports') !== -1) {
            let element = $('[name="' + this.name + '"][value="2"]')[0];
            document.getElementById(element.id).checked = true;
          } else {
            this.checked = val == 3 ? true : false;
          }
        });

        $('[id^="conf2_"]').each(function () {
          if (this.name.indexOf('conf_reports') !== -1 && (val == 1 || val == 3)) {} else {
            this.checked = val == 2 ? true : false;
          }
        });
    }

    function set_all_rights(val) {
        $('[id^="set_rights0_cat"]').each(function () {
            this.checked = val == 0 ? true : false;
        });
        $('[id^="set_rights1_cat"]').each(function () {
            this.checked = false;
        });
        $('[id^="set_rights2_cat"]').each(function () {
            this.checked = val == 0 ? false : true;
        });
        
        $('[id^="set_rights0_table"], [id^="set_rights0_report"], [id^="set_rights0_calendar"]').each(function () {
            this.checked = val == 0 ? true : false;
        });
        $('[id^="set_rights1_table"], [id^="set_rights1_report"], [id^="set_rights1_calendar"]').each(function () {
            this.checked = val == 1 ? true : false;
            this.disabled = val == 0 ? true : false;
        });
        $('[id^="set_rights2_table"], [id^="set_rights2_report"], [id^="set_rights2_calendar"]').each(function () {
            this.checked = val == 2 ? true : false;
            this.disabled = val == 0 ? true : false;
        });
    }

    function set_cat_rights(cat_id, val) {
        if (val == 0) {
            $('.set_cat_rights0'+cat_id).each(function () {
                this.checked = true;
            });
            $('.set_cat_rights1'+cat_id).each(function () {
                this.checked = false;
            });
        }
        $('.set_cat_rights1'+cat_id).each(function () {
            this.disabled = val == 0 ? true : false;
        });
    }

    $(function () {
        $('input[name=are_fields_editable]').click(
                function (e) {
                    $('.editable_fields_list_tr').toggle();
                });

        if ($('input[name=are_fields_editable]').is(':checked')) {
            $('.editable_fields_list_tr').show();
        }
    });

</script>
{if $group_id!=777 && $edit_group==1}
    <form method="POST" id="send_form" class="access-group-settings--form">
        <input type="hidden" name="save_group_settings" value="1"/>
        <input type="hidden" name="set_parent" id="set_parent" value="0"/>
        <input type="hidden" name="csrf" value="{$csrf}"/>
        <input type="hidden" name="subadmin_add_rule" id="subadmin_add_rule" value="0"/>
        <table border=0 cellpadding=3>
            {if $message}
                <tr>
                    <td align=center colspan=2 style="color:green;padding-bottom:15px">{$message}</td>
                </tr>
            {/if}
            <tr>
                <td align=right>{$lang.Group_name}<span class="help_bt" h_id="acc_Group_name"></span>:</td>
                <td><input class="form-control" name="group_name" value="{$group.name}" size=30/></td>
            </tr>
            <tr>
                <td align=right>{$lang.Description}<span class="help_bt" h_id="acc_Group_desc"></span>:</td>
                <td><textarea class="form-control" name="group_help">{$group.help}</textarea></td>
            </tr>
            {if $group_id!=1}
                <tr>
                    <td align=right>{$lang.Subordinated_to_group}<span class="help_bt" h_id="acc_Subordinated_to_group"></span>:</td>
                    <td><select class="form-control" name="pid" id="pid" onchange="change_pid(this)">
                            <option value="0">{$lang.no}</option>
                            {$in_group_select}
                        </select></td>
                </tr>
            {/if}
            <tr>
                <td align="right">{$lang.default_page}<span class="help_bt" h_id="default_page"></span>:
                </td>
                <td colspan=2 width="50%">
                    <select class="form-control" name="default_page">
                        <option value=" ">{$lang.absent_page}</option>
                        {foreach from=$dp_list item=dp_gr key=dp_gr_key}
                            <optgroup label="{$dp_gr.name}">
                                {foreach from=$dp_gr.contents item=dp_item key=dp_item_key}
                                    <option value="{$dp_item.id}"
                                            {if $dp_item.id==$group.default_page}selected{/if}>{$dp_item.name}
                                        {if $dp_item.type=='r'} {$lang.report_page} {/if}
                                        {if $dp_item.type=='c'} {$lang.calendar_page} {/if}
                                    </option>
                                {/foreach}
                            </optgroup>
                        {/foreach}
                    </select>
                </td>
            </tr>

            <tr>
                <td align="right">{$lang.system_lang}<span class='help_bt' h_id='system_lang'></span>:</td>
                <td colspan=2 width="50%">
                    <select class="form-control" name="system_lang">
                        {foreach from=$langs_list item=lang_name key=sname}
                            <option value="{$sname}" {if $sname==$group.lang}selected{/if}>{$lang_name}</option>
                        {/foreach}
                    </select>
                </td>
            </tr>
            <tr>
                <td align="right">{$lang.time_zone}<span class='help_bt' h_id='time_zone'></span>:</td>
                <td colspan=2 width="50%">
                    <select class="form-control" name="time_zone">
                        {foreach from=$tz_list item=tz_name key=time_zone}
                            <option value="{$time_zone}"
                                    {if $time_zone==$group.time_zone}selected{/if}>{$tz_name}</option>
                        {/foreach}
                    </select>
                </td>
            </tr>
            <tr>
                <td align="right">{$lang.Mass_actions_table}<span class='help_bt' h_id='mass_edit_mode'></span>:</td>
                <td colspan=2 width="310">
                    <input type="radio" name="mass_edit_mode" value="1" {if $group.mass_edit_mode == 1}checked{/if}> {$lang.mass_edit_option_1}<br>
                    <input type="radio" name="mass_edit_mode" value="2" {if $group.mass_edit_mode == 2}checked{/if}> {$lang.mass_edit_option_2}
                </td>
            </tr>

            <tr>
                <td align=right>{$lang.Are_fields_editable}<span class="help_bt" h_id="Are_fields_editable"></span>:</td>
                <td class="apply-for-all-groups">
                    <input type="checkbox" name="are_fields_editable" class="apply-for-all-groups--input" {if $group.editable_fields}checked{/if}>
                    <input class="btn btn-default btn-sm" type="submit" onclick="return confirm('{$lang.confirm_apply_editable_to_all}');"
                           name="apply_editable_to_all" value="{$lang.apply_editable_to_all}">
                </td>
            </tr>
            <tr class="editable_fields_list_tr" style="display:none;">
                <td align="center" colspan="2">
                    <hr/>
                </td>
            </tr>
            <tr class="editable_fields_list_tr" style="display:none;">
                <td align="center" colspan="2"><b> {$lang.Editable_fields_list} </b></td>
            </tr>
            <tr class="editable_fields_list_tr" style="display:none">
                <td align="right" style="width:183px;">{$lang.Login}:</td>
                <td><input type="checkbox" name="editable[login]"
                           {if strpos($group.editable_fields, 'login')}checked{/if}></td>
            </tr>
            <tr class="editable_fields_list_tr" style="display:none;">
                <td align="right" style="width:183px;">{$lang.Password}:</td>
                <td><input type="checkbox" name="editable[password]"
                           {if strpos($group.editable_fields, 'password')}checked{/if}></td>
            </tr>
            <tr class="editable_fields_list_tr" style="display:none;">
                <td align="right" style="width:183px;">{$lang.Fillot}:</td>
                <td><input type="checkbox" name="editable[fio]" 
                           {if strpos($group.editable_fields, 'fio')}checked{/if}></td>
            </tr>
            <tr class="editable_fields_list_tr" style="display:none;">
                <td align="right" style="width:183px;">{$lang.e_mail}:</td>
                <td><input type="checkbox" name="editable[e_mail]"
                           {if strpos($group.editable_fields, 'e_mail')}checked{/if}></td>
            </tr>
            <tr class="editable_fields_list_tr" style="display:none;">
                <td align="right" style="width:183px;">{$lang.Phone}:</td>
                <td><input type="checkbox" name="editable[phone]"
                           {if strpos($group.editable_fields, 'phone')}checked{/if}></td>
            </tr>
            {if $all_modules.telegram_notify}
            <tr class="editable_fields_list_tr" style="display:none;">
                <td align="right" style="width:183px;">{$lang.Telegram_notify}:</td>
                <td><input type="checkbox" name="editable[telegram]"
                           {if strpos($group.editable_fields, 'telegram')}checked{/if}></td>
            </tr>
            {/if}
            <tr class="editable_fields_list_tr" style="display:none;">
                <td align="right" style="width:183px;">{$lang.system_lang}:</td>
                <td><input type="checkbox" name="editable[lang]"
                           {if strpos($group.editable_fields, 'lang')}checked{/if}></td>
            </tr>
            <tr class="editable_fields_list_tr" style="display:none;">
                <td align="right" style="width:183px;">{$lang.time_zone}:</td>
                <td><input type="checkbox" name="editable[time_zone]"
                           {if strpos($group.editable_fields, 'time_zone')}checked{/if}></td>
            </tr>
            <!--tr class="editable_fields_list_tr" style="display:none;">
                <td align="right" style="width:183px;">{$lang.window_scroll}:</td>
                <td><input type="checkbox" name="editable[window_scroll]"
                           {if strpos($group.editable_fields, 'window_scroll')}checked{/if}></td>
            </tr-->
            <!--tr class="editable_fields_list_tr" style="display:none;">
                <td align="right" style="width:183px;">{$lang.api_on}:</td>
                <td><input type="checkbox" name="editable[api_on]"
                           {if strpos($group.editable_fields, 'api_on')}checked{/if}></td>
            </tr-->
            <tr class="editable_fields_list_tr" style="display:none;">
                <td align="right" style="width:183px;">{$lang.sound_notify}:</td>
                <td><input type="checkbox" name="editable[sound_on]"
                           {if strpos($group.editable_fields, 'sound_on')}checked{/if}></td>
            </tr>
            <tr class="editable_fields_list_tr" style="display:none;">
                <td align="right" style="width:183px;">{$lang.cut_notify_text}:</td>
                <td><input type="checkbox" name="editable[cut_notify_text]"
                           {if strpos($group.editable_fields, 'cut_notify_text')}checked{/if}></td>
            </tr>
            <tr class="editable_fields_list_tr" style="display:none;">
                <td align="right" style="width:183px;">{$lang.display_notification_on}:</td>
                <td><input type="checkbox" name="editable[display_notification_on]"
                           {if strpos($group.editable_fields, 'display_notification_on')}checked{/if}></td>
            </tr>
            <tr class="editable_fields_list_tr" style="display:none;">
                <td align="right" style="width:183px;">{$lang.user_avatar}:</td>
                <td><input type="checkbox" name="editable[user_avatar]"
                           {if strpos($group.editable_fields, 'user_avatar')}checked{/if}></td>
            </tr>
            <tr class="editable_fields_list_tr" style="display:none;">
                <td align="right" style="width:183px;">{$lang.default_page}:</td>
                <td><input type="checkbox" name="editable[default_page]"
                           {if strpos($group.editable_fields, 'default_page')}checked{/if}></td>
            </tr>
            <tr class="editable_fields_list_tr" style="display:none;">
                <td align="right" style="width:183px;">{$lang.default_channel}:</td>
                <td><input type="checkbox" name="editable[default_channel]"
                           {if strpos($group.editable_fields, 'default_channel')}checked{/if}></td>
            </tr>
            <tr class="editable_fields_list_tr" style="display:none;">
                <td align="center" colspan="2">
                    <hr/>
                </td>
            </tr>

            <tr>
                <td align="right">{$lang.Scheduled_access}<span class='help_bt' h_id='scheduled_access'></span>:</td>
                <td colspan=2 width="310">
                    <table><tr>
                    <td style="padding:0"><input type="checkbox" name="scheduled_access_on" {if $group.scheduled_access}checked{/if} onclick="$('#scheduled_access').toggle()"></td>
                    <td><table id="scheduled_access" {if !$group.scheduled_access}style="display:none"{/if}>
                        <tr><td align=right>{$lang.Monday}:</td><td><input class="form-control" name="scheduled_access[1]" value="{$scheduled_access[1]}" style="width: 70px"></td></tr>
                        <tr><td align=right>{$lang.Tuesday}:</td><td><input class="form-control" name="scheduled_access[2]" value="{$scheduled_access[2]}" style="width: 70px"></td></tr>
                        <tr><td align=right>{$lang.Wednesday}:</td><td><input class="form-control" name="scheduled_access[3]" value="{$scheduled_access[3]}" style="width: 70px"></td></tr>
                        <tr><td align=right>{$lang.Thursday}:</td><td><input class="form-control" name="scheduled_access[4]" value="{$scheduled_access[4]}" style="width: 70px"></td></tr>
                        <tr><td align=right>{$lang.Friday}:</td><td><input class="form-control" name="scheduled_access[5]" value="{$scheduled_access[5]}" style="width: 70px"></td></tr>
                        <tr><td align=right>{$lang.Saturday}:</td><td><input class="form-control" name="scheduled_access[6]" value="{$scheduled_access[6]}" style="width: 70px"></td></tr>
                        <tr><td align=right>{$lang.Sunday}:</td><td><input class="form-control" name="scheduled_access[0]" value="{$scheduled_access[0]}" style="width: 70px"></td></tr>
                    </table></td>
                    </tr></table>
                </td>
            </tr>
            {if $group_id!=1}
                <tr>
                    <td align=right>{$lang.Sub_admin}<span class='help_bt' h_id='Sub_admin'></span>:</td>
                    <td><input type="checkbox" name="sub_admin" id="sub_admin_checkbox"
                               {if $group.sub_admin}checked{/if}
                               onclick="open_subadmin();"/></td>
                </tr>
                {include file="./acc_subadmin.tpl"}
            {/if}
        </table>
        <br/>
        <input class="btn btn-default btn-sm" type="submit" value="{$lang.Save}" onclick="send_submit();return false;"/>
        {*<input type="submit" id="subadmin_add_rule_button" value="{$lang.Add_rule}" onclick="send_submit(1);return false;" {if !$group.sub_admin}style="display:none;"{/if}/>*}
    </form>
    <br/>
{else}
    <br/>
    <table border=0 cellpadding=3>
        {foreach from=$cats item=one_cat}
            {if $one_cat.display_name && ($user.group_id == 1 || $user.sub_admin_rights.set_rights.cats[$one_cat.id])}
                <tr>
                    <td>{if $one_cat.blocked}<img src="images/lock.svg" style="width:13px">{/if}</td>
                    <td>
                        <div style="width:19px;float:left;height:13px;margin-right:5px;">
                            <img src='images/acc_{if $one_cat.access==1}allow{elseif $one_cat.access==2}invisibe{else}close{/if}.png'
                                 {if !$one_cat.blocked}onclick='send_cat_acc({$group_id}, {$one_cat.id}, this);change_eye(this);'{/if}
                                 style='text-decoration:none;background-color:transparent;margin-top:2px;{if !$one_cat.blocked}cursor:pointer{/if}'>
                        </div>
                        {if $user.group_id == 1 || $user.sub_admin_rights.set_rights.cats[$one_cat.id] == 2}
                            <a href="edit_group.php?group={$group_id}&cat={$one_cat.id}">{$one_cat.display_name}</a>
                        {else}
                            {$one_cat.display_name}
                        {/if}
                    </td>
                </tr>
            {/if}
            {foreachelse}
            <tr>
                <td>{$lang.no_categories}</td>
            </tr>
        {/foreach}
    </table>
    <br/>
{/if}
<a href="edit_group.php">{$lang.Return_to_group}</a>
<br/><br/>
