<?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-person-badge me-2 text-primary"></i>
      <?= isset($isEdit) ? 'Edit Guru' : 'Tambah Guru' ?>
    </h1>
    <nav aria-label="breadcrumb">
      <ol class="breadcrumb">
        <li class="breadcrumb-item"><a href="<?= BASE_URL ?>/index.php?page=guru">Data Guru</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; ?>

  <?php
  // Load mapel list sekali di sini
  if (!class_exists('MapelModel')) {
      require_once __DIR__ . '/../../models/MapelModel.php';
  }
  $mapelModel     = new MapelModel();
  $mapelAll       = $mapelModel->getAllSimple();
  $currentMapelId = $guru['mapel_id'] ?? $data['mapel_id'] ?? '';
  // Fallback: cari dari kode teks lama jika mapel_id kosong
  if (!$currentMapelId && !empty($guru['mata_pelajaran'])) {
      foreach ($mapelAll as $mp) {
          if (strtoupper($mp['kode']) === strtoupper($guru['mata_pelajaran'])) {
              $currentMapelId = $mp['id'];
              break;
          }
      }
  }
  ?>

  <form method="POST" enctype="multipart/form-data" class="needs-validation" novalidate>
    <input type="hidden" name="csrf_token" value="<?= e($csrfToken) ?>">

    <div class="row g-3">

      <!-- ===== KOLOM KIRI ===== -->
      <div class="col-12 col-lg-8">

        <!-- Data Pribadi -->
        <div class="card mb-3">
          <div class="card-header">
            <h6><i class="bi bi-person-badge me-2 text-primary"></i>Data Guru</h6>
          </div>
          <div class="card-body">
            <div class="row g-3">

              <!-- NIP -->
              <div class="col-md-6">
                <label class="form-label">NIP</label>
                <input type="text" class="form-control" name="nip"
                       value="<?= e($guru['nip'] ?? $data['nip'] ?? '') ?>"
                       placeholder="Nomor Induk Pegawai">
              </div>

              <!-- Nama -->
              <div class="col-md-6">
                <label class="form-label">Nama Lengkap <span class="text-danger">*</span></label>
                <input type="text" class="form-control" name="nama" required
                       value="<?= e($guru['nama'] ?? $data['nama'] ?? '') ?>"
                       placeholder="Nama lengkap guru">
                <div class="invalid-feedback">Nama wajib diisi.</div>
              </div>

              <!-- Jenis Kelamin -->
              <div class="col-md-4">
                <label class="form-label">Jenis Kelamin <span class="text-danger">*</span></label>
                <select class="form-select" name="jenis_kelamin" required>
                  <option value="">-- Pilih --</option>
                  <?php foreach (['Laki-laki', 'Perempuan'] as $jk): ?>
                  <option value="<?= $jk ?>"
                    <?= ($guru['jenis_kelamin'] ?? $data['jenis_kelamin'] ?? '') === $jk ? 'selected' : '' ?>>
                    <?= $jk ?>
                  </option>
                  <?php endforeach; ?>
                </select>
                <div class="invalid-feedback">Jenis kelamin wajib dipilih.</div>
              </div>

              <!-- Tempat Lahir -->
              <div class="col-md-4">
                <label class="form-label">Tempat Lahir</label>
                <input type="text" class="form-control" name="tempat_lahir"
                       value="<?= e($guru['tempat_lahir'] ?? $data['tempat_lahir'] ?? '') ?>">
              </div>

              <!-- Tanggal Lahir -->
              <div class="col-md-4">
                <label class="form-label">Tanggal Lahir</label>
                <input type="date" class="form-control" name="tanggal_lahir"
                       value="<?= e($guru['tanggal_lahir'] ?? $data['tanggal_lahir'] ?? '') ?>">
              </div>

              <!-- Agama -->
              <div class="col-md-4">
                <label class="form-label">Agama</label>
                <select class="form-select" name="agama">
                  <option value="">-- Pilih --</option>
                  <?php foreach (['Islam','Kristen','Katolik','Hindu','Buddha','Konghucu'] as $ag): ?>
                  <option value="<?= $ag ?>"
                    <?= ($guru['agama'] ?? $data['agama'] ?? '') === $ag ? 'selected' : '' ?>>
                    <?= $ag ?>
                  </option>
                  <?php endforeach; ?>
                </select>
              </div>

              <!-- Pendidikan -->
              <div class="col-md-4">
                <label class="form-label">Pendidikan Terakhir</label>
                <select class="form-select" name="pendidikan">
                  <option value="">-- Pilih --</option>
                  <?php foreach (['SMA/SMK','D1','D2','D3','S1','S2','S3'] as $p): ?>
                  <option value="<?= $p ?>"
                    <?= ($guru['pendidikan'] ?? $data['pendidikan'] ?? '') === $p ? 'selected' : '' ?>>
                    <?= $p ?>
                  </option>
                  <?php endforeach; ?>
                </select>
              </div>

              <!-- Jurusan -->
              <div class="col-md-4">
                <label class="form-label">Jurusan Pendidikan</label>
                <input type="text" class="form-control" name="jurusan_pendidikan"
                       value="<?= e($guru['jurusan_pendidikan'] ?? $data['jurusan_pendidikan'] ?? '') ?>">
              </div>

              <!-- Mata Pelajaran (dropdown dari tabel mapel) -->
              <div class="col-md-6">
                <label class="form-label">Mata Pelajaran Diampu</label>
                <select class="form-select" name="mapel_id">
                  <option value="">-- Pilih Mata Pelajaran --</option>
                  <?php foreach ($mapelAll as $mp): ?>
                  <option value="<?= $mp['id'] ?>"
                    <?= $currentMapelId == $mp['id'] ? 'selected' : '' ?>>
                    <?= e($mp['kode']) ?><?= $mp['nama'] !== $mp['kode'] ? ' — ' . e($mp['nama']) : '' ?>
                  </option>
                  <?php endforeach; ?>
                </select>
              </div>

              <!-- Status Kepegawaian -->
              <div class="col-md-3">
                <label class="form-label">Status Kepegawaian</label>
                <select class="form-select" name="status_kepegawaian">
                  <?php foreach (['PNS','GTT','GTY','Honorer'] as $sk): ?>
                  <option value="<?= $sk ?>"
                    <?= ($guru['status_kepegawaian'] ?? $data['status_kepegawaian'] ?? 'GTT') === $sk ? 'selected' : '' ?>>
                    <?= $sk ?>
                  </option>
                  <?php endforeach; ?>
                </select>
              </div>

              <!-- Tahun Masuk -->
              <div class="col-md-3">
                <label class="form-label">Tahun Masuk</label>
                <input type="number" class="form-control" name="tahun_masuk"
                       min="1970" max="<?= date('Y') ?>"
                       value="<?= e($guru['tahun_masuk'] ?? $data['tahun_masuk'] ?? '') ?>"
                       placeholder="<?= date('Y') ?>">
              </div>

              <!-- No HP -->
              <div class="col-md-6">
                <label class="form-label">No. HP</label>
                <input type="text" class="form-control" name="no_hp"
                       value="<?= e($guru['no_hp'] ?? $data['no_hp'] ?? '') ?>"
                       placeholder="08xxxxxxxxxx">
              </div>

              <!-- Email -->
              <div class="col-md-6">
                <label class="form-label">Email</label>
                <input type="email" class="form-control" name="email"
                       value="<?= e($guru['email'] ?? $data['email'] ?? '') ?>"
                       placeholder="guru@sekolah.sch.id">
              </div>

              <!-- Alamat -->
              <div class="col-12">
                <label class="form-label">Alamat</label>
                <textarea class="form-control" name="alamat" rows="2"
                          placeholder="Alamat lengkap"><?= e($guru['alamat'] ?? $data['alamat'] ?? '') ?></textarea>
              </div>

              <!-- Status -->
              <div class="col-md-4">
                <label class="form-label">Status</label>
                <select class="form-select" name="status">
                  <option value="Aktif"
                    <?= ($guru['status'] ?? $data['status'] ?? 'Aktif') === 'Aktif' ? 'selected' : '' ?>>
                    Aktif
                  </option>
                  <option value="Tidak Aktif"
                    <?= ($guru['status'] ?? $data['status'] ?? '') === 'Tidak Aktif' ? 'selected' : '' ?>>
                    Tidak Aktif
                  </option>
                </select>
              </div>

            </div><!-- /row -->
          </div><!-- /card-body -->
        </div><!-- /card -->

      </div><!-- /col-lg-8 -->

      <!-- ===== KOLOM KANAN ===== -->
      <div class="col-12 col-lg-4">

        <!-- Foto -->
        <div class="card mb-3">
          <div class="card-header">
            <h6><i class="bi bi-camera me-2"></i>Foto Guru</h6>
          </div>
          <div class="card-body text-center">
            <img id="fotoPreview"
                 src="<?= !empty($guru['foto'])
                   ? BASE_URL . '/uploads/' . e($guru['foto'])
                   : 'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIxMjAiIGhlaWdodD0iMTIwIiB2aWV3Qm94PSIwIDAgMTIwIDEyMCI+PHJlY3Qgd2lkdGg9IjEyMCIgaGVpZ2h0PSIxMjAiIGZpbGw9IiNlMmU4ZjAiIHJ4PSI2MCIvPjxjaXJjbGUgY3g9IjYwIiBjeT0iNDUiIHI9IjIwIiBmaWxsPSIjOTRhM2I4Ii8+PGVsbGlwc2UgY3g9IjYwIiBjeT0iOTAiIHJ4PSIzMCIgcnk9IjIwIiBmaWxsPSIjOTRhM2I4Ii8+PC9zdmc+'
                 ?>"
                 class="rounded mb-3"
                 style="width:120px;height:120px;object-fit:cover;border:3px solid #e2e8f0;"
                 alt="Foto Guru">
            <div>
              <label class="btn btn-outline-primary btn-sm">
                <i class="bi bi-upload me-1"></i> Pilih Foto
                <input type="file" name="foto" accept="image/*"
                       class="d-none img-upload-input" data-preview="fotoPreview">
              </label>
              <div class="text-muted mt-2" style="font-size:.75rem;">JPG/PNG, maks 2MB</div>
            </div>
          </div>
        </div>

        <!-- Tombol Aksi -->
        <div class="card">
          <div class="card-body">
            <button type="submit" class="btn btn-primary w-100 mb-2">
              <i class="bi bi-save me-1"></i>
              <?= isset($isEdit) ? 'Simpan Perubahan' : 'Simpan Data' ?>
            </button>
            <a href="<?= BASE_URL ?>/index.php?page=guru" class="btn btn-outline-secondary w-100">
              <i class="bi bi-arrow-left me-1"></i> Kembali
            </a>
          </div>
        </div>

      </div><!-- /col-lg-4 -->

    </div><!-- /row -->
  </form>
</div><!-- /page-content -->

<?php include __DIR__ . '/../../views/layouts/footer.php'; ?>
</div><!-- /main-content -->
</div><!-- /wrapper -->
