<?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;
}

$abilities = getAllAbilities();

require_once 'inc/header.php';
?>

<span class="ttl"><b>Магазин умений</b></span>
<div class="clb"></div>
<div class="hr"></div>

<div class="small cntr">
    Здесь ты можешь купить умения, которые повысят твои боевые качества.
</div>
<div class="hr"></div>

<?php foreach ($abilities as $ability): 
    $userAbility = getUserAbility($user['id'], $ability['id']);
    $currentTier = $userAbility ? $userAbility['tier_level'] : 0;
    $nextTier = $currentTier + 1;
    $tierInfo = null;
    if ($nextTier <= 6) {
        $tierInfo = getAbilityTier($ability['id'], $nextTier);
    }
?>
    <div>
        <table><tbody><tr>
            <td style="width:1%;vertical-align:top;">
                <img width="40" height="40" src="/images/ability/<?= $ability['icon'] ?>" alt="">
            </td>
            <td style="vertical-align:top;">
                <a href="ability.php?id=<?= $ability['id'] ?>"><span><?= htmlspecialchars($ability['name']) ?></span></a>
                <br><span class="small">
                    <?php if ($currentTier > 0): ?>
                        <span class="info">Ваш уровень: <?= $currentTier ?> (<?= getUserAbility($user['id'], $ability['id'])['tier_name'] ?? '' ?>)</span><br>
                    <?php endif; ?>
                    <?php if ($tierInfo): ?>
                        <span class=""><img width="12" height="12" src="/images/icons/<?= $tierInfo['icon_star'] ?>" alt=""/> 
                        <?php
                        switch ($ability['effect_type']) {
                            case 'attack_multiplier': echo 'сила атаки'; break;
                            case 'defense_percent':   echo 'процент защиты'; break;
                            case 'evasion':            echo 'уклонение'; break;
                            case 'crit_chance':        echo 'крит. шанс'; break;
                            default: echo 'эффект';
                        }
                        ?>: 
                        <span class="info"><?= $tierInfo['effect_value'] ?>%</span></span>
                        <br>[ <a href="ability.php?id=<?= $ability['id'] ?>&buy=<?= $nextTier ?>">купить</a> ] 
                        цена: <img src="images/icons/money_gold.png" width="12" height="12" alt="з"/><?= $tierInfo['price_gold'] ?>
                    <?php else: ?>
                        <span class="minor">Максимальный уровень достигнут!</span>
                    <?php endif; ?>
                </span>
            </td>
        </tr></tbody></table>
        <div class="hr"></div>
    </div>
<?php endforeach; ?>

<div class="small info">
    При покупке любого умения Вы можете получить подарок - более качественное умение за те же деньги! Шансы более 10%! (акция)
</div>

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