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

if (isset($_GET['type']) && preg_match("#^([A-z])+$#ui", $_GET['type'])) {
  $type = esc(text($_GET['type']));
} else {
  $type = '0';
}

if (isset($_GET['truncate']) AND $_GET['truncate'] == 'yes') {
  $sql -> delete("DELETE FROM `journal` WHERE `id_user` = '$user[id]'");
  $_SESSION['message'] = 'Журнал успешно очищен';
  header('Location: /?func=' . $func);
  exit;
}

$count = $sql -> fetch("
SELECT COUNT(*) AS other,
(SELECT COUNT(*) FROM `journal` WHERE `id_user` = '$user[id]' AND `type` = 'goods') AS 'goods',
(SELECT COUNT(*) FROM `journal` WHERE `id_user` = '$user[id]' AND `type` = 'comment') AS 'comment',
(SELECT COUNT(*) FROM `journal` WHERE `id_user` = '$user[id]' AND `type` = 'spy') AS 'spy',
(SELECT COUNT(*) FROM `journal` WHERE `id_user` = '$user[id]' AND `type` = 'referals') AS 'referals',
(SELECT COUNT(*) FROM `journal` WHERE `id_user` = '$user[id]' AND `type` = 'support') AS 'support',
(SELECT COUNT(*) FROM `journal` WHERE `id_user` = '$user[id]' AND `type` = 'arbitrage') AS 'arbitrage',
(SELECT COUNT(*) FROM `journal` WHERE `id_user` = '$user[id]' AND `type` = 'arbitrage' AND `read` = '0') AS 'arbitrage_new',
(SELECT COUNT(*) FROM `journal` WHERE `id_user` = '$user[id]' AND `type` = 'support' AND `read` = '0') AS 'support_new',
(SELECT COUNT(*) FROM `journal` WHERE `id_user` = '$user[id]' AND `type` = 'goods' AND `read` = '0') AS 'goods_new',
(SELECT COUNT(*) FROM `journal` WHERE `id_user` = '$user[id]' AND `type` = 'spy' AND `read` = '0') AS 'spy_new',
(SELECT COUNT(*) FROM `journal` WHERE `id_user` = '$user[id]' AND `type` = 'referals' AND `read` = '0') AS 'referals_new',
(SELECT COUNT(*) FROM `journal` WHERE `id_user` = '$user[id]' AND `type` = 'comment' AND `read` = '0') AS 'comment_new',
(SELECT COUNT(*) FROM `journal` WHERE `id_user` = '$user[id]' AND `type` = 'other' AND `read` = '0') AS 'other_new'
FROM `journal` WHERE `id_user` = '$user[id]' AND `type` = 'other'");


$system['title'] = 'Журнал';
require SYS . 'header.php';
if (isset($_GET['truncate'])) {
	?>
	<div class="div_2">
	Вы уверены?
	  <a class="" href="/?func=user.journal&amp;truncate=yes">Да</a> | 
	  <a class="" href="/?func=user.journal">Нет</a>
	</div>
	<?
}
if ($type != '0') {
  $k_post = $sql -> selectCount("SELECT COUNT(*) FROM `journal` WHERE `id_user` = '$user[id]' AND `type` = '$type'");

  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 `journal` WHERE `id_user` = '$user[id]' AND `type` = '$type' ORDER BY `time` DESC LIMIT $start, $system[page_str]");
    
    $sql -> update("UPDATE `journal` SET `read` = '1' WHERE `id_user` = '$user[id]' AND `read` = '0' AND `type` = '$type'");
    
    foreach ($array AS $post) {
      $num++;
      ?>
      <div class="<?= ($num % 2 ? "nav1" : "nav2")?>">
        <?= ($post['read'] == 0 ? '<span class="off">' . vremja($post['time']) . '</span>' : vremja($post['time']))?> :: 
        <?= text_out($post['msg'])?><br />
      </div>
      <?
    }

    if ($k_page > 1) { 
      str('/?func=' . $func . '&amp;type=' . $type . '&amp;', $k_page, $page);
    }
  }
  
?>
  <div class="foot">
  <a class="link" href="/?func=user.journal"> &laquo; К разделам</a>
  </div>
<?
} else {
  if ($count['goods'] > 0) { ?>
    <a class="div_1" href="/?func=user.journal&amp;type=goods"><i class="icon-shopping-cart"></i> Товары (<?= $count['goods']?>) <?= ($count['goods_new'] > 0 ? '<span class="off">+' . $count['goods_new'] . '</span>' : '')?></a>
  <? $truej = true; } 

  if ($count['referals'] > 0) { ?>
    <a class="div_1" href="?func=user.journal&amp;type=referals"><i class="icon-magnet"></i> <?= __('Рефералы')?> (<?= $count['referals']?>) <?= ($count['referals_new'] > 0 ? '<span class="off">+' . $count['referals_new'] . '</span>' : '')?></a>
  <? $truej = true; } 
  
  if ($count['spy'] > 0) { ?>
    <a class="div_1" href="/?func=user.journal&amp;type=spy"><i class="icon-indent-left"></i> Подписки (<?= $count['spy']?>) <?= ($count['spy_new'] > 0 ? '<span class="off">+' . $count['spy_new'] . '</span>' : '')?></a>
  <? $truej = true; } 
  
  if ($count['support'] > 0) { ?>
    <a class="div_1" href="/?func=user.journal&amp;type=support"><i class="icon-bullhorn"></i> Тикеты (<?= $count['support']?>) <?= ($count['support_new'] > 0 ? '<span class="off">+' . $count['support_new'] . '</span>' : '')?></a>
  <? $truej = true; } 
  
  if ($count['arbitrage'] > 0) { ?>
    <a class="div_1" href="/?func=user.journal&amp;type=arbitrage"><i class="icon-filter"></i> Арбитраж (<?= $count['arbitrage']?>) <?= ($count['arbitrage_new'] > 0 ? '<span class="off">+' . $count['arbitrage_new'] . '</span>' : '')?></a>
  <? $truej = true; } 
  
  if ($count['comment'] > 0) { ?>
    <a class="div_1" href="/?func=user.journal&amp;type=comment"><i class="icon-comment"></i> Комментарии (<?= $count['comment']?>) <?= ($count['comment_new'] > 0 ? '<span class="off">+' . $count['comment_new'] . '</span>' : '')?></a>
  
  <? $truej = true; } ?>
  
  <? if ($count['other'] > 0) { ?>
    <a class="div_1" href="/?func=user.journal&amp;type=other"><i class="icon-question-sign"></i> Разное | Другое (<?= $count['other']?>) <?= ($count['other_new'] > 0 ? '<span class="off">+' . $count['other_new'] . '</span>' : '')?></a>
  
  <? $truej = true; } ?>
  
  <? if (!isset($truej)) { ?>
    <div class="alert">
    Список уведомлений пуст
    </div>
  <? } else { ?>
  <div class="foot">
  <a class="link" href="/?func=user.journal&amp;truncate">&raquo; Очистить журнал</a>
  </div>
  <? 
  }
}
?>
<div class="foot">
<a class="link" href="/?func=user.main">&laquo; В кабинет</a>
</div>