<?php
require_once __DIR__ . '/../config/database.php';
require_once __DIR__ . '/../includes/auth.php';
requireAdmin();

$totalKelas = $pdo->query("SELECT COUNT(*) FROM kelas WHERE status='aktif'")->fetchColumn();
$totalMember = $pdo->query("SELECT COUNT(*) FROM users WHERE role='member'")->fetchColumn();
$totalPending = $pdo->query("SELECT COUNT(*) FROM transaksi WHERE status='pending'")->fetchColumn();
$totalPendapatan = (int)$pdo->query("SELECT COALESCE(SUM(total_harga),0) FROM transaksi WHERE status='tervalidasi'")->fetchColumn();

$recentTrx = $pdo->query("
    SELECT t.*, u.nama, (SELECT COUNT(*) FROM detail_transaksi WHERE transaksi_id = t.id) as jml_kelas
    FROM transaksi t JOIN users u ON t.user_id = u.id
    ORDER BY t.created_at DESC LIMIT 10
")->fetchAll();

require_once __DIR__ . '/../includes/header.php';
?>
<div class="max-w-7xl mx-auto px-4 py-8">
    <div class="mb-8 animate-fadeUp"><h1 class="text-3xl font-black mb-2">Admin <span class="text-volt">Dashboard</span></h1><p class="text-muted text-sm">Statistik dan monitoring sistem</p></div>

    <!-- Stat Cards -->
    <div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-6 mb-10">
        <div class="stat-card animate-fadeUp d1">
            <p class="text-xs text-muted font-bold uppercase mb-2">Kelas Aktif</p>
            <p class="text-3xl font-black text-volt" data-count="<?= $totalKelas ?>">0</p>
        </div>
        <div class="stat-card animate-fadeUp d2">
            <p class="text-xs text-muted font-bold uppercase mb-2">Total Member</p>
            <p class="text-3xl font-black text-volt" data-count="<?= $totalMember ?>">0</p>
        </div>
        <div class="stat-card animate-fadeUp d3">
            <p class="text-xs text-muted font-bold uppercase mb-2">Transaksi Pending</p>
            <p class="text-3xl font-black" style="color:#fbbf24" data-count="<?= $totalPending ?>">0</p>
        </div>
        <div class="stat-card animate-fadeUp d4">
            <p class="text-xs text-muted font-bold uppercase mb-2">Pendapatan</p>
            <p class="text-2xl font-black text-volt">Rp <span data-count="<?= $totalPendapatan ?>">0</span></p>
        </div>
    </div>

    <!-- Recent Transactions -->
    <div class="card animate-fadeUp d5">
        <h2 class="text-lg font-bold mb-4">Transaksi Terbaru</h2>
        <div class="overflow-x-auto">
            <table class="data-table">
                <thead><tr><th>Member</th><th>Kelas</th><th>Total</th><th>Status</th><th>Tanggal</th></tr></thead>
                <tbody>
                <?php foreach ($recentTrx as $trx): ?>
                <tr>
                    <td class="font-bold"><?= htmlspecialchars($trx['nama']) ?></td>
                    <td><?= $trx['jml_kelas'] ?> kelas</td>
                    <td class="font-bold text-volt">Rp <?= number_format($trx['total_harga'],0,',','.') ?></td>
                    <td><span class="badge-<?= $trx['status'] ?>"><?= ucfirst($trx['status']) ?></span></td>
                    <td class="text-muted text-sm"><?= date('d M Y', strtotime($trx['created_at'])) ?></td>
                </tr>
                <?php endforeach; ?>
                <?php if (empty($recentTrx)): ?>
                <tr><td colspan="5" class="text-center text-muted py-8">Belum ada transaksi</td></tr>
                <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>
<?php require_once __DIR__ . '/../includes/footer.php'; ?>
