<?php
require_once '../../includes/functions.php';
requireAdmin();
$pageTitle = 'Tambah Kelas';
$activeMenu = 'kelas';
$pengaturan = getPengaturan();
$guruList = db_fetchAll("SELECT * FROM guru WHERE status='aktif' ORDER BY nama");
$data = [];
$errors = [];

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $data = $_POST;
    if (empty($data['nama_kelas'])) { $errors['nama_kelas'] = 'Nama kelas wajib diisi'; }
    if (empty($data['tingkat'])) { $errors['tingkat'] = 'Tingkat wajib dipilih'; }

    if (empty($errors)) {
        db_insert('kelas', [
            'nama_kelas' => sanitize($data['nama_kelas']),
            'tingkat' => $data['tingkat'],
            'wali_kelas_id' => $data['wali_kelas_id'] ?: null,
            'tahun_ajaran' => $pengaturan['tahun_ajaran_aktif'],
            'semester' => $pengaturan['semester_aktif'],
            'kapasitas' => intval($data['kapasitas'] ?? 32),
        ]);
        setFlash('success', 'Kelas berhasil ditambahkan!');
        header('Location: list.php'); exit;
    }
}

include '../../templates/header.php';
?>
<div class="card" style="max-width:600px;">
  <div class="card-header">
    <span class="card-title">Tambah Kelas Baru</span>
    <a href="list.php" class="btn btn-outline btn-sm"><i class="fas fa-arrow-left"></i> Kembali</a>
  </div>
  <div class="card-body">
    <?php if (!empty($errors)): ?>
    <div class="alert alert-danger"><i class="fas fa-exclamation-circle"></i> <?= implode(', ', $errors) ?></div>
    <?php endif; ?>
    <form method="POST">
      <div class="form-row cols-2">
        <div class="form-group">
          <label class="form-label">Nama Kelas <span class="required">*</span></label>
          <input type="text" name="nama_kelas" class="form-control" value="<?= htmlspecialchars($data['nama_kelas'] ?? '') ?>" placeholder="e.g. 7A, 8B, 9C" required>
        </div>
        <div class="form-group">
          <label class="form-label">Tingkat <span class="required">*</span></label>
          <select name="tingkat" class="form-control" required>
            <option value="">-- Pilih --</option>
            <option value="7" <?= ($data['tingkat'] ?? '') === '7' ? 'selected' : '' ?>>Kelas 7</option>
            <option value="8" <?= ($data['tingkat'] ?? '') === '8' ? 'selected' : '' ?>>Kelas 8</option>
            <option value="9" <?= ($data['tingkat'] ?? '') === '9' ? 'selected' : '' ?>>Kelas 9</option>
          </select>
        </div>
      </div>
      <div class="form-row cols-2">
        <div class="form-group">
          <label class="form-label">Wali Kelas</label>
          <select name="wali_kelas_id" class="form-control">
            <option value="">-- Pilih Wali Kelas --</option>
            <?php foreach ($guruList as $g): ?>
            <option value="<?= $g['id'] ?>" <?= ($data['wali_kelas_id'] ?? '') == $g['id'] ? 'selected' : '' ?>><?= htmlspecialchars($g['nama']) ?></option>
            <?php endforeach; ?>
          </select>
        </div>
        <div class="form-group">
          <label class="form-label">Kapasitas Siswa</label>
          <input type="number" name="kapasitas" class="form-control" value="<?= $data['kapasitas'] ?? 32 ?>" min="1" max="50">
        </div>
      </div>
      <div class="form-group">
        <label class="form-label">Tahun Ajaran / Semester</label>
        <input type="text" class="form-control" value="<?= $pengaturan['tahun_ajaran_aktif'] ?> / Semester <?= $pengaturan['semester_aktif'] ?>" disabled>
      </div>
      <div style="display:flex;gap:10px;">
        <button type="submit" class="btn btn-primary"><i class="fas fa-save"></i> Simpan</button>
        <a href="list.php" class="btn btn-outline">Batal</a>
      </div>
    </form>
  </div>
</div>
<?php include '../../templates/footer.php'; ?>
