<?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-book me-2 text-primary"></i>
      <?= isset($isEdit) ? 'Edit Mata Pelajaran' : 'Tambah Mata Pelajaran' ?>
    </h1>
    <nav aria-label="breadcrumb">
      <ol class="breadcrumb">
        <li class="breadcrumb-item"><a href="<?= BASE_URL ?>/index.php?page=mapel">Mata Pelajaran</a></li>
        <li class="breadcrumb-item active"><?= isset($isEdit) ? 'Edit' : 'Tambah' ?></li>
      </ol>
    </nav>
  </div>

  <?php if (!empty($errors)): ?>
  <div class="alert alert-danger">
    <ul class="mb-0">
      <?php foreach ($errors as $err): ?>
        <li><?= e($err) ?></li>
      <?php endforeach; ?>
    </ul>
  </div>
  <?php endif; ?>

  <div class="row justify-content-center">
    <div class="col-12 col-lg-6">
      <div class="card">
        <div class="card-header">
          <h6 class="mb-0">
            <i class="bi bi-book me-2 text-primary"></i>
            Form Mata Pelajaran
          </h6>
        </div>
        <div class="card-body">
          <form method="POST" class="needs-validation" novalidate>
            <input type="hidden" name="csrf_token" value="<?= e($csrfToken) ?>">

            <div class="mb-3">
              <label class="form-label">
                Kode Mapel <span class="text-danger">*</span>
              </label>
              <input type="text"
                     class="form-control text-uppercase"
                     name="kode"
                     maxlength="10"
                     required
                     placeholder="Contoh: MTK, IPA, BIN"
                     value="<?= e($mapel['kode'] ?? $data['kode'] ?? '') ?>"
                     oninput="this.value = this.value.toUpperCase()">
              <div class="form-text">Kode singkat, maksimal 10 karakter (huruf kapital otomatis)</div>
              <div class="invalid-feedback">Kode mapel wajib diisi.</div>
            </div>

            <div class="mb-3">
              <label class="form-label">
                Nama Mata Pelajaran <span class="text-danger">*</span>
              </label>
              <input type="text"
                     class="form-control"
                     name="nama"
                     required
                     placeholder="Contoh: Matematika, Ilmu Pengetahuan Alam"
                     value="<?= e($mapel['nama'] ?? $data['nama'] ?? '') ?>">
              <div class="invalid-feedback">Nama mata pelajaran wajib diisi.</div>
            </div>

            <div class="mb-4">
              <label class="form-label">KKM</label>
              <input type="number"
                     class="form-control"
                     name="kkm"
                     min="0"
                     max="100"
                     value="<?= e($mapel['kkm'] ?? $data['kkm'] ?? 70) ?>">
              <div class="form-text">Kriteria Ketuntasan Minimal (0–100), default: 70</div>
            </div>

            <div class="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=mapel" class="btn btn-outline-secondary">
                <i class="bi bi-arrow-left me-1"></i> Batal
              </a>
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>
</div>

<?php include __DIR__ . '/../../views/layouts/footer.php'; ?>
</div>
</div>
