<?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-<?= isset($isEdit) ? 'gear' : 'plus' ?>-fill me-2 text-primary"></i>
      <?= isset($isEdit) ? 'Edit Siswa' : 'Tambah Siswa' ?>
    </h1>
    <nav aria-label="breadcrumb">
      <ol class="breadcrumb">
        <li class="breadcrumb-item"><a href="<?= BASE_URL ?>/index.php?page=dashboard">Dashboard</a></li>
        <li class="breadcrumb-item"><a href="<?= BASE_URL ?>/index.php?page=siswa">Data Siswa</a></li>
        <li class="breadcrumb-item active"><?= isset($isEdit) ? 'Edit' : 'Tambah' ?></li>
      </ol>
    </nav>
  </div>

  <?php if (!empty($errors)): ?>
  <div class="alert alert-danger">
    <i class="bi bi-exclamation-triangle-fill me-2"></i>
    <strong>Terdapat kesalahan:</strong>
    <ul class="mb-0 mt-1">
      <?php foreach ($errors as $err): ?>
        <li><?= e($err) ?></li>
      <?php endforeach; ?>
    </ul>
  </div>
  <?php endif; ?>

  <form method="POST" action="" 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 Pokok Siswa -->
        <div class="card mb-3">
          <div class="card-header">
            <h6><i class="bi bi-person-fill me-2 text-primary"></i>Data Pokok Siswa</h6>
          </div>
          <div class="card-body">
            <div class="row g-3">
              <div class="col-md-4">
                <label class="form-label">No. Urut</label>
                <input type="number" class="form-control" name="no"
                       value="<?= e($siswa['no'] ?? $data['no'] ?? '') ?>">
              </div>
              <div class="col-md-4">
                <label class="form-label">NIS</label>
                <input type="text" class="form-control" name="nis"
                       value="<?= e($siswa['nis'] ?? $data['nis'] ?? '') ?>"
                       placeholder="Nomor Induk Siswa">
              </div>
              <div class="col-md-4">
                <label class="form-label">NISN</label>
                <input type="text" class="form-control" name="nisn"
                       value="<?= e($siswa['nisn'] ?? $data['nisn'] ?? '') ?>"
                       placeholder="Nomor Induk Siswa Nasional">
              </div>
              <div class="col-md-6">
                <label class="form-label">NIK</label>
                <input type="text" class="form-control" name="nik"
                       value="<?= e($siswa['nik'] ?? $data['nik'] ?? '') ?>"
                       placeholder="Nomor Induk Kependudukan" maxlength="16">
              </div>
              <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($siswa['nama'] ?? $data['nama'] ?? '') ?>"
                       placeholder="Nama lengkap siswa">
                <div class="invalid-feedback">Nama wajib diisi.</div>
              </div>
              <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 ?>"
                    <?= ($siswa['jenis_kelamin'] ?? $data['jenis_kelamin'] ?? '') === $jk ? 'selected' : '' ?>>
                    <?= $jk ?>
                  </option>
                  <?php endforeach; ?>
                </select>
              </div>
              <div class="col-md-4">
                <label class="form-label">Kelas</label>
                <select class="form-select" name="kelas_id">
                  <option value="">-- Pilih Kelas --</option>
                  <?php foreach ($kelasList as $k): ?>
                  <option value="<?= $k['id'] ?>"
                    <?= ($siswa['kelas_id'] ?? $data['kelas_id'] ?? '') == $k['id'] ? 'selected' : '' ?>>
                    <?= e($k['nama_kelas']) ?>
                  </option>
                  <?php endforeach; ?>
                </select>
              </div>
              <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 ?>"
                    <?= ($siswa['agama'] ?? $data['agama'] ?? '') === $ag ? 'selected' : '' ?>>
                    <?= $ag ?>
                  </option>
                  <?php endforeach; ?>
                </select>
              </div>
              <div class="col-md-4">
                <label class="form-label">Tempat Lahir</label>
                <input type="text" class="form-control" name="tempat_lahir"
                       value="<?= e($siswa['tempat_lahir'] ?? $data['tempat_lahir'] ?? '') ?>">
              </div>
              <div class="col-md-4">
                <label class="form-label">Tanggal Lahir</label>
                <input type="date" class="form-control" name="tanggal_lahir"
                       value="<?= e($siswa['tanggal_lahir'] ?? $data['tanggal_lahir'] ?? '') ?>">
              </div>
              <div class="col-md-4">
                <label class="form-label">Anak Ke</label>
                <input type="number" class="form-control" name="anak_ke" min="1"
                       value="<?= e($siswa['anak_ke'] ?? $data['anak_ke'] ?? '') ?>">
              </div>
              <div class="col-md-4">
                <label class="form-label">Tanggal Masuk</label>
                <input type="date" class="form-control" name="tgl_masuk"
                       value="<?= e($siswa['tgl_masuk'] ?? $data['tgl_masuk'] ?? '') ?>">
              </div>
              <div class="col-md-4">
                <label class="form-label">Asal Sekolah</label>
                <input type="text" class="form-control" name="asal_sekolah"
                       value="<?= e($siswa['asal_sekolah'] ?? $data['asal_sekolah'] ?? '') ?>">
              </div>
              <div class="col-md-4">
                <label class="form-label">Status</label>
                <select class="form-select" name="status">
                  <?php foreach (['Aktif','Gaib','Tidak Aktif','Lulus','Pindah','Keluar'] as $st): ?>
                  <option value="<?= $st ?>"
                    <?= ($siswa['status'] ?? $data['status'] ?? 'Aktif') === $st ? 'selected' : '' ?>>
                    <?= $st ?>
                  </option>
                  <?php endforeach; ?>
                </select>
                <?php if (($siswa['status'] ?? '') === 'Gaib' || ($data['status'] ?? '') === 'Gaib'): ?>
                <div class="form-text text-warning">
                  <i class="bi bi-exclamation-triangle me-1"></i>
                  Siswa Gaib: terdaftar aktif di data namun tidak hadir/tidak diketahui keberadaannya.
                </div>
                <?php endif; ?>
              </div>
            </div>
          </div>
        </div>

        <!-- Alamat -->
        <div class="card mb-3">
          <div class="card-header">
            <h6><i class="bi bi-geo-alt-fill me-2 text-success"></i>Alamat Siswa</h6>
          </div>
          <div class="card-body">
            <div class="row g-3">
              <div class="col-12">
                <label class="form-label">Alamat Lengkap</label>
                <textarea class="form-control" name="alamat" rows="2"><?= e($alamat['alamat'] ?? '') ?></textarea>
              </div>
              <div class="col-md-2">
                <label class="form-label">RT</label>
                <input type="text" class="form-control" name="rt" value="<?= e($alamat['rt'] ?? '') ?>">
              </div>
              <div class="col-md-2">
                <label class="form-label">RW</label>
                <input type="text" class="form-control" name="rw" value="<?= e($alamat['rw'] ?? '') ?>">
              </div>
              <div class="col-md-4">
                <label class="form-label">Dusun</label>
                <input type="text" class="form-control" name="dusun" value="<?= e($alamat['dusun'] ?? '') ?>">
              </div>
              <div class="col-md-4">
                <label class="form-label">Kelurahan/Desa</label>
                <input type="text" class="form-control" name="kelurahan" value="<?= e($alamat['kelurahan'] ?? '') ?>">
              </div>
              <div class="col-md-4">
                <label class="form-label">Kecamatan</label>
                <input type="text" class="form-control" name="kecamatan" value="<?= e($alamat['kecamatan'] ?? '') ?>">
              </div>
              <div class="col-md-4">
                <label class="form-label">Kabupaten/Kota</label>
                <input type="text" class="form-control" name="kabupaten" value="<?= e($alamat['kabupaten'] ?? '') ?>">
              </div>
              <div class="col-md-4">
                <label class="form-label">Provinsi</label>
                <input type="text" class="form-control" name="provinsi" value="<?= e($alamat['provinsi'] ?? '') ?>">
              </div>
            </div>
          </div>
        </div>

        <!-- Orang Tua -->
        <div class="card mb-3">
          <div class="card-header">
            <h6><i class="bi bi-people me-2 text-warning"></i>Data Orang Tua</h6>
          </div>
          <div class="card-body">
            <div class="row g-3">
              <div class="col-md-6">
                <label class="form-label">Nama Ayah</label>
                <input type="text" class="form-control" name="nama_ayah" value="<?= e($orangTua['nama_ayah'] ?? '') ?>">
              </div>
              <div class="col-md-6">
                <label class="form-label">Nama Ibu</label>
                <input type="text" class="form-control" name="nama_ibu" value="<?= e($orangTua['nama_ibu'] ?? '') ?>">
              </div>
              <div class="col-md-6">
                <label class="form-label">Pekerjaan Ayah</label>
                <input type="text" class="form-control" name="pekerjaan_ayah" value="<?= e($orangTua['pekerjaan_ayah'] ?? '') ?>">
              </div>
              <div class="col-md-6">
                <label class="form-label">Pekerjaan Ibu</label>
                <input type="text" class="form-control" name="pekerjaan_ibu" value="<?= e($orangTua['pekerjaan_ibu'] ?? '') ?>">
              </div>
              <div class="col-md-6">
                <label class="form-label">No. HP Ayah</label>
                <input type="text" class="form-control" name="no_hp_ayah" value="<?= e($orangTua['no_hp_ayah'] ?? '') ?>">
              </div>
              <div class="col-md-6">
                <label class="form-label">No. HP Ibu</label>
                <input type="text" class="form-control" name="no_hp_ibu" value="<?= e($orangTua['no_hp_ibu'] ?? '') ?>">
              </div>
              <div class="col-md-6">
                <label class="form-label">Penghasilan</label>
                <input type="text" class="form-control" name="penghasilan" value="<?= e($orangTua['penghasilan'] ?? '') ?>" placeholder="Contoh: Rp 3.000.000">
              </div>
            </div>
          </div>
        </div>

        <!-- Wali -->
        <div class="card mb-3">
          <div class="card-header">
            <h6><i class="bi bi-person-check me-2 text-info"></i>Data Wali</h6>
          </div>
          <div class="card-body">
            <div class="row g-3">
              <div class="col-md-6">
                <label class="form-label">Nama Wali</label>
                <input type="text" class="form-control" name="nama_wali" value="<?= e($wali['nama_wali'] ?? '') ?>">
              </div>
              <div class="col-md-6">
                <label class="form-label">Hubungan</label>
                <input type="text" class="form-control" name="hubungan" value="<?= e($wali['hubungan'] ?? '') ?>" placeholder="Paman, Bibi, dll">
              </div>
              <div class="col-md-6">
                <label class="form-label">Pekerjaan Wali</label>
                <input type="text" class="form-control" name="pekerjaan_wali" value="<?= e($wali['pekerjaan_wali'] ?? '') ?>">
              </div>
              <div class="col-md-6">
                <label class="form-label">No. HP Wali</label>
                <input type="text" class="form-control" name="no_hp_wali" value="<?= e($wali['no_hp_wali'] ?? '') ?>">
              </div>
              <div class="col-12">
                <label class="form-label">Alamat Wali</label>
                <textarea class="form-control" name="alamat_wali" rows="2"><?= e($wali['alamat_wali'] ?? '') ?></textarea>
              </div>
            </div>
          </div>
        </div>

      </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 Siswa</h6>
          </div>
          <div class="card-body text-center">
            <img id="fotoPreview"
                 src="<?= !empty($siswa['foto']) ? BASE_URL . '/uploads/' . e($siswa['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 Siswa">
            <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, max 2MB</div>
            </div>
          </div>
        </div>

        <!-- 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=siswa" class="btn btn-outline-secondary w-100">
              <i class="bi bi-arrow-left me-1"></i> Kembali
            </a>
          </div>
        </div>
      </div>
    </div>
  </form>
</div>

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