<?
if ($level == 0){
  die('Только для зарегистрированных пользователей');
}

if (isset($ID) && $level > 1 && !isset($_GET['admin'])) {
  $ank = $sql -> fetch("SELECT * FROM `users` WHERE `id` = '$ID' LIMIT 1");
}

if (!isset($ank['id']) && isset($user) || $level < 3) {
  $ank = $user;
}

if (isset($_GET['delete'])) {
  $id_goods = (int) $_GET['delete'];
  
  $pays = $sql -> fetch("SELECT * FROM `shop_pays` WHERE `id_goods` = '$id_goods' AND `id_user` = '$ank[id]' AND `pays` = '0' LIMIT 1");
  
  if ($pays['pays'] == 0) {
     $sql -> delete("DELETE FROM `shop_pays` WHERE `id_goods` = '$id_goods' AND `id_user` = '$ank[id]' AND `pays` = '0'");
    $messages[] = 'Счет успешно удален';
  }
}

$system['title'] = 'Корзина товаров ' . ($ank['id'] != $user['id'] ? login($ank['id']) : '');
require SYS . 'header.php';

  $k_post = $sql -> selectCount("SELECT COUNT(*) FROM `shop_pays` WHERE `id_user` = '$ank[id]' AND `pays` = '0'");

  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 `shop_pays` WHERE `id_user` = '$ank[id]' AND `pays` = '0' ORDER BY `time_pays` DESC LIMIT $start, $system[page_str]");

    foreach ($array AS $post) {
      $goods = $sql -> fetch("SELECT * FROM `shop_goods` WHERE `id` = '$post[id_goods]' AND `unlink` = '0' LIMIT 1");
      
      if ($goods['screens']) {
        $screen = unserialize($goods['screens']);
        if (is_file(SRC . 'screen/shop/' . $screen[0] . '.png')) {
          $_src = '/_src/img.php?link=' . base64_encode(SRC.'screen/shop/' . $screen[0] . '.png') . '&type=1';
        }
      } else {
        $_src = '/_src/img.php?link=' . base64_encode(ROOT.'_images/images/no_image.png') . '&type=1';
      }
?>
      <div class="media <?= ($num % 2 ? "nav1" : "nav2")?>">
        <a class="close" href="?func=shop.trash&amp;delete=<?= $post['id_goods']?>"><i class="icon-remove"></i></a> 
        <img src="<?= $_src?>" class="img-polaroid pull-left" /> 
        <div class="media-body"><a href="?func=shop.goods&amp;id=<?= $goods['id']?>" class="media-heading"><?= text($goods['name'])?></a> &raquo; 
        <span class="on"><b><?= $goods['wmr']?></b> wmr</span><br />
        <a href="?func=shop.payment&amp;id=<?= $goods['id']?>"><i class="icon-shopping-cart"></i> купить</a> <br />
        </div>
      </div>
<?
    }

    if ($k_page > 1) { 
      str('?func=' . $func . '&amp;id=' . $ank['id'] . '&amp;', $k_page, $page);
    }  
  }  
?>
<div class="foot">
&laquo; <a href="?func=user.main">В кабинет</a><br />
</div>