<?php

define('_CONSTANT_', 1);

require_once '../../../core/start.php';

checkAuth();

head('Пояс', 'Пояс');

$all = DB::$dbs->querySingle("SELECT COUNT(`id`) FROM ". BELT ." WHERE `user_id` = ?", [$user['id']]);

if (empty($all)) 
{
    ?> 
    <div class="wrap"> 
        Пусто...
    </div>
    <?php
} 
else 
{
    $n = new Navigator($all,10,'');
    $sql = DB::$dbs->query("SELECT * FROM ". BELT ." WHERE `user_id` = ? ORDER BY `status` DESC LIMIT {$n->start()}, 10", [$user['id']]);

    while ($potion = $sql -> fetch()) 
    {
        ?> 
        <div class="wrap">
            <img src="<?=HOME?>/ico/bottle/<?=$potion['img']?>.png" width="18px;"><?=$Filter->output($potion['name'])?> x<?=$Filter->clearInt($potion['count'])?>
             <?=(empty($potion['status']) ? '<a href="?setStatus='. $potion['id'] .'">Повесить на пояс</a>' : 'На поясе')?>
        </div>
        <?php
        if (isset($_GET['setStatus'])) 
        {
            $potionId = $Filter->clearInt($_GET['setStatus']);
            
            $pot = DB::$dbs->queryFetch("SELECT * FROM ". BELT ." WHERE `id` = ? and `user_id` = ?", [$potionId, $user['id']]);
            
            if (empty($pot)) 
            {
                errorNoExit('Зелье не найдено.');
            } 
            else 
            {
                if (DB::$dbs->querySingle("SELECT COUNT(`id`) FROM ". BELT ." WHERE `type` = ? and `user_id` = ? and `status` = ?", [$pot['type'], $user['id'], 1]) == 1) 
                {
                    DB::$dbs->query("UPDATE ". BELT ." SET `status` = ? WHERE `status` = ? and `type` = ? and `user_id` = ?", [0, 1, $pot['type'], $user['id']]);
                    DB::$dbs->query("UPDATE ". BELT ." SET `status` = ? WHERE `id` = ?", [1, $pot['id']]);
                    successNoExit('Вы повесили зелье на пояс.');
                } 
                else 
                {
                    DB::$dbs->query("UPDATE ". BELT ." SET `status` = ? WHERE `id` = ?", [1, $pot['id']]);
                    successNoExit('Вы повесили зелье на пояс.');
                }
            }
        }
    }
    echo $n->navi();
}

echo '<a href="' . HOME . '/modules/user/items/index.php?do=rack" class="link-touch">'. ico('rack.png') .' Рюкзак</a>';

require_once '../../../core/foot.php';