<?php
session_start();
require_once '../inc/admin_auth.php';
requireAdmin();
require_once '../inc/functions.php';

$page = $_GET['page'] ?? 1;
$limit = 50;
$offset = ($page - 1) * $limit;

$logs = getAllBattleLogs($limit, $offset);
$total = $pdo->query("SELECT COUNT(*) FROM battle_logs")->fetchColumn();

require_once '../inc/header.php';
?>

<span class="ttl"><b>⚔️ Логи боёв</b></span>
<div class="clb"></div>
<div class="hr"></div>

<table class="tables" border="1" cellpadding="5" style="width:100%;">
    <tr>
        <th>ID</th>
        <th>Атакующий</th>
        <th>Защитник</th>
        <th>Результат</th>
        <th>💰 Баксы</th>
        <th>🔮 Опыт</th>
        <th>⏰ Время</th>
    </tr>
    <?php foreach ($logs as $log): ?>
    <tr>
        <td><?= $log['id'] ?></td>
        <td><?= htmlspecialchars($log['attacker_name'] ?? 'Бот') ?> (<?= $log['attacker_stats'] ?>)</td>
        <td><?= htmlspecialchars($log['defender_name'] ?? 'Бот') ?> (<?= $log['defender_stats'] ?>)</td>
        <td>
            <?php if ($log['result'] == 'win'): ?>🏆 Победа
            <?php elseif ($log['result'] == 'loss'): ?>💔 Поражение
            <?php else: ?>🤝 Ничья
            <?php endif; ?>
        </td>
        <td><?= $log['bucks_earned'] ?></td>
        <td><?= $log['exp_earned'] ?></td>
        <td><?= date('d.m.Y H:i', strtotime($log['created_at'])) ?></td>
    </tr>
    <?php endforeach; ?>
</table>

<?php if ($total > $limit): ?>
<div style="margin-top:15px;">
    Страницы: 
    <?php for ($i = 1; $i <= ceil($total / $limit); $i++): ?>
        <a href="?page=<?= $i ?>" <?= ($i == $page) ? 'style="font-weight:bold;"' : '' ?>>
            [<?= $i ?>]
        </a>
    <?php endfor; ?>
</div>
<?php endif; ?>

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