{include file="./index_top.tpl"}
<div align=center>
    <br>{$lang.Statistics}<span class='help_bt' h_id='Statistics'></span><br>
    <br><a href="edit_stat.php">{$lang.Load}</a> / {$lang.Tables}<br><br>
    {if $create_msg}
        <div style="text-align: center">{$lang.Create_backup} &quot;{$backup_filename}&quot;<br/><br/></div>
        <div id="backup_status" style="font-size: 13px; text-align: center">
            <img src='images/indicator.gif' alt='' style='vertical-align: middle'/> {$create_msg}...<br/><br/>
        </div>
        <div id="backup_process" style="width: 200px; text-align: left; border: 1px solid black; margin: 3px auto 40px">
            <div id="backup_bar" style="height: 18px; width: {$part_done}%; background: #85C51F"></div>
            <div id="backup_progress"
                 style="position: absolute; margin-top: -18px; text-align: center; width: 200px; font-size: 13px;">{$part_done}
                %
            </div>
        </div>
        <script type="text/javascript">
            var backupError = true;
            $(document).ready(function () {
                if (backupError) {
                    document.getElementById("backup_process").style.display = "none";
                    document.getElementById("backup_status").style.display = "none";
                }
            });
        </script>
    {else}
        <script type="text/javascript" src="mybic.js?{$revision}"></script>
        <script language="JavaScript" type="text/javascript">
            function display_files(table_id) {
                if (document.getElementById('files' + table_id).style.display == 'none')
                    document.getElementById('files' + table_id).style.display = 'block';
                else
                    document.getElementById('files' + table_id).style.display = 'none';
            }

            var load_t_id = 0;

            var file_tables = [ {foreach from=$all_cats item=all_tables}{foreach from=$all_tables item=one_table}{if $one_table.file_fields} {$one_table.id}, 0, {$one_table.id}, 1,  {$one_table.id}, 2, {/if}{/foreach}{/foreach} ]

            var ajax_update = new XMLHTTP("edit_stat.php");
            var total_files_cnt = [0, 0, 0];
            var total_files_size = [0, 0, 0];

            // Загрузить статистику по файлам
            function load_file_stat() {
                var t_id = file_tables[load_t_id];
                var t_st = file_tables[load_t_id + 1];
                document.getElementById("files" + t_id + "_" + t_st).innerHTML = "<img src='images/indicator.gif'>";
                ajax_update.format = 0;
                ajax_update.abort_timeout = 150000;
                ajax_update.method = "GET";
                ajax_update.call("calc_file_table=" + t_id + "&calc_file_status=" + t_st, load_file_stat_resp);
            }

            // Обновить результат
            function load_file_stat_resp(resp) {
                var t_id = file_tables[load_t_id];
                var t_st = file_tables[load_t_id + 1];
                var res_arr = resp.toString().split("|");
                var files_count = res_arr[0];
                var files_size = res_arr[1];

                if (parseInt(files_count) > 0) {
                    if (files_size < 100) {
                        str_files_size = files_size + ' kB';
                    } else if (files_size < 1000 * 1024) {
                        str_files_size = (files_size / 1024).toFixed(1) + ' MB';
                    } else {
                        str_files_size = (files_size / 1024 / 1024).toFixed(3) + ' GB';
                    }
                    document.getElementById("files" + t_id + "_" + t_st).innerHTML = files_count + " {$lang.files} (" + str_files_size + ")"

                    total_files_cnt[t_st] += parseInt(files_count);
                    total_files_size[t_st] += parseFloat(files_size);

                    if (total_files_size[t_st] < 100) {
                        str_files_size = total_files_size[t_st] + ' kB';
                    } else if (total_files_size[t_st] < 1000 * 1024) {
                        str_files_size = (total_files_size[t_st] / 1024).toFixed(1) + ' MB';
                    } else {
                        str_files_size = (total_files_size[t_st] / 1024 / 1024).toFixed(3) + ' GB';
                    }
                    document.getElementById("total_files_span_" + t_st).innerHTML = "<br>" + total_files_cnt[t_st] + " {$lang.files} (" + str_files_size + ")";
                }
                else {
                    document.getElementById("files" + t_id + "_" + t_st).innerHTML = '';
                }
                load_t_id += 2;
                t_id = file_tables[load_t_id];
                t_st = file_tables[load_t_id + 1];
                if (document.getElementById("files" + t_id + "_" + t_st)) load_file_stat();
            }

            $().ready(function () {
                // Подгружаем статиситику по файлам
                load_file_stat();
            });

        </script>
        <table border=0 cellpadding=5>
            <tr>
                <td></td>
                <td align=center><b>{$lang.Active}</b></td>
                <td align=center><b>{$lang.Archive}</b></td>
                <td align=center><b>{$lang.Deleted}</b></td>
            </tr>
            <tr>
                <td></td>
                <td align=center>{if $total.act}<a class="red_link href_post" title="{$lang.delete_active_records}"
                                                   href="edit_stat.php?clean=0"
                                                   onClick='{if !$full_version}jalert("{$lang.oper_prohib_trial_alert}"); return href_post=false;{else}return href_post=jconfirm("{$lang.clean_active_confirm}", this);{/if}'>{$lang.clean}</a>{/if}
                </td>
                <td align=center>{if $total.arc}<a class="red_link href_post" title="{$lang.clean_archive}"
                                                   href="edit_stat.php?clean=1"
                                                   onClick='{if !$full_version}jalert("{$lang.oper_prohib_trial_alert}"); return href_post=false;{else}return href_post=jconfirm("{$lang.clean_archive}?\n\n{$lang.create_auto_backup}", this);{/if}'>{$lang.clean}</a>{/if}
                </td>
                <td align=center>{if $total.del}<a class="red_link href_post" title="{$lang.clean_deleted}"
                                                   href="edit_stat.php?clean=2"
                                                   onClick='{if !$full_version}jalert("{$lang.oper_prohib_trial_alert}"); return href_post=false;{else}return href_post=jconfirm("{$lang.clean_deleted}?\n\n{$lang.create_auto_backup}", this);{/if}'>{$lang.clean}</a>{/if}
                </td>
            </tr>
            <tr>
                <td><b>{$lang.All_tables}</b></td>
                <td align=center id="total_act">{$total.act} {$lang.records} <span id="total_files_span_0"></span></td>
                <td align=center id="total_arc">{$total.arc} {$lang.records} <span id="total_files_span_1"></span></td>
                <td align=center id="total_del">{$total.del} {$lang.records} <span id="total_files_span_2"></span></td>
            </tr>
            {foreach from=$all_cats item=all_tables}
                <tr>
                    <td><b>{$all_tables[0].cat}</b></td>
                </tr>
                {foreach from=$all_tables item=one_table}
                    <tr>
                        <td style="padding-left:20px">{$one_table.display.name_table}</td>
                        <td align=center>
                            {if $one_table.act}<a href="fields.php?table={$one_table.id}&all_filters_on">{else}<span
                                        style="color:gray">{/if}
                                    {$one_table.act} {$lang.records}
                                {if $one_table.act}</a>{else}</span>{/if}
                            {if $one_table.file_fields}
                                <div id="files{$one_table.id}_0"></div>
                            {/if}
                        </td>
                        <td align=center>
                            {if $one_table.arc}<a href="fields.php?table={$one_table.id}&all_filters_on&archive">{else}
                                <span style="color:gray">{/if}
                                    {$one_table.arc} {$lang.records}
                                {if $one_table.arc}</a>{else}</span>{/if}
                            {if $one_table.file_fields}
                                <div id="files{$one_table.id}_1">
                                </div>
                            {/if}
                        </td>
                        <td align=center>
                            {if $one_table.del}<a href="fields.php?table={$one_table.id}&all_filters_on&deleted">{else}
                                <span style="color:gray">{/if}
                                    {$one_table.del} {$lang.records}
                                {if $one_table.del}</a>{else}</span>{/if}
                            {if $one_table.file_fields}
                                <div id="files{$one_table.id}_2">
                                </div>
                            {/if}
                        </td>
                    </tr>
                {/foreach}
            {/foreach}
        </table>
        <br>
        <a href="edit_add.php">{$lang.return_to_add_settings}</a>
        <br>
        <br>
    {/if}
</div>