<?php
session_start();
require_once 'config.php';
require_once 'functions.php';

requireLogin();
$user = getConnectedUser($pdo);
requireAdmin($user);

// Statistiques globales
$nbUsers   = $pdo->query("SELECT COUNT(*) FROM users")->fetchColumn();
$nbBets    = $pdo->query("SELECT COUNT(*) FROM bets")->fetchColumn();
$nbPending = $pdo->query("SELECT COUNT(*) FROM bets WHERE status='pending'")->fetchColumn();
$nbReports = $pdo->query("SELECT COUNT(*) FROM reports WHERE status='new'")->fetchColumn();
$nbCircles = $pdo->query("SELECT COUNT(*) FROM circles WHERE status='active'")->fetchColumn();
$nbMsg     = $pdo->query("SELECT COUNT(*) FROM messages")->fetchColumn();

include 'header.php';
?>

<div class="container mt-4">
    <a href="index.php" class="btn btn-outline-secondary btn-sm mb-3">&larr; Dashboard</a>
    <h5 class="oracle-text text-danger mb-4">️ PANNEAU ADMINISTRATEUR</h5>

    <!-- Cartes stats -->
    <div class="row g-3 mb-4">
        <?php
        $cards = [
            ['Membres',       $nbUsers,   'primary', 'fa-users',         'admin_users.php'],
            ['Gages total',   $nbBets,    'info',    'fa-list-check',    'admin_bets.php'],
            ['En attente',    $nbPending, 'warning', 'fa-clock',         'admin_bets.php'],
            ['Reports',       $nbReports, 'danger',  'fa-flag',          'admin_reports.php'],
            ['Cercles actifs',$nbCircles, 'success', 'fa-circle-nodes',  '#'],
            ['Messages',      $nbMsg,     'secondary','fa-comments',     '#'],
        ];
        foreach ($cards as $c): ?>
        <div class="col-6 col-md-4 col-lg-2">
            <a href="<?= $c[4] ?>" class="text-decoration-none">
                <div class="card bg-dark border-<?= $c[2] ?> text-center p-3 h-100">
                    <i class="fa-solid <?= $c[3] ?> text-<?= $c[2] ?> mb-2" style="font-size:1.4rem;"></i>
                    <div class="fw-bold text-white" style="font-size:1.3rem;"><?= number_format((int)$c[1]) ?></div>
                    <small class="text-white text-uppercase" style="font-size:0.65rem;"><?= $c[0] ?></small>
                </div>
            </a>
        </div>
        <?php endforeach; ?>
    </div>

    <!-- Raccourcis -->
    <div class="row g-3">
        <div class="col-md-3">
            <div class="card bg-dark border-secondary p-3">
                <h6 class="text-info small fw-bold mb-3"> Gestion</h6>
                <div class="d-grid gap-2">
                    <a href="admin_users.php"   class="btn btn-outline-primary btn-sm">Membres</a>
                    <a href="admin_bets.php"    class="btn btn-outline-info btn-sm">Gages</a>
                    <a href="admin_reports.php" class="btn btn-outline-danger btn-sm">Reports <?= $nbReports>0?"<span class='badge bg-danger'>{$nbReports}</span>":'' ?></a>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card bg-dark border-secondary p-3">
                <h6 class="text-warning small fw-bold mb-3">⚙️ Configuration</h6>
                <div class="d-grid gap-2">
                    <a href="admin_config.php"   class="btn btn-outline-warning btn-sm">Points & Config</a>
                    <a href="admin_droits.php"   class="btn btn-outline-danger btn-sm"><i class="fa-solid fa-shield-halved fa-xs me-1"></i>Droits d'accès</a>
                    <a href="admin_limites.php"  class="btn btn-outline-warning btn-sm"><i class="fa-solid fa-sliders fa-xs me-1"></i>Limites par grade</a>
                    <a href="admin_commandites.php" class="btn btn-sm fw-bold" style="background:rgba(212,175,55,0.15);border:1px solid #d4af37;color:#d4af37;"><i class="fa-solid fa-handshake fa-xs me-1"></i>Commanditaires</a>
                    <a href="admin_pricing.php" class="btn btn-outline-info btn-sm fw-bold"><i class="fa-solid fa-tag fa-xs me-1"></i>Prix & Forfaits</a>
                    <a href="admin_arbitrages.php" class="btn btn-outline-warning btn-sm fw-bold"><i class="fa-solid fa-scale-balanced fa-xs me-1"></i>Arbitrages</a>
                    <a href="admin_gage_rules.php" class="btn btn-outline-warning btn-sm fw-bold"><i class="fa-solid fa-gavel fa-xs me-1"></i>Règles des gages</a>
                    <a href="admin_terms.php" class="btn btn-outline-info btn-sm fw-bold"><i class="fa-solid fa-language fa-xs me-1"></i>Terminologie</a>
                    <a href="admin_emails.php"       class="btn btn-outline-info btn-sm fw-bold"><i class="fa-solid fa-envelope-open-text fa-xs me-1"></i>Templates courriels</a>
                    <a href="admin_chat_icons.php"   class="btn btn-outline-secondary btn-sm fw-bold"><i class="fa-solid fa-face-smile fa-xs me-1"></i>Icônes du chat</a>
                    <a href="admin_anomalies.php"    class="btn btn-outline-danger btn-sm fw-bold"><i class="fa-solid fa-triangle-exclamation fa-xs me-1"></i>Anomalies</a>
                    <a href="admin_notes.php"        class="btn btn-outline-info btn-sm fw-bold"><i class="fa-solid fa-clipboard-list fa-xs me-1"></i>Notes & Tâches</a>
                    <a href="admin_trophees.php" class="btn btn-outline-warning btn-sm">Trophées</a>
                    <a href="marquee_manage.php" class="btn btn-outline-secondary btn-sm">Bande défilante</a>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card bg-dark border-secondary p-3">
                <h6 class="text-success small fw-bold mb-3"> Boutique</h6>
                <div class="d-grid gap-2">
                    <a href="admin_store.php"    class="btn btn-outline-success btn-sm">Gérer la boutique</a>
                    <a href="store.php"          class="btn btn-outline-secondary btn-sm">Voir la boutique</a>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card bg-dark border-secondary p-3">
                <h6 class="text-danger small fw-bold mb-3">o Derniers reports</h6>
                <?php
                $rStmt = $pdo->query("SELECT r.*, u.username FROM reports r LEFT JOIN users u ON u.id=r.reporter_id WHERE r.status='new' ORDER BY r.created_at DESC LIMIT 4");
                $rep = $rStmt->fetchAll();
                if (empty($rep)): ?>
                    <small class="text-muted">Aucun report en attente.</small>
                <?php else: foreach ($rep as $r): ?>
                    <div class="mb-1 pb-1 border-bottom border-secondary">
                        <small class="text-warning" style="font-size:0.65rem;">
                            <?= htmlspecialchars($r['username'] ?? 'Visiteur') ?> —
                            <span class="text-danger"><?= htmlspecialchars($r['reason']) ?></span>
                        </small>
                    </div>
                <?php endforeach; endif; ?>
                <a href="admin_reports.php" class="btn btn-outline-danger btn-sm w-100 mt-2">Voir tout</a>
            </div>
        </div>
    </div>
</div>

<?php include 'footer.php'; ?>
