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

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (!validateCsrf()) { setFlash('error', 'CSRF error.'); header('Location: ' . url('/admin/kelas/tambah.php')); exit; }
    $nama = trim($_POST['nama_kelas'] ?? '');
    $jenis = $_POST['jenis_olahraga'] ?? '';
    $instruktur = trim($_POST['instruktur'] ?? '');
    $hari = $_POST['hari'] ?? '';
    $jamMulai = $_POST['jam_mulai'] ?? '';
    $jamSelesai = $_POST['jam_selesai'] ?? '';
    $kuota = (int)$_POST['kuota'];
    $harga = (int)$_POST['harga'];
    $intensitas = $_POST['intensitas'] ?? 'medium';
    $status = $_POST['status'] ?? 'aktif';

    if (empty($nama) || empty($jenis) || empty($instruktur) || empty($hari) || $kuota < 1 || $harga < 0) {
        setFlash('error', 'Semua field wajib diisi dengan benar.'); header('Location: ' . url('/admin/kelas/tambah.php')); exit;
    }

    $pdo->prepare("INSERT INTO kelas (nama_kelas, jenis_olahraga, instruktur, hari, jam_mulai, jam_selesai, kuota, sisa_slot, harga, intensitas, status) VALUES (?,?,?,?,?,?,?,?,?,?,?)")
        ->execute([$nama, $jenis, $instruktur, $hari, $jamMulai, $jamSelesai, $kuota, $kuota, $harga, $intensitas, $status]);
    setFlash('success', 'Kelas berhasil ditambahkan!'); header('Location: ' . url('/admin/kelas/index.php')); exit;
}

require_once __DIR__ . '/../../includes/header.php';
$jenisList = ['Yoga','Zumba','Muay Thai','Gym','Pilates','Spinning'];
$hariList = ['Senin','Selasa','Rabu','Kamis','Jumat','Sabtu','Minggu'];
?>
<div class="max-w-2xl mx-auto px-4 py-8">
    <div class="mb-8 animate-fadeUp"><h1 class="text-3xl font-black mb-2">Tambah <span class="text-volt">Kelas</span></h1></div>
    <form method="POST" class="card space-y-5 animate-fadeUp d1">
        <input type="hidden" name="csrf_token" value="<?= csrfToken() ?>">
        <div><label class="form-label">Nama Kelas</label><input type="text" name="nama_kelas" class="form-input" required></div>
        <div><label class="form-label">Jenis Olahraga</label><select name="jenis_olahraga" class="form-input" required><option value="">Pilih...</option><?php foreach($jenisList as $j): ?><option value="<?= $j ?>"><?= $j ?></option><?php endforeach; ?></select></div>
        <div><label class="form-label">Instruktur</label><input type="text" name="instruktur" class="form-input" required></div>
        <div><label class="form-label">Hari</label><select name="hari" class="form-input" required><option value="">Pilih...</option><?php foreach($hariList as $h): ?><option value="<?= $h ?>"><?= $h ?></option><?php endforeach; ?></select></div>
        <div class="grid grid-cols-2 gap-4">
            <div><label class="form-label">Jam Mulai</label><input type="time" name="jam_mulai" class="form-input" required></div>
            <div><label class="form-label">Jam Selesai</label><input type="time" name="jam_selesai" class="form-input" required></div>
        </div>
        <div class="grid grid-cols-2 gap-4">
            <div><label class="form-label">Kuota</label><input type="number" name="kuota" class="form-input" min="1" required></div>
            <div><label class="form-label">Harga (Rp)</label><input type="number" name="harga" class="form-input" min="0" required></div>
        </div>
        <div><label class="form-label">Intensitas</label><div class="flex gap-4">
            <label class="flex items-center gap-2 text-sm"><input type="radio" name="intensitas" value="low"> 🟢 Low</label>
            <label class="flex items-center gap-2 text-sm"><input type="radio" name="intensitas" value="medium" checked> 🟡 Medium</label>
            <label class="flex items-center gap-2 text-sm"><input type="radio" name="intensitas" value="high"> 🔴 High</label>
        </div></div>
        <div><label class="form-label">Status</label><select name="status" class="form-input"><option value="aktif">Aktif</option><option value="nonaktif">Nonaktif</option></select></div>
        <button type="submit" class="btn-volt w-full py-3">Simpan Kelas</button>
        <a href="<?= BASE_URL ?>/admin/kelas/index.php" class="block text-center text-sm text-muted hover:text-white">← Kembali</a>
    </form>
</div>
<?php require_once __DIR__ . '/../../includes/footer.php'; ?>
