<?php
require_once __DIR__ . '/../includes/bootstrap.php';
require_once __DIR__ . '/_helpers.php';
require_admin();

$clans = DB::fetchAll(
    "SELECT c.*, u.name AS leader_name, (SELECT COUNT(*) FROM clan_members cm WHERE cm.clan_id = c.id) AS member_count
     FROM clans c
     LEFT JOIN users u ON c.leader_id = u.id
     ORDER BY c.created_at DESC LIMIT 100"
);

$page_title = __('admin_clans');
require __DIR__ . '/_header.php';
?>

<h1>🛡 <?= e(__('admin_clans')) ?></h1>

<?php if (empty($clans)): ?>
    <p class="text-muted text-center" style="padding:2rem;">—</p>
<?php else: ?>
<table class="tbl">
    <thead>
        <tr>
            <th>ID</th>
            <th>Tag</th>
            <th>Name</th>
            <th>Leader</th>
            <th>Members</th>
            <th>Gold</th>
            <th>EXP</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($clans as $c): ?>
        <tr>
            <td><?= (int)$c['id'] ?></td>
            <td><strong>[<?= e($c['tag']) ?>]</strong></td>
            <td><?= e($c['name']) ?></td>
            <td><?= e($c['leader_name'] ?? '-') ?></td>
            <td><?= (int)$c['member_count'] ?></td>
            <td><?= format_number((int)$c['gold']) ?></td>
            <td><?= format_number((int)$c['exp']) ?></td>
        </tr>
        <?php endforeach; ?>
    </tbody>
</table>
<?php endif; ?>

<?php require __DIR__ . '/_footer.php'; ?>
