{include file="./index_top.tpl"}
<script type="text/javascript">
    // значения для системной подсказки
    var help_section = 'Settings';
    var help_sub_section = 'Logs';

    $(function () {

        $('.datetimepicker').datetimepicker({
            showOn: "button",
            dateFormat: lang.date_js_format,
            timeFormat: "HH:mm",
            showAlways: true,
            buttonImage: "images/calbtn.png",
            buttonImageOnly: true,
            buttonText: lang.Calendar,
            showAnim: (('\v' == 'v') ? "" : "show")  // в ie не включаем анимацию, тормозит
        }).attr('placeholder', lang.datetime_placeholder);
    });
</script>
<div align=center>
    <p style="font-size:1em;">{$lang.Logs}<span class='help_bt' h_id='Logs'></span></p>
    <div style="font-size:13px;"><a href="edit_log.php"
                                    {if !$difference && !$settings && !$archive && !$detail_crash && !$crash}class='selected_href'{/if}>{$lang.View_log}</a><span class="help_bt" h_id="View_log"></span>
        / <a href="?difference" {if $difference}class='selected_href'{/if}>{$lang.Difference} (<span
                    {if $logs_diff_change}style='color:red;'{/if}>{$logs_diff_change}</span>)</a><span class="help_bt" h_id="logs_diff_change"></span>{if $user.group_id==1 || $user.sub_admin_rights.ext_logs} /
        <a href="?crash"
           {if $crash || $detail_crash}class='selected_href'{/if}>{$lang.Crash_log}{if $crash_logs_count} (
            <span style='color:red;'>{$crash_logs_count}</span>
            ){/if}</a><span class="help_bt" h_id="crash_logs"></span>{/if}{if $user.group_id==1 || $user.sub_admin_rights.ext_logs} / <a href="?settings"
                                                                                                    {if $settings}class='selected_href'{/if}>{$lang.Settings_log}</a><span class="help_bt" h_id="Settings_log"></span>{/if}{if $user.group_id==1 || $user.sub_admin_rights.ext_logs}  /
            <a href="?archive" {if $archive}class='selected_href'{/if}>{$lang.Archive}</a><span class="help_bt" h_id="Archive_log"></span>{/if}</div>
    <br>
    <br>
    {if $settings} {* раздел настройки лога *}
    <form method=post action='edit_log.php?settings'>
        <input type=hidden name=csrf value='{$csrf}'>
        {$lang.Event_log_on}:
        <br><br>
        <table cellpadding=2 cellspacing=0 style="padding-left:20px">
            <tr>
                <td><input type="checkbox" name=event_on[login] {if $config.event_on.login}checked{/if}></td>
                <td>{$lang.login_event}<span class='help_bt' h_id='login_event'></span></td>
            </tr>
            <tr>
                <td><input type="checkbox" name=event_on[logout] {if $config.event_on.logout}checked{/if}></td>
                <td>{$lang.logout_event}<span class='help_bt' h_id='logout_event'></span></td>
            </tr>
            <tr>
                <td><input type="checkbox" name=event_on[view_table] {if $config.event_on.view_table}checked{/if}></td>
                <td>{$lang.view_table_event}<span class='help_bt' h_id='view_table_event'></span></td>
            </tr>

            <tr>
                <td><input type="checkbox" name=event_on[view_report] {if $config.event_on.view_report}checked{/if}>
                </td>
                <td>{$lang.view_report_event}<span class='help_bt' h_id='view_report_event'></span></td>
            </tr>

            <tr>
                <td><input type="checkbox" name=event_on[view_record] {if $config.event_on.view_record}checked{/if}>
                </td>
                <td>{$lang.view_record_event}<span class='help_bt' h_id='view_record_event'></span></td>
            </tr>
            <tr>
                <td><input type="checkbox" name=event_on[add_record] {if $config.event_on.add_record}checked{/if}></td>
                <td>{$lang.add_record_event}<span class='help_bt' h_id='add_record_event'></span></td>
            </tr>
            <tr>
                <td><input type="checkbox" name=event_on[delete_record] {if $config.event_on.delete_record}checked{/if}>
                </td>
                <td>{$lang.delete_record_event}<span class='help_bt' h_id='delete_record_event'></span></td>
            </tr>
            <tr>
                <td><input type="checkbox" name=event_on[archive_record]
                           {if $config.event_on.archive_record}checked{/if}></td>
                <td>{$lang.archive_record_event}<span class='help_bt' h_id='archive_record_event'></span></td>
            </tr>
            <tr>
                <td><input type="checkbox" name=event_on[restore_record]
                           {if $config.event_on.restore_record}checked{/if}></td>
                <td>{$lang.restore_record_event}<span class='help_bt' h_id='restore_record_event'></span></td>
            </tr>
            <tr>
                <td><input type="checkbox" name=event_on[import] {if $config.event_on.import}checked{/if}></td>
                <td>{$lang.import_event}<span class='help_bt' h_id='import_event'></span></td>
            </tr>
            <tr>
                <td><input type="checkbox" name=event_on[export] {if $config.event_on.export}checked{/if}></td>
                <td>{$lang.export_event}<span class='help_bt' h_id='export_event'></span></td>
            </tr>
            <tr>
                <td><input type="checkbox" name=event_on[change_field] {if $config.event_on.change_field}checked{/if}>
                </td>
                <td>{$lang.change_field_man_event}<span class='help_bt' h_id='change_field_man_event'></span></td>
            </tr>
            <tr>
                <td><input type="checkbox" name=event_on[change_calc_field]
                           {if $config.event_on.change_calc_field}checked{/if}></td>
                <td>{$lang.change_field_calc_event}<span class='help_bt' h_id='change_field_calc_event'></span></td>
            </tr>
            <tr>
                <td><input type="checkbox" name=event_on[change_cron_field]
                           {if $config.event_on.change_cron_field}checked{/if}></td>
                <td>{$lang.change_field_cron_event}<span class='help_bt' h_id='change_field_cron_event'></span></td>
            </tr>
            <tr>
                <td><input type="checkbox" name=event_on[sync] {if $config.event_on.sync}checked{/if}></td>
                <td>{$lang.sync_event}<span class='help_bt' h_id='sync_event'></span></td>
            </tr>
            <tr>
                <td><input type="checkbox" name=event_on[revis] {if $config.event_on.revis}checked{/if}></td>
                <td>{$lang.revis_event}<span class='help_bt' h_id='revis_event'></span></td>
            </tr>
            <tr>
                <td><input type="checkbox" name=event_on[asterisk] {if $config.event_on.asterisk}checked{/if}></td>
                <td>{$lang.asterisk_event}<span class='help_bt' h_id='asterisk_event'></span></td>
            </tr>
            <tr>
                <td><input type="checkbox" name=event_on[module_1c] {if $config.event_on.module_1c}checked{/if}></td>
                <td>{$lang.module_1c}<span class='help_bt' h_id='module_1c_event'></span></td>
            </tr>
            <tr>
                <td><input type="checkbox" name=event_on[use_add_action] {if $config.event_on.use_add_action}checked{/if}></td>
                <td>{$lang.add_action_event}<span class='help_bt' h_id='add_action_event'></span></td>
            </tr>
            <tr>
                <td><input type="checkbox" name=event_on[integration] {if $config.event_on.integration}checked{/if}></td>
                <td>{$lang.integration}<span class='help_bt' h_id='integration_event'></span></td>
            </tr>    
            <tr>
                <td><input type="checkbox" name=event_on[cbext_share] {if $config.event_on.cbext_share}checked{/if}></td>
                <td>{$lang.cbext_share}<span class='help_bt' h_id='cbext_share'></span></td>
            </tr>                    
        </table>
        <br>
        {$lang.Archiving_log}:
        <br><br>

        <table cellpadding=2 cellspacing=0 style="padding-left:20px">
            <tr>
                <td>{$lang.archiving_log_records} <input class="form-control form-control-160" type="text" name=log_arc
                                                         {if $config.log_arc}value='{$config.log_arc}'{/if}
                                                         style="text-align: right;"> {$lang.records}.
                </td>
            </tr>
        </table>
        <br>

        <input class="btn btn-default btn-sm" type=submit name=save_settings value="{$lang.Save}">
    </form>
    <br>

    {elseif $archive}        {* раздел архивы лога *}
    <form method=post action='edit_log.php?archive'>
        <input type=hidden name='csrf' value='{$csrf}'>
        <input class="btn btn-default btn-sm" type="submit" name='create_arc' value="{$lang.Archive_log_file}"><span class='help_bt'
                                                                                      h_id='Archive_log_file'></span>
        {if $arr_arc}
            <br>
            <br>
            <table cellpadding=2 cellspacing=0>
                {foreach from=$arr_arc item=file name=arc}
                    <tr valign="top">
                        <td width="30"><a class='href_post'
                                          href="edit_log.php?drop_file={$file.name|rawurlencode}&csrf={$csrf}"
                                          onClick="return href_post=jconfirm('{$lang.confirm_delete_file}', this);"><img
                                        src="images/b_drop.png" title="{$lang.Delete_file} {$file}"
                                        style="border-width: 0px; border-style: solid; "/></a></td>
                        <td nowrap><a href="edit_log.php?download&name={$file.name|rawurlencode}"
                                      title="{$lang.Download_file}">{$file.name}</a> &nbsp; {$file.size}</td>
                    </tr>
                {/foreach}
            </table>
        {/if}
        {if $smarty.foreach.arc.total > 1}
            <p>Общий объем архивов: {$fullsize_arc}</p>
            <input class="btn btn-default btn-sm" type="submit" name="download_all" value="Скачать все архивы">
            <br>
            <br>
            <input class="btn btn-default btn-sm" type="submit" name="drop_all" value="Удалить все архивы"
                   onclick="return confirm('Вы действительно хотите удалить все архивы?');">
        {/if}
    </form>
    {elseif $detail_crash}  {* подробно об ошибке *}
    <table cellpadding=2 cellspacing=0>
        <tr>
            <td align=right>{$lang.Date}:</td>
            <td>{$detail_crash.dislpay_date}</td>
        </tr>
        <tr>
            <td align=right>{$lang.User}:</td>
            <td>{$detail_crash.display_user}</td>
        </tr>
        <tr>
            <td align=right>URL:</td>
            <td>{$detail_crash.display_url}</td>
        </tr>
        <tr>
            <td align=right>{$lang.Error}:</td>
            <td>{$detail_crash.display_message}</td>
        </tr>
        <tr>
            <td align=right>{$lang.In_detail}:</td>
            <td>{$detail_crash.in_detail}</td>
        </tr>
    </table>
    {elseif $crash}        {* раздел ошибки программы *}

    {if $all_lines}
        <form method="post" style="text-align: center; margin: 20px 0px" id="clear_crash_form">
            <input type="hidden" name="csrf" value="{$csrf}"/>
            <input type="hidden" name="clear_crash_log" value="1"/>
            <input class="btn btn-default btn-sm" type="submit" value="{$lang.clear_crash_log}"
                   onclick="jconfirm('{$lang.clear_crash_confirm}', function() { document.getElementById('clear_crash_form').submit(); }, function() { }); return false;"/>
        </form>
        <table class="logs-crash" width="100%" cellpadding=2 cellspacing=0>
            <td align=center><b>{$lang.Date}</b></td>
            <td align=center><b>{$lang.User}</b></td>
            <td align=center><b>URL</b></td>
            <td align=center><b>{$lang.Error}</b></td>
            <td align=center><b>{$lang.In_detail}</b></td>
            </tr>
            {foreach from=$all_lines item=one_line}
                <tr>
                    <td align=center style='border-bottom: 1px #ccc solid' nowrap>{$one_line.dislpay_date}</td>
                    <td align=center style='border-bottom: 1px #ccc solid;' nowrap>{$one_line.display_user}</td>
                    <td style='border-bottom: 1px #ccc solid;' nowrap>{$one_line.display_url}</td>
                    <td style='border-bottom: 1px #ccc solid;'>{$one_line.display_message}</td>
                    <td style='border-bottom: 1px #ccc solid;'><a
                                href='edit_log.php?detail_crash={$one_line.guid}'>{$lang.In_detail}</a></td>
                </tr>
            {/foreach}
        </table>
    {else}
        <div style="text-align: center; margin: 20px 0px; font-size: 13px">
            {$lang.no_system_errors}
        </div>
    {/if}

    {elseif $difference}        {* раздел расхождений лога *}

    {*if $num_diff > 0*}

    <table width="100%" cellpadding=2 cellspacing=0>
        <td align=center><b>{$lang.Number}</b></td>
        <td align=center><b>{$lang.Message}</b></td>
        <td align=center><b>IP</b></td>
        <td align=center><b>{$lang.Date}</b></td>
        <td align=center><b>{$lang.Comment}</b></td>
        </tr>
        {foreach from=$arr_diff item=gr}
            <tr>
                <td colspan="5" style="height: 3em; background-color:#FFF;">
                    <hr>
                </td>
            </tr>
            {foreach from=$gr item=one_line}
                <tr {if $one_line.color}style="background-color:{$one_line.color};"{/if}>
                    <td align=center style='border-bottom: 1px #ccc solid'>{$one_line.id}</td>
                    <td style='border-bottom: 1px #ccc solid;{if $one_line.color}background-color:pink{*$one_line.color*};{/if}'>{$one_line.text}</td>
                    <td align=center style='border-bottom: 1px #ccc solid' nowrap>{$one_line.ip}&nbsp;&nbsp;</td>
                    <td align=center style='border-bottom: 1px #ccc solid' nowrap>{$one_line.date}</td>
                    <td style='border-bottom: 1px #ccc solid'>{$one_line.comment}</td>
                </tr>
            {/foreach}
        {/foreach}
    </table>
    {*/if*}

    {else}                   {* раздел просмотр лога *}

    {if $table_line}
        <div class="notifySearchLog no_print" id="table_line">
            {$table_line}
            <a class="bullet_button" href="edit_log.php" title="{$lang.Cancel_filter}"></a>
        </div>
    {else}
        <form method=post action="edit_log.php" class="search-form">
            <div class="search_bar" style="line-height:35px; border-bottom:1px solid #e3e3e3">
              <span class="search_cell" style="line-height: 5px;">
                {$lang.Event}:
                <table style="margin:7px 0" class="show_log_event">
                    <tr>
                        <td><input type="checkbox" name="f_event[]" value="admin"
                                   {if $f_event.admin}checked{/if}>{$lang.changing_settings}</td>
                        <td><input type="checkbox" name="f_event[]" value="login"
                                   {if $f_event.login}checked{/if}>{$lang.login_event}</td>
                        <td><input type="checkbox" name="f_event[]" value="logout"
                                   {if $f_event.logout}checked{/if}>{$lang.logout_event}</td>
                    </tr>
                    <tr>
                        <td><input type="checkbox" name="f_event[]" value="view_table"
                                   {if $f_event.view_table}checked{/if}>{$lang.view_table_event}</td>
                        <td><input type="checkbox" name="f_event[]" value="view_report"
                                   {if $f_event.view_report}checked{/if}>{$lang.view_report_event}</td>
                        <td><input type="checkbox" name="f_event[]" value="view_record"
                                   {if $f_event.view_record}checked{/if}>{$lang.view_record_event}</td>
                    </tr>
                    <tr>
                        <td><input type="checkbox" name="f_event[]" value="add_record"
                                   {if $f_event.add_record}checked{/if}>{$lang.add_record_event}</td>
                        <td><input type="checkbox" name="f_event[]" value="delete_record"
                                   {if $f_event.delete_record}checked{/if}>{$lang.delete_record_event}</td>
                        <td><input type="checkbox" name="f_event[]" value="archive_record"
                                   {if $f_event.archive_record}checked{/if}>{$lang.archive_record_event}</td>
                    </tr>
                    <tr>
                        <td><input type="checkbox" name="f_event[]" value="restore_record"
                                   {if $f_event.restore_record}checked{/if}>{$lang.restore_record_event}</td>
                        <td><input type="checkbox" name="f_event[]" value="import"
                                   {if $f_event.import}checked{/if}>{$lang.import_event}</td>
                        <td><input type="checkbox" name="f_event[]" value="export"
                                   {if $f_event.export}checked{/if}>{$lang.export_event}</td>
                    </tr>
                    <tr>
                        <td><input type="checkbox" name="f_event[]" value="change_field"
                                   {if $f_event.change_field}checked{/if}>{$lang.change_field_man_event}</td>
                        <td><input type="checkbox" name="f_event[]" value="change_calc_field"
                                   {if $f_event.change_calc_field}checked{/if}>{$lang.change_field_calc_event}</td>
                        <td><input type="checkbox" name="f_event[]" value="change_cron_field"
                                   {if $f_event.change_cron_field}checked{/if}>{$lang.change_field_cron_event}</td>
                    </tr>
                    <tr>
                        <td><input type="checkbox" name="f_event[]" value="sync"
                                   {if $f_event.sync}checked{/if}>{$lang.sync_event}</td>
                        <td><input type="checkbox" name="f_event[]" value="revis"
                                   {if $f_event.revis}checked{/if}>{$lang.revis_event}</td>
                        <td><input type="checkbox" name="f_event[]" value="asterisk"
                                   {if $f_event.asterisk}checked{/if}>{$lang.asterisk_event}</td>
                    </tr>
                    <tr>
                        <td><input type="checkbox" name="f_event[]" value="create_backup"
                                   {if $f_event.create_backup}checked{/if}>{$lang.create_backup_event}</td>
                        <td><input type="checkbox" name="f_event[]" value="download_backup"
                                   {if $f_event.download_backup}checked{/if}>{$lang.download_backup_event}</td>
                        <td><input type="checkbox" name="f_event[]" value="restore_backup"
                                   {if $f_event.restore_backup}checked{/if}>{$lang.restore_backup_event}</td>
                    </tr>
                    <tr>
                        <td><input type="checkbox" name="f_event[]" value="module_1c"
                                   {if $f_event.module_1c}checked{/if}>{$lang.module_1c}</td>
                        <td><input type="checkbox" name="f_event[]" value="security"
                                   {if $f_event.security}checked{/if}>{$lang.Security_tuning|mb_convert_case:MB_CASE_LOWER}</td>
                        <td><input type="checkbox" name="f_event[]" value="use_add_action"
                                   {if $f_event.use_add_action}checked{/if}>{$lang.add_action_event}</td>
                    </tr>
                    <tr>
                        <td><input type="checkbox" name="f_event[]" value="integration"
                                   {if $f_event.integration}checked{/if}>{$lang.integration}</td>
                        <td><input type="checkbox" name="f_event[]" value="cbext_share"
                                   {if $f_event.cbext_share}checked{/if}>{$lang.cbext_share}</td>
                    </tr>       
                </table>
              </span>
              <br>

              <span class="search_cell">
              {$lang.Period}
                  {$lang.since_from} <input type=text name='st_date' value='{$st_date}' class="datetimepicker form-control">
                  {$lang.since_to} <input type=text name='end_date' value='{$end_date}' class="datetimepicker form-control">
              </span>

              <span class="search_cell">
                {$lang.User}: <select class="form-control form-control-160" id=f_user_id name=f_user_id>
                      {foreach $f_users as $f_id=>$f_login}
                          <option value='{$f_id}' {if $f_id==$f_user_id}selected{/if}>{$f_login}</option>
                      {/foreach}
                  </select>
              </span>

              {* <span class="search_cell">
              {$lang.search_event}
                  <select style="display:none" hidden id="archive_find" class="form-control form-control-160" name="archive_find" id="">
                      <option value="no">Нет</option>
                      <option {if $archive_find}selected{/if} value="yes">Да</option>
                  </select>
              </span> *}

              <span class="search_cell">
                {$lang.Search} {$lang.for_text}: <input class="form-control form-control-250" type=text name="f_text" id="f_text" value="{$f_text}">
              </span>
              <br>

              <span class="search_cell">
                <b>IP:</b>
                <select class="form-control form-control-160" id="ip_cond" name="ip_cond">
                    <option value="0" {if $ip_cond==0}selected{/if}></option>
                    <option value="1" {if $ip_cond==1}selected{/if}>{$lang['=']}</option>
                    <option value="2" {if $ip_cond==2}selected{/if}>{$lang['!=']}</option>
                    <option value="3" {if $ip_cond==3}selected{/if}>{$lang['LIKE']}</option>
                </select>

                <input class="form-control form-control-160" type=text name='choosen_ip' id='choosen_ip' value="{if $choosen_ip}{$choosen_ip}{/if}">
                <br>
              </span>

              <span class="search_cell">
               <input type="hidden" name="csrf" value="{$csrf}"/>
               <input type="hidden" name="apply_but" value="1"/>
               <input class="btn btn-default btn-sm submit-apply" type=submit value="{$lang.Apply}">
               <input class="btn btn-default btn-sm submit-reset" type=button value="{$lang.Reset}" onclick="location.href='edit_log.php'">
              </span>
            </div>
        </form>
    {/if}
    <br>
    <table class="logs-table" width="100%" cellpadding=2 cellspacing=0>
        <td align=center><b>{$lang.Number}</b></td>
        <td align=center><b>{$lang.Message}</b></td>
        <td align=center><b>IP</b></td>
        <td align=center><b>{$lang.Date}</b></td>
        <td align=center><b>{$lang.User}</b></td>
        </tr>
        {foreach from=$all_lines item=one_line}
            <tr style='{if $is_pink == 0}{if $one_line.color}background-color:{$one_line.color};{/if}{/if}'>
                <td align=center style='border-bottom: 1px #ccc solid; {if $one_line.important}font-weight:bold;{/if}'>{$one_line.id}</td>
                <td style='border-bottom: 1px #ccc solid; {if $one_line.important}font-weight:bold;{/if}'>
                    {assign var="textparts" value="[[--more--]]"|explode:$one_line.text}
                    {$textparts.0}  
                    {if isset($textparts.1)}
                        <br/>
                        <a onclick="this.nextElementSibling.style.display='block'; this.style.display='none'; return false;">{$lang.In_detail}...</a>
                        <pre style="display:none; max-height: 600px; overflow-y: auto;">{$textparts.1}</pre>
                    {/if}
                </td>
                <td align=center style='border-bottom: 1px #ccc solid; {if $one_line.important}font-weight:bold;{/if}' nowrap>{$one_line.ip}&nbsp;&nbsp;</td>
                <td align=center style='border-bottom: 1px #ccc solid; {if $one_line.important}font-weight:bold;{/if}' nowrap>{$one_line.date}&nbsp;&nbsp;</td>
                <td style='border-bottom: 1px #ccc solid; {if $one_line.important}font-weight:bold;{/if}' nowrap>{$one_line.user}</td>
            </tr>
        {/foreach}
    </table>
