<?php
session_start();
require_once 'inc/functions.php';
check404();
securityCheck();
if (!isLoggedIn()) {
    header('Location: index.php');
    exit;
}

$user = getUserById($_SESSION['user_id']);
if (!$user) {
    session_destroy();
    header('Location: index.php');
    exit;
}

$itemId = isset($_GET['id']) ? (int)$_GET['id'] : 0;
$userItem = getUserItemById($itemId);
if (!$userItem || $userItem['user_id'] != $user['id']) {
    header('Location: bag.php');
    exit;
}

$slotNames = [
    'head' => 'Голова',
    'neck' => 'Шея',
    'body' => 'Тело',
    'pants' => 'Штаны',
    'belt' => 'Пояс',
    'bracers' => 'Браслеты',
    'gloves' => 'Перчатки',
    'ring' => 'Кольцо',
    'weapon' => 'Оружие',
    'boots' => 'Обувь'
];

$bonusValues = getItemBonusValues($userItem);
$statsMap = [
    'attack'   => ['icon' => 'strength',     'name' => 'Атака'],
    'health'   => ['icon' => 'life',         'name' => 'Здоровье'],
    'defense'  => ['icon' => 'shield',       'name' => 'Защита'],
    'stamina'  => ['icon' => 'energy',       'name' => 'Выносливость'],
    'accuracy' => ['icon' => 'accuracy',     'name' => 'Точность'],
    'recovery' => ['icon' => 'regeneration', 'name' => 'Восстановление'],
];

require_once 'inc/header.php';
?>

<span class="ttl"><b><?= htmlspecialchars($userItem['item_name']) ?></b></span>
<div class="clb"></div>
<div class="hr"></div>

<div>
    <table cellpadding="1" cellspacing="0" border="0">
        <tr>
            <td valign="top">
                <img src="<?= htmlspecialchars($userItem['image']) ?>" width="40" height="40" alt=""/>
            </td>
            <td valign="top" style="padding-left:6px;">
                <div>
                    <img width="12" height="12" src="images/icons/<?= $userItem['quality_icon'] ?>" alt=""/>
                    <span style="color:<?= $userItem['quality_color'] ?>;"><?= htmlspecialchars($userItem['quality_name']) ?></span>
                </div>
                <div>
                    <span><?= htmlspecialchars($userItem['item_name']) ?></span>
                </div>
                <div>
                    <?= $slotNames[$userItem['slot']] ?? $userItem['slot'] ?>, <strong><?= $userItem['level_required'] ?></strong> ур
                </div>
            </td>
        </tr>
    </table>
</div>
<div class="hr"></div>

<div>
    <strong>Характеристики:</strong><br>
    <ul>
        <?php foreach ($bonusValues as $stat => $value): ?>
            <?php if ($value > 0): ?>
                <li>
                    <img border="0" width="12" height="12" src="images/icons/<?= $statsMap[$stat]['icon'] ?>.png" alt=""/>
                    <?= $statsMap[$stat]['name'] ?>: +<?= $value ?>
                </li>
            <?php endif; ?>
        <?php endforeach; ?>
    </ul>
    <p class="minor">(бонус качества: +<?= $userItem['bonus_percent'] ?>%)</p>
</div>
<div class="hr"></div>

<div>
    Статус: <?= $userItem['is_equipped'] ? 'надето' : 'в сумке' ?><br>
    <?php if ($userItem['is_bound']): ?>
        <span class="minor">(личное)</span>
    <?php endif; ?>
</div>
<div class="hr"></div>

<a href="bag.php">← Вернуться в сумку</a>

<?php require_once 'inc/foot.php'; ?>