<?
if ($level < 2){
  header('Location: /index.php');
  exit;
}

$cats = array('1' => 'Личные данные', '2' => 'Магазин', '3' => 'Товары', '4' => 'Вывод на другие системы', '5' => 'Прочее');

if (isset($_GET['delete']) && $level == 4) {
  $delete = (int) $_GET['delete'];
  $sup = $sql -> fetch("SELECT * FROM `support_id` WHERE `id` = '$delete' LIMIT 1");
  
  if (isset($sup['id'])) {
    $sql -> delete("DELETE FROM `support_id` WHERE `id` = '$delete'");
    $sql -> delete("DELETE FROM `support` WHERE `id_tiket` = '$delete'");
    
    if ($sup['screens']) {
      $screens = unserialize($sup['screens']);
      foreach($screens AS $key => $cache) {
        @unlink(SRC . 'screen/support/' . $cache . '.png');
      }      
    }
    $messages[] = 'Тикет успешно удален';
  }
}

if (isset($_GET['close']) && $_GET['close'] != '0') {
  $close = '1';
  $order = "`close` = '1'";
} else {
  $close = '0';
  $order = "`close` = '0'";
}

$system['title'] = 'Cписок тикетов';
require SYS . 'header.php';

?>
<div class="pagination pagination-centered pagination-small" style="margin: 4px;"><ul>
<li><?= ($close != 0 ? '<a href="?func=' . $func . '&amp;close=0">Открытые</a>' : '<a style="color: black;">Открытые</a>')?></li>
<li><?= ($close != 1 ? '<a href="?func=' . $func . '&amp;close=1">Закрытые</a>' : '<a style="color: black;">Закрытые</a>')?></li>
</ul></div>
<?

$k_post = $sql -> selectCount("SELECT COUNT(*) FROM `support_id` WHERE $order");

if ($k_post == 0) {
  ?>
  <div class="alert" style="margin: 2px;">Список тикетов пуст</div>
  <?
} else {
  $k_page = k_page($k_post, $system['page_str']);
  $page = page($k_page);
  $start =  $system['page_str'] * $page -  $system['page_str'];

  $array = $sql -> select("SELECT * FROM `support_id` WHERE $order ORDER BY `time_last` DESC LIMIT $start, $system[page_str]");

  foreach ($array AS $support) {
    
    $count = $sql -> fetch("SELECT 
      SUM(IF(`read` = '0' AND `level` = '1' AND `id_user` = '$support[id_user]' AND `id_tiket` = '$support[id]', 1, 0)) AS 'new',
      SUM(IF(`level` = '1' AND `id_tiket` = '$support[id]' AND `id_user` != '0', 1, 0)) AS 'contact',
      SUM(IF(`level` > '1' AND `id_tiket` = '$support[id]', 1, 0)) AS 'user'
      FROM `support`");
      
    $count = array('new' => (int) $count['new'], 'contact' => (int) $count['contact'], 'user' => (int) $count['user']);
    $num++;
    ?>
    <div class="<?= ($num % 2 ? "nav1" : "nav2")?>">
      <? if ($level == 4) { ?>
      <a href="?func=admin.support&amp;delete=<?= $support['id']?>&amp;page=<?= $page?>&amp;close=<?= $close?>" class="close">&times;</a>
      <? } ?>
      <a href="?func=support&amp;id=<?= $support['id']?>"><i class="icon-comment"></i> <?= text($support['title'])?></a> 
      [<?= $count['contact']?>/<?= $count['user']?>]<?= ($count['new'] > 0 ? ' <span class="off">+' . $count['new'] . '</span>' : '')?>  (<?= vremja($support['time'])?>)<br />
      Категория: <b><?= $cats[$support['type']]?></b><br />
      Посл.: <?= login($support['id_last'], 1, 1)?> (<?= vremja($support['time_last'])?>)<br />
      Статус: <b><?= ($support['close'] == 0 ? '<span class="on">Открытый тикет</span>' : '<span class="off">Закрытый тикет</span>')?></b><br />
      
    </div>
    <?
  }

  if ($k_page > 1) { 
    str('?func=' . $func . '&close='.$close.'&amp;', $k_page, $page);
  }
}
?>