</div>
    <div align=center class="pages">
        {foreach from=$all_pages item=one_page}
            {if $one_page eq $page}
                <span>{$one_page}</span>
            {elseif $one_page eq '...'}
                {$one_page}
            {elseif $sync_id}
                <a href="edit_log.php?sync={$sync_id}&page={$one_page}">{$one_page}</a>
            {elseif $line_id && $table_id}
                <a href="edit_log.php?table={$table_id}&line={$line_id}&page={$one_page}&back_url={$base64_back_url}">{$one_page}</a>
            {else}
                <a href="edit_log.php?f_user_id={$f_user_id}&{foreach from=$f_event item=item_event key=key_event}f_event[]={$key_event}&{/foreach}st_date={$st_date}&end_date={$end_date}&f_text={$f_text}&ip_cond={$ip_cond}&choosen_ip={$choosen_ip}&page={$one_page}">{$one_page}</a>
            {/if}
        {/foreach}
    </div>
<br>
    {if $back_url}<p align="center"><a href="{$back_url}">{$lang.Return}</a></p>{/if}
{/if}
<br>

<script>
    $('.search-form').submit(function(e) {
        e.preventDefault();
        var self = this;
        jconfirm('{$lang.long_search}', function() {
            var preloader = create_preloader_block('100%', '100%');
            $('.logs-table').prepend(preloader);
            $(self).unbind('submit').submit();
        });

    });


    {if isset($smarty.get.file_size_exceeded)}
        jalert('{$lang.file} {$smarty.get.filename} {$lang.file_oversize}');
    {/if}

    {if isset($smarty.get.total_size_exceeded)}
        jalert('{$lang.total_oversize}');
    {/if}

</script>
