<?
if ($level < 3) {
  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 `admin_journal`");
  $_SESSION['message'] = 'Журнал успешно очищен';
  header('Location: ?func=' . $func);
  exit;
}

$tmp = $system;
  
if (isset($_GET['set'])) {
  $set = esc(text($_GET['set']));
  
  if (isset($tmp[$set])) {
    $tmp[$set] = (int) $_GET['elid'];
  } else {
    $err[] = 'Параметр не существует';
  }
  
  if (!isset($err) && save_dat($tmp, 'settings.system')) {
    $messages[] = 'Настройки успешно приняты';
  }
}

$count = $sql -> fetch("
SELECT COUNT(*) AS other,
(SELECT COUNT(*) FROM `admin_journal` WHERE `type` = 'votes') AS 'votes',
(SELECT COUNT(*) FROM `admin_journal` WHERE `type` = 'goods') AS 'goods',
(SELECT COUNT(*) FROM `admin_journal` WHERE `type` = 'wmid') AS 'wmid',
(SELECT COUNT(*) FROM `admin_journal` WHERE `type` = 'complaint') AS 'complaint',
(SELECT COUNT(*) FROM `admin_journal` WHERE `type` = 'arbitrage') AS 'arbitrage',
(SELECT COUNT(*) FROM `admin_journal` WHERE `type` = 'votes' AND `read` = '0') AS 'votes_new',
(SELECT COUNT(*) FROM `admin_journal` WHERE `type` = 'complaint' AND `read` = '0') AS 'complaint_new',
(SELECT COUNT(*) FROM `admin_journal` WHERE `type` = 'arbitrage' AND `read` = '0') AS 'arbitrage_new',
(SELECT COUNT(*) FROM `admin_journal` WHERE `type` = 'goods' AND `read` = '0') AS 'goods_new',
(SELECT COUNT(*) FROM `admin_journal` WHERE `type` = 'wmid' AND `read` = '0') AS 'wmid_new',
(SELECT COUNT(*) FROM `admin_journal` WHERE `type` = 'other' AND `read` = '0') AS 'other_new'
FROM `admin_journal` WHERE `type` = 'other'");

$system['title'] = 'Админ - Журнал';
require SYS . 'header.php';
if (isset($_GET['truncate'])) {
	?>
	<div class="div_2">
	Вы уверены?
	  <a class="" href="/?func=admin.journal&amp;truncate=yes">Да</a> | 
	  <a class="" href="/?func=admin.journal">Нет</a>
	</div>
	<?
}
if ($type != '0') {
  $k_post = $sql -> selectCount("SELECT COUNT(*) FROM `admin_journal` WHERE `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 `admin_journal` WHERE `type` = '$type' ORDER BY `time` DESC LIMIT $start, $system[page_str]");
    
    $sql -> update("UPDATE `admin_journal` SET `read` = '1' WHERE `read` = '0' AND `type` = '$type'");
    
    foreach ($array AS $post) {
      $num++;
      ?>
      <div class="<?= ($num % 2 ? "nav1" : "nav2")?>">
       <?= login($post['id_user'], 1, 1)?> <?= ($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">
  &laquo; <a href="?func=admin.journal">К разделам</a>
  </div>
<?
} else {
  ?>
  
<div class="nav2">
&raquo; Уведомлять о выгрузке нового товара 
 <a href="?func=<?= $func?>&amp;set=j_newgoods&amp;elid=1">(<?= ($tmp['j_newgoods'] == 1 ? '&radic; ' : '')?>вкл)</a> <a href="?func=<?= $func?>&amp;set=j_newgoods&amp;elid=0">(<?= ($tmp['j_newgoods'] == 0 ? '&radic; ' : '')?>выкл)</a><br />
&raquo; Уведомлять о замене архива товара 
 <a href="?func=<?= $func?>&amp;set=j_filegoods&amp;elid=1">(<?= ($tmp['j_filegoods'] == 1 ? '&radic; ' : '')?>вкл)</a> <a href="?func=<?= $func?>&amp;set=j_filegoods&amp;elid=0">(<?= ($tmp['j_filegoods'] == 0 ? '&radic; ' : '')?>выкл)</a><br />
&raquo; Уведомлять о покупке кем то товара  
 <a href="?func=<?= $func?>&amp;set=j_paysgoods&amp;elid=1">(<?= ($tmp['j_paysgoods'] == 1 ? '&radic; ' : '')?>вкл)</a>  <a href="?func=<?= $func?>&amp;set=j_paysgoods&amp;elid=0">(<?= ($tmp['j_paysgoods'] == 0 ? '&radic; ' : '')?>выкл)</a><br />
&raquo; Уведомлять о новом сообщении забаненого  
 <a href="?func=<?= $func?>&amp;set=j_banmess&amp;elid=1">(<?= ($tmp['j_banmess'] == 1 ? '&radic; ' : '')?>вкл)</a>  <a href="?func=<?= $func?>&amp;set=j_banmess&amp;elid=0">(<?= ($tmp['j_banmess'] == 0 ? '&radic; ' : '')?>выкл)</a><br />
&raquo; Уведомлять о подтверждении WMID юзером  
 <a href="?func=<?= $func?>&amp;set=j_setwmid&amp;elid=1">(<?= ($tmp['j_setwmid'] == 1 ? '&radic; ' : '')?>вкл)</a>  <a href="?func=<?= $func?>&amp;set=j_setwmid&amp;elid=0">(<?= ($tmp['j_setwmid'] == 0 ? '&radic; ' : '')?>выкл)</a><br />
</div>
  
  <?
  if ($count['goods'] > 0) { ?>
    <a class="div_1" href="?func=admin.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['votes'] > 0) { ?>
    <a class="div_1" href="?func=admin.journal&amp;type=votes"><i class="icon-comment"></i> Отзывы (<?= $count['votes']?>) <?= ($count['votes_new'] > 0 ? '<span class="off">+' . $count['votes_new'] . '</span>' : '')?></a>
  <? $truej = true; } 
  
  if ($count['wmid'] > 0) { ?>
    <a class="div_1" href="?func=admin.journal&amp;type=wmid"><i class="icon-check"></i> Подтверждение WMID (<?= $count['wmid']?>) <?= ($count['wmid_new'] > 0 ? '<span class="off">+' . $count['wmid_new'] . '</span>' : '')?></a>
  <? $truej = true; } 
  
  if ($count['arbitrage'] > 0) { ?>
    <a class="div_1" href="?func=admin.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['complaint'] > 0) { ?>

    <a class="div_1" href="?func=admin.journal&amp;type=complaint"><i class="icon-ban-circle"></i> Жалобы (<?= $count['complaint']?>) <?= ($count['complaint_new'] > 0 ? '<span class="off">+' . $count['complaint_new'] . '</span>' : '')?></a>
 
  <? $truej = true; } 
  
  if ($count['other'] > 0) { ?>
    <a class="div_1" href="?func=admin.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=admin.journal&amp;truncate">&raquo; Очистить журнал</a>
  </div>
  <? 
  }
}
?>
<div class="foot">
 <a class="link" href="?func=admin">&laquo; В админку</a>
</div>