<?php

if ($ID != 0) {
  $category = mysql_fetch_assoc(mysql_query("SELECT * FROM `treats_category` WHERE `id` = '$ID' LIMIT 1"));
}

if (empty($category['id'])) {
  $category = array('id' => 0, 'name' => $config['module_name'], 'dir' => '/');
}

$returnPage = returnPage($category['id'], $category['dir']);

moduleHeader($config['module_name']);

if ($level == 1 && $category['id'] != 0) { 
  ?>
  <div class="foot">
    <a href="?func=edit&amp;act=goods&amp;id=<?= $category['id']?>&amp;elid=create">
      <img src="/<?= $config['icons_patch']?>/blue-document--plus.png" /> <?= __('Добавить товар')?>
    </a>
  </div>
  <? 
}

if ($category['id'] == 0) {
  ?>
  <div class="mess">
    <center><img src="/<?= $config['style_patch']?>/logo.png" /></center>
  </div>
  <?
}

$count['goods'] = mysql_result(mysql_query("SELECT COUNT(*) FROM `treats_goods` WHERE `id_category` = '$category[id]'"),0);
$count['category'] = mysql_result(mysql_query("SELECT COUNT(*) FROM `treats_category` WHERE `id_category` = '$category[id]'"),0);
$k_post = $count['goods'];

if ($count['goods'] + $count['category'] == 0) {
  ?>
  <div class="mess"><?= __('Категория пуста')?></div>
  <?
}

$k_page = k_page($k_post, $set['p_str']);
$page = page($k_page);
$start = $set['p_str'] * $page - $set['p_str'];

if ($page == 1) {
  $q = mysql_query("SELECT *
                    FROM `treats_category` 
                    WHERE `id_category` = '$category[id]' ORDER BY `name`");

  while($post = mysql_fetch_assoc($q)) 
  {
    $post['count'] = mysql_result(mysql_query("SELECT COUNT(*) FROM `treats_goods` WHERE `dir` like '%/$post[id]/%'"), 0);
    $post['new'] = mysql_result(mysql_query("SELECT COUNT(*) FROM `treats_goods` WHERE `dir` like '%/$post[id]/%' AND `time` > '" . mktime(0, 0, 0) . "'"), 0);
    ?>
    <div class="nav<?= ($num % 2 ? 1 : 2)?>">
      <a href="?func=category&amp;id=<?= $post['id']?>" style="display: block;">
        <img src="/<?= $config['icons_patch']?>/<?= $post['icon']?>" /> <?= text($post['name'])?> 
        (<?= $post['count']?>) <?= ($post['new'] > 0 ? '<span class="off">+' . $post['new'] . '</span>' : '')?><br />
        <span class="text"><?= output_text($post['msg'])?></span>
      </a>
    </div>
    <?
  }
  unset($q, $post);  
}


$q = mysql_query("SELECT *
                  FROM `treats_goods` 
                  WHERE `id_category` = '$category[id]' ORDER BY `time` DESC LIMIT $start, $set[p_str]");

while($post = mysql_fetch_assoc($q)) 
{
  ?>
  <div class="nav<?= ($num % 2 ? 1 : 2)?>">
    <a href="?func=treats&amp;id=<?= $post['id']?>" style="display: block;">
      <b><?= text($post['name'])?></b><br /> 
      <img src="/<?= $config['goods_patch']?>/<?= $post['icon']?>.png" style="max-width: 70px; max-height: 70px;" /><br /> 
      <?= __('Стоимость')?>: <span class="on"><?= sclon2num($post['coins'], $config['coins_name'])?></span><br />
    </a>
  </div>
  <?
}
unset($q, $post);

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

if ($level == 1) {
  ?>
  <div class="foot">
    <a href="?func=edit&amp;act=category&amp;id=<?= $category['id']?>&amp;elid=create"><img src="/<?= $config['icons_patch']?>/folder--plus.png" /> <?= __('Добавить раздел')?></a><br />
    
    <? if ($category['id'] != 0) { ?>
    
      <a href="?func=edit&amp;act=category&amp;id=<?= $category['id']?>&amp;elid=edit">
        <img src="/<?= $config['icons_patch']?>/folder--pencil.png" /> <?= __('Параметры раздела')?>
      </a><br />
      
      <a href="?func=delete&amp;act=category&amp;id=<?= $category['id']?>">
        <img src="/<?= $config['icons_patch']?>/folder--minus.png" /> <?= __('Удалить раздел')?>
      </a><br />
      
    <? } ?>
  </div>
  <?
}
?>
<div class="foot">
  <?= $returnPage?>
</div>