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

$viewed_user_id = isset($_GET['id']) ? (int)$_GET['id'] : $_SESSION['user_id'];
$user = getUserById($viewed_user_id);
if (!$user) {
    header('Location: index.php');
    exit;
}

$is_own_profile = ($viewed_user_id == $_SESSION['user_id']);
$equippedItems = getEquippedItems($viewed_user_id);
$equipCount = count($equippedItems);

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

require_once 'inc/header.php';
?>

<span class="ttl">
    <b>Снаряжение</b> <?= htmlspecialchars($user['username']) ?> 
    <span class="minor">(<?= $equipCount ?>/10)</span>
</span>
<div class="clb"></div>
<div class="hr"></div>

<table cellpadding="1" cellspacing="0" border="0" width="100%">
<tbody>
<?php foreach ($slots as $slot): 
    $item = null;
    foreach ($equippedItems as $ei) {
        if ($ei['equipped_slot'] == $slot) {
            $item = $ei;
            break;
        }
    }
?>
    <tr>
        <td valign="top" width="40">
            <?php if ($item): ?>
                <img src="<?= htmlspecialchars($item['image']) ?>" width="40" height="40" alt="" border="0"/>
            <?php else: ?>
                <img src="images/items/empty_slot.png" width="40" height="40" alt="пусто" border="0"/>
            <?php endif; ?>
        </td>
        <td valign="top" style="padding-left:6px;">
            <?php if ($item): 
                $bonusValues = getItemBonusValues($item);
                $statsList = [];
                $statNames = [
                    'attack'   => 'атака',
                    'health'   => 'здоровье',
                    'defense'  => 'защита',
                    'stamina'  => 'выносливость',
                    'accuracy' => 'точность',
                    'recovery' => 'восстановление'
                ];
                foreach ($bonusValues as $stat => $value) {
                    if ($value > 0) {
                        $statsList[] = $statNames[$stat] . ' +' . $value;
                    }
                }
            ?>
                <span>
                    <img width="12" height="12" src="images/icons/<?= $item['quality_icon'] ?>" alt=""/>
                    <?php if ($is_own_profile): ?>
                        <a href="item.php?id=<?= $item['id'] ?>" style="color:<?= $item['quality_color'] ?>;">
                            <?= htmlspecialchars($item['item_name']) ?>
                        </a>
                    <?php else: ?>
                        <span style="color:<?= $item['quality_color'] ?>;">
                            <?= htmlspecialchars($item['item_name']) ?>
                        </span>
                    <?php endif; ?>
                </span>
                <br>
                <span class="small">
                    <?= $item['level_required'] ?> ур,
                    <?= $slotNames[$slot] ?>,
                    <?php if (!empty($statsList)): ?>
                        <span style="color:#6FCD72;"><?= implode(', ', $statsList) ?></span>
                    <?php endif; ?>
                    <?php if ($item['is_bound']): ?>
                        <span class="minor">(личное)</span>
                    <?php endif; ?>
                </span>
                <?php if ($is_own_profile): ?>
                    <br>
                    <form method="post" action="bag.php" style="display:inline;">
                        <input type="hidden" name="item_id" value="<?= $item['id'] ?>">
                        <button type="submit" name="action" value="unequip" class="btn small">снять</button>
                    </form>
                <?php endif; ?>
            <?php else: ?>
                <span class="small">[пустой слот]</span><br>
                <span class="small"><?= $slotNames[$slot] ?></span>
            <?php endif; ?>
        </td>
    </tr>
    <tr>
        <td colspan="2"><div class="hr"></div></td>
    </tr>
<?php endforeach; ?>
</tbody>
</table>

<a href="profile.php<?= $is_own_profile ? '' : '?id='.$viewed_user_id ?>" class="btn full">В профиль</a>
<div class="hr"></div>

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