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

$stmt = $pdo->query("SELECT * FROM kelas ORDER BY status DESC, nama_kelas");
$kelasList = $stmt->fetchAll();

require_once __DIR__ . '/../../includes/header.php';
?>
<div class="max-w-7xl mx-auto px-4 py-8">
    <div class="flex flex-wrap items-center justify-between gap-4 mb-8 animate-fadeUp">
        <div><h1 class="text-3xl font-black mb-1">Kelola <span class="text-volt">Kelas</span></h1><p class="text-muted text-sm">Tambah, edit, dan kelola semua kelas</p></div>
        <a href="<?= BASE_URL ?>/admin/kelas/tambah.php" class="btn-volt">+ Tambah Kelas</a>
    </div>

    <div class="card animate-fadeUp d2">
        <div class="overflow-x-auto">
            <table class="data-table">
                <thead><tr><th>Nama</th><th>Jenis</th><th>Instruktur</th><th>Hari</th><th>Jam</th><th>Kuota</th><th>Sisa</th><th>Harga</th><th>Level</th><th>Status</th><th>Aksi</th></tr></thead>
                <tbody>
                <?php foreach ($kelasList as $k): ?>
                <tr>
                    <td class="font-bold"><?= htmlspecialchars($k['nama_kelas']) ?></td>
                    <td><?= htmlspecialchars($k['jenis_olahraga']) ?></td>
                    <td class="text-muted"><?= htmlspecialchars($k['instruktur']) ?></td>
                    <td><?= $k['hari'] ?></td>
                    <td class="text-sm"><?= date('H:i', strtotime($k['jam_mulai'])) ?>-<?= date('H:i', strtotime($k['jam_selesai'])) ?></td>
                    <td class="text-center"><?= $k['kuota'] ?></td>
                    <td class="text-center font-bold"><?= $k['sisa_slot'] ?></td>
                    <td class="text-volt font-bold">Rp <?= number_format($k['harga'],0,',','.') ?></td>
                    <td><span class="badge-<?= $k['intensitas'] ?>"><?= ucfirst($k['intensitas']) ?></span></td>
                    <td><span class="badge-<?= $k['status'] === 'aktif' ? 'tervalidasi' : 'ditolak' ?>"><?= ucfirst($k['status']) ?></span></td>
                    <td class="whitespace-nowrap">
                        <a href="<?= BASE_URL ?>/admin/kelas/edit.php?id=<?= $k['id'] ?>" class="text-volt text-xs font-bold hover:underline mr-2">Edit</a>
                        <?php if ($k['status'] === 'aktif'): ?>
                        <a href="<?= BASE_URL ?>/admin/kelas/hapus.php?id=<?= $k['id'] ?>" class="text-red-400 text-xs font-bold hover:underline" onclick="return confirm('Nonaktifkan kelas ini?')">Nonaktifkan</a>
                        <?php endif; ?>
                    </td>
                </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>
<?php require_once __DIR__ . '/../../includes/footer.php'; ?>
