<?php include __DIR__ . '/../../views/layouts/header.php'; ?>
<div class="wrapper">
<?php include __DIR__ . '/../../views/layouts/sidebar.php'; ?>
<div class="main-content">
<?php include __DIR__ . '/../../views/layouts/topbar.php'; ?>
<div class="page-content">
  <div class="page-header">
    <h1><i class="bi bi-building me-2 text-primary"></i><?= isset($isEdit) ? 'Edit Kelas' : 'Tambah Kelas' ?></h1>
  </div>
  <?php if (!empty($errors)): ?>
  <div class="alert alert-danger"><ul class="mb-0"><?php foreach ($errors as $e): ?><li><?= e($e) ?></li><?php endforeach; ?></ul></div>
  <?php endif; ?>
  <div class="row justify-content-center">
    <div class="col-12 col-lg-7">
      <div class="card">
        <div class="card-header"><h6>Form Kelas</h6></div>
        <div class="card-body">
          <form method="POST" class="needs-validation" novalidate>
            <input type="hidden" name="csrf_token" value="<?= e($csrfToken) ?>">
            <div class="row g-3">
              <div class="col-md-6">
                <label class="form-label">Nama Kelas <span class="text-danger">*</span></label>
                <input type="text" class="form-control" name="nama_kelas" required
                       value="<?= e($kelas['nama_kelas'] ?? $data['nama_kelas'] ?? '') ?>"
                       placeholder="Contoh: VII A">
              </div>
              <div class="col-md-3">
                <label class="form-label">Tingkat <span class="text-danger">*</span></label>
                <select class="form-select" name="tingkat" required>
                  <option value="">-- Pilih --</option>
                  <?php foreach (['VII','VIII','IX','X','XI','XII'] as $t): ?>
                  <option value="<?= $t ?>" <?= ($kelas['tingkat'] ?? $data['tingkat'] ?? '') === $t ? 'selected' : '' ?>><?= $t ?></option>
                  <?php endforeach; ?>
                </select>
              </div>
              <div class="col-md-3">
                <label class="form-label">Kapasitas</label>
                <input type="number" class="form-control" name="kapasitas" min="1" max="50"
                       value="<?= e($kelas['kapasitas'] ?? $data['kapasitas'] ?? 30) ?>">
              </div>
              <div class="col-md-6">
                <label class="form-label">Jurusan</label>
                <input type="text" class="form-control" name="jurusan"
                       value="<?= e($kelas['jurusan'] ?? $data['jurusan'] ?? '') ?>"
                       placeholder="Opsional">
              </div>
              <div class="col-md-6">
                <label class="form-label">Tahun Ajaran</label>
                <input type="text" class="form-control" name="tahun_ajaran"
                       value="<?= e($kelas['tahun_ajaran'] ?? $data['tahun_ajaran'] ?? '2024/2025') ?>"
                       placeholder="2024/2025">
              </div>
              <div class="col-12">
                <label class="form-label">Wali Kelas</label>
                <select class="form-select" name="wali_kelas_id">
                  <option value="">-- Pilih Wali Kelas --</option>
                  <?php foreach ($guruList as $g): ?>
                  <option value="<?= $g['id'] ?>"
                    <?= ($kelas['wali_kelas_id'] ?? $data['wali_kelas_id'] ?? '') == $g['id'] ? 'selected' : '' ?>>
                    <?= e($g['nama']) ?> <?= $g['nip'] ? '(' . e($g['nip']) . ')' : '' ?>
                  </option>
                  <?php endforeach; ?>
                </select>
              </div>
            </div>
            <div class="mt-4 d-flex gap-2">
              <button type="submit" class="btn btn-primary">
                <i class="bi bi-save me-1"></i> <?= isset($isEdit) ? 'Simpan Perubahan' : 'Simpan' ?>
              </button>
              <a href="<?= BASE_URL ?>/index.php?page=kelas" class="btn btn-outline-secondary">Batal</a>
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>
</div>
<?php include __DIR__ . '/../../views/layouts/footer.php'; ?>
</div>
</div>
