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

$id = (int)($_GET['id'] ?? 0);
$stmt = $pdo->prepare("SELECT * FROM kelas WHERE id = ?");
$stmt->execute([$id]);
$kelas = $stmt->fetch();
if (!$kelas) { setFlash('error', 'Kelas tidak ditemukan.'); header('Location: ' . url('/admin/kelas/index.php')); exit; }

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (!validateCsrf()) { setFlash('error', 'CSRF error.'); header("Location: " . url("/admin/kelas/edit.php?id={$id}")); exit; }
    $pdo->prepare("UPDATE kelas SET nama_kelas=?, jenis_olahraga=?, instruktur=?, hari=?, jam_mulai=?, jam_selesai=?, kuota=?, sisa_slot=?, harga=?, intensitas=?, status=? WHERE id=?")
        ->execute([
            trim($_POST['nama_kelas']), $_POST['jenis_olahraga'], trim($_POST['instruktur']),
            $_POST['hari'], $_POST['jam_mulai'], $_POST['jam_selesai'],
            (int)$_POST['kuota'], (int)$_POST['sisa_slot'], (int)$_POST['harga'],
            $_POST['intensitas'], $_POST['status'], $id
        ]);
    setFlash('success', 'Kelas berhasil diupdate!'); 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">Edit <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" value="<?= htmlspecialchars($kelas['nama_kelas']) ?>" required></div>
        <div><label class="form-label">Jenis Olahraga</label><select name="jenis_olahraga" class="form-input" required><?php foreach($jenisList as $j): ?><option value="<?= $j ?>" <?= $kelas['jenis_olahraga']==$j?'selected':'' ?>><?= $j ?></option><?php endforeach; ?></select></div>
        <div><label class="form-label">Instruktur</label><input type="text" name="instruktur" class="form-input" value="<?= htmlspecialchars($kelas['instruktur']) ?>" required></div>
        <div><label class="form-label">Hari</label><select name="hari" class="form-input" required><?php foreach($hariList as $h): ?><option value="<?= $h ?>" <?= $kelas['hari']==$h?'selected':'' ?>><?= $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" value="<?= $kelas['jam_mulai'] ?>" required></div>
            <div><label class="form-label">Jam Selesai</label><input type="time" name="jam_selesai" class="form-input" value="<?= $kelas['jam_selesai'] ?>" 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" value="<?= $kelas['kuota'] ?>" required></div>
            <div><label class="form-label">Sisa Slot</label><input type="number" name="sisa_slot" class="form-input" min="0" value="<?= $kelas['sisa_slot'] ?>" required></div>
        </div>
        <div><label class="form-label">Harga (Rp)</label><input type="number" name="harga" class="form-input" min="0" value="<?= $kelas['harga'] ?>" required></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" <?= $kelas['intensitas']=='low'?'checked':'' ?>> 🟢 Low</label>
            <label class="flex items-center gap-2 text-sm"><input type="radio" name="intensitas" value="medium" <?= $kelas['intensitas']=='medium'?'checked':'' ?>> 🟡 Medium</label>
            <label class="flex items-center gap-2 text-sm"><input type="radio" name="intensitas" value="high" <?= $kelas['intensitas']=='high'?'checked':'' ?>> 🔴 High</label>
        </div></div>
        <div><label class="form-label">Status</label><select name="status" class="form-input"><option value="aktif" <?= $kelas['status']=='aktif'?'selected':'' ?>>Aktif</option><option value="nonaktif" <?= $kelas['status']=='nonaktif'?'selected':'' ?>>Nonaktif</option></select></div>
        <button type="submit" class="btn-volt w-full py-3">Update 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'; ?>
