{include file="./index_top.tpl"}
<script type="text/javascript">
    function table_dubl(table_name, old_name) {
        var res;
        if (old_name != table_name) {
            $.ajax({
                url: "edit_table.php",
                data: "name_table="+table_name,
                async: false,
                success: function(data) {
                    res = data;
                }
            })
        } else {
            res = 0;
        }
        return res;
    }
    function copy_table(copy_name) {
        var copy_name_ok = false;
        do {
            copy_name = prompt("{$lang.Copy_as}", copy_name);
            // Проверяем Нажали "ОК" или "отмена"
            if (copy_name!=null) {
                if (!copy_name) {
                    alert('{$lang.must_enter_name_table}');
                } else if (copy_name!=null) {
                    if (table_dubl(copy_name)==0) {
                        copy_name_ok = true;
                    } else {
                        alert('{$lang.table_name_already_exists}');
                    }
                }
            } else {
                copy_name_ok = true; // Нажали кнопку отмены
            }
        }
        while (copy_name_ok===false);
        return copy_name;
    }
</script>
<div align=center>
    <br>
    {if $table_id}
        {$lang.Edit_sett_table} "{$name_table}"
    {elseif $calendar_id}
        Редактирование настроек календаря &quot;{$name_calendar}&quot;
    {else}
        {$lang.Tables_and_reports}{if $name_cat} {$lang.of_category} "{$name_cat}"{/if}<span class='help_bt' h_id='Tables'></span>
    {/if}
    <br><br>
    {include file="./conf_nav.tpl"}
    <p style="font-size:14px;">
        {if $archive}<a style="font-size:14px;" href="edit_tabrep.php?cat={$cat_id}">{/if}
            {$lang.active}
        {if $archive}</a>{/if}
        /
        {if !$archive}
            {if $has_archive}
                <a style="font-size:14px;" href="edit_tabrep.php?cat={$cat_id}&archive">
            {else}
                <span style="color:gray" title="{$lang.Empty_archive}">
            {/if}
        {/if}
            {$lang.archive}
        {if !$archive}{if $has_archive}</a>{else}</span>{/if}{/if}
    </p>
    {if $search}
    <form>
        <div style="margin-bottom:15px">
            <input class="form-control form-control-300" type="text" name="search" id="search" value="{$search}">
            <input class="btn btn-default btn-sm" type="submit" value="{$lang.Search}" style="margin-top:-3px"
                   onclick="if (document.getElementById('search').value == '') return false;">
            <input class="btn btn-default btn-sm" type="button" value="{$lang.Reset}" style="margin-top:-3px"
                   onclick="if (document.getElementById('search').value == '') return false; else location.href = 'edit_cat.php';">
        </div>
    </form>
    {/if}
    {if $archive}
    <table>
        {foreach from=$arc_tables item=one_tabrep}
        <tr>
            <td>
                {if $one_tabrep.type=="table"}
                    <a class='href_post'
                        href="edit_table.php?table={$one_tabrep.id}&restore"><img
                                alt="{$lang.restore}" title="{$lang.restore_from_archive}"
                                src="images/b_restore.png" border=0></a>
                {elseif $one_tabrep.type=="report"}
                    <a class='href_post'
                        href="edit_report.php?cat={$cat_id}&report={$one_tabrep.id}&sel=restore"><img
                                alt="{$lang.restore}" title="{$lang.restore_from_archive}"
                                src="images/b_restore.png" border=0></a>
                {elseif $one_tabrep.type=="calendar"}
                    <a class='href_post'
                        href="edit_calendar.php?cat={$cat_id}&calendar={$one_tabrep.id}&sel=restore"><img
                                alt="{$lang.restore}" title="{$lang.restore_from_archive}"
                                src="images/b_restore.png" border=0></a>
                {/if}
            </td>
            <td>
                {if $one_tabrep.type == "report" && ($user.group_id==1 || ($user.sub_admin_rights.reports.{$one_tabrep.id} == 2 && $user.sub_admin_rights.allow_del) || $one_tabrep.owner_id == $user.group_id) && !$one_tabrep.blocked}
                    <a class='href_post' href="edit_report.php?cat={$cat_id}&report={$one_tabrep.id}&delete"
                       onClick='return href_post=jconfirm("{$lang.alert_delete_report}", this);'><img
                       alt="{$lang.delete}" title="{$lang.Delete_report}" src="images/b_drop.png" border=0></a>
                {elseif $one_tabrep.type == "calendar" && ($user.group_id==1 || ($user.sub_admin_rights.calendars.{$one_tabrep.id} == 2 && $user.sub_admin_rights.allow_del) || $one_tabrep.owner_id == $user.group_id) and !$one_tabrep.blocked}
                    <a class='href_post'
                       href="edit_calendar.php?cat={$cat_id}&calendar={$one_tabrep.id}&delete"
                       onClick='return href_post=jconfirm("{$lang.alert_delete_calendar}", this);'><img
                       alt="{$lang.delete}" title="{$lang.Delete_calendar}" src="images/b_drop.png" border=0></a>
                {/if}
            </td>
            <td>
                {if $one_tabrep.type == "report" && ($user.group_id == 1 || $user.sub_admin_rights.reports.{$one_tabrep.id} > 0) && !$one_tabrep.blocked}
                    <a href="edit_report.php?report={$one_tabrep.id}"><b>{$one_tabrep.name}</b></a>
                {elseif $one_tabrep.type == "calendar" && ($user.group_id == 1 || $user.sub_admin_rights.calendars.{$one_tabrep.id} > 0) && !$one_tabrep.blocked}
                    <a href="edit_calendar.php?calendar={$one_tabrep.id}"><b>{$one_tabrep.name}</b></a>
                {else}
                    <b>{$one_tabrep.name}</b>
                {/if}
            </td>
        </tr>
        {foreachelse}
        <tr>
            <td>{$lang.no_tables}</td>
        </tr>
        {/foreach}
    </table>
    {else}
    <form action="edit_tabrep.php?cat={$cat_id}" method="post" {if $table_id}onsubmit="return false;"{/if}>
        <input type=hidden name=csrf value='{$csrf}'>
        <table class='sortableTable'>
            {if $error}
                <tr class="noDrug"><td style="color:red" colspan="8" align="center">{$error}<br><br></td></tr>
            {/if}
            {foreach from=$all_tabrep item=one_tabrep}
                {if $config.display_protected_elements || $one_tabrep.blocked != 2 || $one_tabrep.type == "table"}
                <tr>
                    {if $one_tabrep.type=="table"}
                        {if !$search}
                        <td><span class="draggableImg">&nbsp;</span></td>
                        <td>{if ($user.group_id==1 || $user.sub_admin_rights.tables.{$one_tabrep.id}==2 || $one_tabrep.owner_id==$user.group_id) and !$one_tabrep.blocked}
                            <a class='href_post'
                                href="edit_table.php?table={$one_tabrep.id}&archive"><img
                                        alt="{$lang.into_archive}" title="{$lang.send_in_archive}"
                                        src="images/b_archive.png" border=0></a>{/if}
                        </td>
                        <td>{if ($user.group_id==1 || $user.sub_admin_rights.tables.{$one_tabrep.id}==2 || $user.sub_admin_rights.tables.{$one_tabrep.id}==3 || $one_tabrep.owner_id==$user.group_id) and $one_tabrep.blocked != 2}
                                <a href="edit_table.php?table={$one_tabrep.id}{$back_url_prm}"><img alt="{$lang.edit}"
                                                                                                    title="{$lang.Edit_table}"
                                                                                                    src="images/b_edit.png"
                                                                                                    border=0></a>{/if}
                        </td>
                        <td>{if ($user.group_id==1 || ($user.sub_admin_rights.tables.{$one_tabrep.id}==2 && $user.sub_admin_rights.allow_del) || $one_tabrep.owner_id==$user.group_id) and !$one_tabrep.blocked}
                                <a class='href_post' href="edit_table.php?table={$one_tabrep.id}&delete"
                                   onClick='return href_post=jconfirm("{$lang.alert_delete_table}", this);'><img
                                        alt="{$lang.delete}" title="{$lang.Delete_table}" src="images/b_drop.png"
                                        border=0></a>{/if}</td>
                        <td>{if ($user.group_id==1 || $user.sub_admin_rights.cats.{$cat_info.id}>0 &&  $user.sub_admin_rights.tables[$one_tabrep.id] == 2) and $one_tabrep.blocked != 2 }
                                <a class='href_post' href="edit_table.php?table={$one_tabrep.id}&amp;copy"
                                    onClick='if (copy_name=copy_table("{$one_tabrep.copy_name}")) {ldelim}this.href=this.href+"="+copy_name; href_post=true;{rdelim} else href_post=false;'>
                                <img alt="{$lang.copy}" title="{$lang.Copy_table}" src="images/b_copy.png" border=0>
                                </a>{/if}</td>
                        <td><input class="form-control form-control-20" type=text size=1 name=num[{$one_tabrep.type}_{$one_tabrep.id}]
                                   value={$one_tabrep.num} {if $table_id}readonly{/if}></td>
                        {/if}
                        <td class="cb_tabrep_cell">
                          {if $user.group_id==1 || $user.sub_admin_rights.tables.{$one_tabrep.id}>0}
                              <a {if $one_tabrep.blocked == 3}class="gray_link"{/if}
                                href="edit_field.php?table={$one_tabrep.id}{$back_url_prm}"
                                {if $one_tabrep.view_sql}style="color:red"{/if}
                                {if $one_tabrep.blocked == 3}onclick="jalert('{$lang.Blocked_table_alert_1} &quot;{$one_tabrep.addon}&quot;<br>{$lang.Blocked_table_alert_2}'); return false;"{/if}>
                                  <b>{$one_tabrep.name}</b>
                              </a>
                          {else}
                              <b style="color:gray;">{$one_tabrep.name}</b>
                          {/if}
                        </td>
                        <td>
                            <span style="color:gray">
                                <i>{$lang.Table|mb_strtolower}</i>
                            </span>
                        </td>
                    {else if $one_tabrep.type=="report"}
                        {if !$search}
                        <td><span class="draggableImg">&nbsp;</span></td>
                        <td>{if ($user.group_id==1 || $user.sub_admin_rights.reports.{$one_tabrep.id}==2 || $one_tabrep.owner_id==$user.group_id) and !$one_tabrep.blocked}
                            <a class='href_post'
                                href="edit_report.php?cat={$cat_id}&report={$one_tabrep.id}&sel=archive"><img
                                        alt="{$lang.into_archive}" title="{$lang.send_in_archive}"
                                        src="images/b_archive.png" border=0></a>{/if}
                        </td>
                        <td>{if ($user.group_id==1 || $user.sub_admin_rights.reports.{$one_tabrep.id}==2 || ($one_tabrep.owner_id==$user.group_id && $user.sub_admin_rights.reports.{$one_tabrep.id}==1)) and $one_tabrep.blocked != 2}
                                <a href="edit_report.php?report={$one_tabrep.id}"><img alt="{$lang.edit}"
                                                                                           title="{$lang.edit_report}"
                                                                                           src="images/b_edit.png"
                                                                                           border=0></a>{/if}</td>
                        <td>{if ($user.group_id==1 || ($user.sub_admin_rights.reports.{$one_tabrep.id}==2 && $user.sub_admin_rights.allow_del) || $one_tabrep.owner_id==$user.group_id) and !$one_tabrep.blocked}
                                <a class='href_post' href="edit_report.php?cat={$cat_id}&report={$one_tabrep.id}&delete"
                                   onClick='return href_post=jconfirm("{$lang.alert_delete_report}", this);'><img
                                        alt="{$lang.delete}" title="{$lang.Delete_report}" src="images/b_drop.png"
                                        border=0></a>{/if}</td>
                        <td>{if ($user.group_id==1 || $user.sub_admin_rights.cats.{$cat_info.id}>0) && $one_tabrep.blocked != 2 && ($all_modules.pro_tariff || !$one_tabrep.expert_mode)}<a class='href_post'
                                                                                                     href="edit_report.php?cat={$cat_id}&report={$one_tabrep.id}&amp;copy"
                                                                                                     onClick="if (copy_name=prompt('{$lang.Copy_as}','{$one_tabrep.name} {$lang['(copy)']}')) { this.href=this.href+'='+copy_name; href_post=true; } else { href_post=false; }">
                                <img alt="{$lang.copy}" title="{$lang.Copy_report}" src="images/b_copy.png" border=0>
                                </a>{/if}</td>
                        <td><input class="form-control form-control-20" type=text size=1 name=num[{$one_tabrep.type}_{$one_tabrep.id}]
                                   value={$one_tabrep.num}></td>
                        {/if}
                        <td class="cb_tabrep_cell">
                            {if ($user.group_id==1 || $user.sub_admin_rights.reports.{$one_tabrep.id}>0) && $one_tabrep.blocked != 2}
                                <a {if $one_tabrep.blocked}class="gray_link"{/if}
                                href="edit_report.php?report={$one_tabrep.id}"
                                {if $one_tabrep.blocked == 3}onclick="jalert('{$lang.Blocked_report_alert_1} &quot;{$one_tabrep.addon}&quot;<br>{$lang.Blocked_report_alert_2}'); return false;"{/if}>
                                    <b>{$one_tabrep.name}</b>
                                </a>
                            {else}
                                <b style="color:gray;">{$one_tabrep.name}</b>
                            {/if}
                        </td>
                        <td><span style="color:gray"><i>{$lang.report}</i></span></td>
                    {else}
                        {if !$search}
                        <td><span class="draggableImg">&nbsp;</span></td>
                        <td>{if ($user.group_id==1 || $user.sub_admin_rights.calendars.{$one_tabrep.id}==2 || $one_tabrep.owner_id==$user.group_id) and !$one_tabrep.blocked}
                            <a class='href_post'
                                href="edit_calendar.php?cat={$cat_id}&calendar={$one_tabrep.id}&sel=archive"><img
                                        alt="{$lang.into_archive}" title="{$lang.send_in_archive}"
                                        src="images/b_archive.png" border=0></a>{/if}
                        </td>
                        <td>{if ($user.group_id==1 || $user.sub_admin_rights.calendars.{$one_tabrep.id}==2 || ($one_tabrep.owner_id==$user.group_id && $user.sub_admin_rights.calendars.{$one_tabrep.id}==1)) and $one_tabrep.blocked != 2}
                                <a href="edit_calendar.php?calendar={$one_tabrep.id}"><img alt="{$lang.edit}"
                                                                                           title="{$lang.edit_calendar}"
                                                                                           src="images/b_edit.png"
                                                                                           border=0></a>{/if}</td>
                        <td>{if ($user.group_id==1 || ($user.sub_admin_rights.calendars.{$one_tabrep.id}==2 && $user.sub_admin_rights.allow_del) || $one_tabrep.owner_id==$user.group_id) and !$one_tabrep.blocked}
                                <a class='href_post'
                                   href="edit_calendar.php?cat={$cat_id}&calendar={$one_tabrep.id}&delete"
                                   onClick='return href_post=jconfirm("{$lang.alert_delete_calendar}", this);'><img
                                        alt="{$lang.delete}" title="{$lang.Delete_calendar}" src="images/b_drop.png"
                                        border=0></a>{/if}</td>
                        <td>{if ($user.group_id==1 || $user.sub_admin_rights.cats.{$cat_info.id}>0 && $user.sub_admin_rights.calendars[$one_tabrep.id] ==2) and $one_tabrep.blocked != 2}
                                <a class='href_post'
                                   href="edit_calendar.php?cat={$cat_id}&calendar={$one_tabrep.id}&amp;copy"
                                   onClick="if (copy_name=prompt('{$lang.Copy_as}','{$one_tabrep.name} {$lang['(copy)']}')) { this.href=this.href+'='+copy_name; href_post=true; } else { href_post=false; }"
                                   >
                                <img alt="{$lang.copy}" title="{$lang.Copy_calendar}" src="images/b_copy.png" border=0>
                                </a>{/if}</td>
                        <td><input class="form-control form-control-20" type=text size=1 name=num[{$one_tabrep.type}_{$one_tabrep.id}]
                                   value={$one_tabrep.num}></td>
                        {/if}
                        <td class="cb_tabrep_cell">
                            {if ($user.group_id==1 || $user.sub_admin_rights.calendars.{$one_tabrep.id}>0) && $one_tabrep.blocked != 2}
                                <a {if $one_tabrep.blocked}class="gray_link"{/if}
                                href="edit_calendar.php?calendar={$one_tabrep.id}&calendar_events"
                                {if $one_tabrep.blocked == 3}onclick="jalert('{$lang.Blocked_calend_alert_1} &quot;{$one_tabrep.addon}&quot;<br>{$lang.Blocked_calend_alert_2}'); return false;"{/if}>
                                    <b>{$one_tabrep.name}</b>
                                </a>
                            {else}
                                <b style="color:gray;">{$one_tabrep.name}</b>
                            {/if}
                        </td>
                        <td><span style="color:gray"><i>{$lang.calendar}</i></span></td>
                    {/if}
                    {if $one_tabrep.protected}<td><img src="images/lock.svg" style="width:13px" title="{$one_tabrep.protect_comment}"></td>{/if}
                </tr>
                {/if}
                {foreachelse}
                <tr>
                    <td>{$lang.no_tables}</td>
                </tr>
            {/foreach}
        </table>
        {foreach from=$sort_only item=one_tabrep}
            <input type=hidden size=1 name=num[{$one_tabrep.type}_{$one_tabrep.id}] value={$one_tabrep.num}>
        {/foreach}
        {if !$table_id and !$calendar_id and !$search}
            <br>
            {*if $all_tabrep}<input id="tables__refresh_table" class="btn btn-default btn-sm" type=submit value="{$lang.Refresh_range}">&nbsp;{/if*}
            {if $user.group_id==1 || $user.sub_admin_rights.cats.{$cat_info.id}>0}
                <input id="tables__add_table" class="btn btn-default btn-sm" type=button value="{$lang.Add_table}"
                       onClick="location.href='edit_table.php?cat={$cat_id}&table=-1';">
                <input id="tables__report_table" class="btn btn-default btn-sm" type=button value="{$lang.Add_report}"
                       onClick="location.href='edit_report.php?cat={$cat_id}&report=-1';">
                <input id="tables__calendar_table" class="btn btn-default btn-sm" type=button value="{$lang.Add_calendar}"
                       onClick="location.href='edit_calendar.php?cat={$cat_id}&calendar=-1';">
            {/if}
        {/if}
    </form>
    {/if}
    <br>
    <a href="edit_cat.php">{$lang.return_to_cat_list}</a><br><br>
</div>

<script src="themes/clientbase/js/cut_title_field.js"></script>
