<?php include_once __DIR__ . '/../layouts/head.php'; ?>
<div class="container py-4" style="max-width: 1350px; width: 100%;">
  <div class="row">
    <div class="col-lg-12">
      <div class="shadow-sm bg-white rounded overflow-hidden border">
        <div class="p-0">
          <div class="row px-0 m-0">
            <?php include_once __DIR__ . '/../layouts/menu_body.php'; ?>

            <div class="col-12 col-md-9 px-0">
              <div class="tab-content">
                <div class="tab-pane fade show active">
                  <div class="p-4">
                    <div class="mb-4">
                      <h5 class="mb-0 fw-bold">Biodata Mahasiswa</h5>
                    </div>
                        <?php if (!empty($_SESSION['flash'])): ?>
                        <script>
                            alert("<?= $_SESSION['flash']; ?>");
                        </script>
                        <?php unset($_SESSION['flash']); endif; ?>
                        <div class="row row-cols-1 row-cols-md-12 g-12">
                          <div class="col">
                            <div class="border rounded d-flex gap-12 p-12">
                              <div class="p-4">
                                <div class="mb-4">

<?php
// --- contoh validasi server-side ---
$errors = [];

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $requiredFields = [
        'nik' => 'nik',
        'jenis_kelamin' => 'jenis_kelamin',
        'rt' => 'RT',
        'rw' => 'RW',
        'nama_dusun' => 'Dusun',
        'kelurahan' => 'Kelurahan',
        'kecamatan' => 'Kecamatan',
        'email' => 'Email',
        'no_hp' => 'No HP',
        'tmpt_lahir' => 'Tempat Lahir',
        'tgl_lahir' => 'Tanggal Lahir',
        'agama' => 'Agama',
        'jenis_tinggal' => 'Jenis Tinggal',
        'alat_transportasi' => 'Alat Transportasi',
    ];

    foreach ($requiredFields as $field => $label) {
        if (empty(trim($_POST[$field] ?? ''))) {
            $errors[$field] = "$label wajib diisi.";
        }
    }
}
?>

<form action="/dashboard/maba-profile" method="POST">
  <div class="row row-cols-1 row-cols-md-2 g-4">

    <!-- NIK -->
    <div class="col">
      <label>NIK <span class="text-danger">*</span></label>
      <input 
        type="number" 
        name="nik" 
        class="form-control <?= isset($errors['nik']) ? 'is-invalid' : '' ?>" 
        value="<?= htmlspecialchars($mahasiswa['nik'] ?? '') ?>" 
        required
      >
      <?php if (isset($errors['nik'])): ?>
          <div class="invalid-feedback"><?= $errors['nik'] ?></div>
      <?php endif; ?>
    </div>

    <!-- jenis kelamin -->
    <div class="col">
      <label>Jenis Kelamin <span class="text-danger">*</span></label>
      <select 
        name="jenis_kelamin" 
        class="form-control <?= isset($errors['jenis_kelamin']) ? 'is-invalid' : '' ?>" 
        required
      >
        <option value="">-- Pilih --</option>
        <option value="L" <?= ($mahasiswa['jenis_kelamin'] ?? '') == 'L' ? 'selected' : '' ?>>Laki-laki</option>
        <option value="P" <?= ($mahasiswa['jenis_kelamin'] ?? '') == 'P' ? 'selected' : '' ?>>Perempuan</option>
      </select>
      <?php if (isset($errors['jenis_kelamin'])): ?>
          <div class="invalid-feedback"><?= $errors['jenis_kelamin'] ?></div>
      <?php endif; ?>
    </div>
    
    <!-- RT -->
    <div class="col">
      <label>RT <span class="text-danger">*</span></label>
      <input 
        type="number" 
        name="rt" 
        class="form-control <?= isset($errors['rt']) ? 'is-invalid' : '' ?>" 
        value="<?= htmlspecialchars($mahasiswa['rt'] ?? '') ?>" 
        required
      >
      <?php if (isset($errors['rt'])): ?>
          <div class="invalid-feedback"><?= $errors['rt'] ?></div>
      <?php endif; ?>
    </div>

    <!-- RW -->
    <div class="col">
      <label>RW <span class="text-danger">*</span></label>
      <input 
        type="number" 
        name="rw" 
        class="form-control <?= isset($errors['rw']) ? 'is-invalid' : '' ?>" 
        value="<?= htmlspecialchars($mahasiswa['rw'] ?? '') ?>" 
        required
      >
      <?php if (isset($errors['rw'])): ?>
          <div class="invalid-feedback"><?= $errors['rw'] ?></div>
      <?php endif; ?>
    </div>

    <!-- Dusun -->
    <div class="col">
      <label>Dusun <span class="text-danger">*</span></label>
      <input 
        type="text" 
        name="nama_dusun" 
        class="form-control <?= isset($errors['nama_dusun']) ? 'is-invalid' : '' ?>" 
        value="<?= htmlspecialchars($mahasiswa['nama_dusun'] ?? '') ?>" 
        required
      >
      <?php if (isset($errors['nama_dusun'])): ?>
          <div class="invalid-feedback"><?= $errors['nama_dusun'] ?></div>
      <?php endif; ?>
    </div>

    <!-- Kelurahan -->
    <div class="col">
      <label>Kelurahan <span class="text-danger">*</span></label>
      <input 
        type="text" 
        name="kelurahan" 
        class="form-control <?= isset($errors['kelurahan']) ? 'is-invalid' : '' ?>" 
        value="<?= htmlspecialchars($mahasiswa['kelurahan'] ?? '') ?>" 
        required
      >
      <?php if (isset($errors['kelurahan'])): ?>
          <div class="invalid-feedback"><?= $errors['kelurahan'] ?></div>
      <?php endif; ?>
    </div>

    <!-- Kecamatan -->
    <div class="col">
      <label>Kecamatan <span class="text-danger" style="font-size:0.60rem;">* Ketik misal “Menes”, lalu pilih kecamatan yang muncul.</span></label>
      <input 
        type="text" 
        id="kecamatan_input"
        class="form-control <?= isset($errors['kecamatan']) ? 'is-invalid' : '' ?>" 
        value="<?= htmlspecialchars($mahasiswa['kecamatan'] ?? '') ?>" 
        required
      >
      <input type="hidden" name="kecamatan" id="kecamatan_hidden" value="<?= htmlspecialchars($mahasiswa['kecamatan'] ?? '') ?>">
      <label><span class="text-defult" style="font-size:0.60rem;">Ketika sudah tersimpan maka kecamatan berubah jadi kode kecamatan.</span></label>    
      <div id="suggestions" class="list-group position-absolute w-50"></div>
    
      <?php if (isset($errors['kecamatan'])): ?>
          <div class="invalid-feedback"><?= $errors['kecamatan'] ?></div>
      <?php endif; ?>
    </div>
    
    <script>
    document.addEventListener("DOMContentLoaded", function() {
        const input = document.getElementById("kecamatan_input");
        const hidden = document.getElementById("kecamatan_hidden");
        const suggestions = document.getElementById("suggestions");
    
        input.addEventListener("keyup", function() {
            const q = this.value;
            if (q.length < 2) {
                suggestions.innerHTML = "";
                return;
            }
            fetch("/dashboard/maba-search-kecamatan?q=" + encodeURIComponent(q))
                .then(res => res.json())
                .then(data => {
                    suggestions.innerHTML = "";
                    data.forEach(item => {
                        const div = document.createElement("div");
                        div.classList.add("list-group-item", "list-group-item-action");
    
                        // tampilkan di dropdown: Kecamatan - Kabupaten
                        div.textContent = item.kecamatan + " - " + item.kabupaten;
    
                        // simpan id_wil untuk hidden input
                        div.dataset.id = item.id_wil;
    
                        div.addEventListener("click", function() {
                            // input text menampilkan gabungan biar user yakin pilihannya benar
                            input.value = item.kecamatan + " - " + item.kabupaten;
    
                            // hidden input tetap id_wil untuk disimpan ke DB
                            hidden.value = item.id_wil;
    
                            suggestions.innerHTML = "";
                        });
    
                        suggestions.appendChild(div);
                    });
                });
        });
    });
    </script>

    <!-- Kode Pos -->
    <div class="col">
      <label>Kode Pos</label>
      <input type="text" name="kode_pos" class="form-control" value="<?= htmlspecialchars($mahasiswa['kode_pos'] ?? '') ?>">
    </div>

    <!-- No HP -->
    <div class="col">
      <label>No. HP <span class="text-danger">*</span></label>
      <input 
        type="text" 
        name="no_hp" 
        class="form-control <?= isset($errors['no_hp']) ? 'is-invalid' : '' ?>" 
        value="<?= htmlspecialchars($mahasiswa['no_hp'] ?? '') ?>" 
        required
      >
      <?php if (isset($errors['no_hp'])): ?>
          <div class="invalid-feedback"><?= $errors['no_hp'] ?></div>
      <?php endif; ?>
    </div>

    <!-- Email -->
    <div class="col">
      <label>Email <span class="text-danger">*</span></label>
      <input 
        type="email" 
        name="email" 
        class="form-control <?= isset($errors['email']) ? 'is-invalid' : '' ?>" 
        value="<?= htmlspecialchars($mahasiswa['email'] ?? '') ?>" 
        required
      >
      <?php if (isset($errors['email'])): ?>
          <div class="invalid-feedback"><?= $errors['email'] ?></div>
      <?php endif; ?>
    </div>

    <!-- Tempat Lahir -->
    <div class="col">
      <label>Tempat Lahir <span class="text-danger">*</span></label>
      <input 
        type="text" 
        name="tmpt_lahir" 
        class="form-control <?= isset($errors['tmpt_lahir']) ? 'is-invalid' : '' ?>" 
        value="<?= htmlspecialchars($mahasiswa['tmpt_lahir'] ?? '') ?>" 
        required
      >
      <?php if (isset($errors['tmpt_lahir'])): ?>
          <div class="invalid-feedback"><?= $errors['tmpt_lahir'] ?></div>
      <?php endif; ?>
    </div>

    <!-- Tanggal Lahir -->
    <div class="col">
      <label>Tanggal Lahir <span class="text-danger">*</span></label>
      <input 
        type="date" 
        name="tgl_lahir" 
        class="form-control <?= isset($errors['tgl_lahir']) ? 'is-invalid' : '' ?>" 
        value="<?= htmlspecialchars($mahasiswa['tgl_lahir'] ?? '') ?>" 
        required
      >
      <?php if (isset($errors['tgl_lahir'])): ?>
          <div class="invalid-feedback"><?= $errors['tgl_lahir'] ?></div>
      <?php endif; ?>
    </div>

    <!-- Agama -->
    <div class="col">
      <label>Agama <span class="text-danger">*</span></label>
      <select 
        name="agama" 
        class="form-control <?= isset($errors['agama']) ? 'is-invalid' : '' ?>" 
        required
      >
        <option value="">-- Pilih --</option>
        <option value="1" <?= ($mahasiswa['agama'] ?? '') == '1' ? 'selected' : '' ?>>Islam</option>
        <option value="2" <?= ($mahasiswa['agama'] ?? '') == '2' ? 'selected' : '' ?>>Kristen</option>
        <option value="3" <?= ($mahasiswa['agama'] ?? '') == '3' ? 'selected' : '' ?>>Katholik</option>
        <option value="4" <?= ($mahasiswa['agama'] ?? '') == '4' ? 'selected' : '' ?>>Hindu</option>
        <option value="5" <?= ($mahasiswa['agama'] ?? '') == '5' ? 'selected' : '' ?>>Budha</option>
        <option value="6" <?= ($mahasiswa['agama'] ?? '') == '6' ? 'selected' : '' ?>>Konghuchu</option>
        <option value="99" <?= ($mahasiswa['agama'] ?? '') == '99' ? 'selected' : '' ?>>Lainnya</option>
      </select>
      <?php if (isset($errors['agama'])): ?>
          <div class="invalid-feedback"><?= $errors['agama'] ?></div>
      <?php endif; ?>
    </div>

    <!-- Jenis Tinggal -->
    <div class="col">
      <label>Jenis Tinggal <span class="text-danger">*</span></label>
      <select 
        name="jenis_tinggal" 
        class="form-control <?= isset($errors['jenis_tinggal']) ? 'is-invalid' : '' ?>" 
        required
      >
        <option value="">-- Pilih --</option>
        <option value="1" <?= ($mahasiswa['jenis_tinggal'] ?? '') == '1' ? 'selected' : '' ?>>Bersama Orangtua</option>
        <option value="2" <?= ($mahasiswa['jenis_tinggal'] ?? '') == '2' ? 'selected' : '' ?>>Wali</option>
        <option value="3" <?= ($mahasiswa['jenis_tinggal'] ?? '') == '3' ? 'selected' : '' ?>>Kost</option>
        <option value="4" <?= ($mahasiswa['jenis_tinggal'] ?? '') == '4' ? 'selected' : '' ?>>Asrama</option>
        <option value="5" <?= ($mahasiswa['jenis_tinggal'] ?? '') == '5' ? 'selected' : '' ?>>Panti Asuhan</option>
        <option value="99" <?= ($mahasiswa['jenis_tinggal'] ?? '') == '99' ? 'selected' : '' ?>>Lainnya</option>
      </select>
      <?php if (isset($errors['jenis_tinggal'])): ?>
          <div class="invalid-feedback"><?= $errors['jenis_tinggal'] ?></div>
      <?php endif; ?>
    </div>

    <!-- Alat Transportasi -->
    <div class="col">
      <label>Alat Transportasi <span class="text-danger">*</span></label>
      <select 
        name="alat_transportasi" 
        class="form-control <?= isset($errors['alat_transportasi']) ? 'is-invalid' : '' ?>" 
        required
      >
        <option value="">-- Pilih --</option>
        <option value="1" <?= ($mahasiswa['alat_transportasi'] ?? '') == '1' ? 'selected' : '' ?>>Jalan Kaki</option>
        <option value="3" <?= ($mahasiswa['alat_transportasi'] ?? '') == '3' ? 'selected' : '' ?>>Angkutan Umum</option>
        <option value="4" <?= ($mahasiswa['alat_transportasi'] ?? '') == '4' ? 'selected' : '' ?>>Antar Jemput</option>
        <option value="5" <?= ($mahasiswa['alat_transportasi'] ?? '') == '5' ? 'selected' : '' ?>>Kereta Api</option>
        <option value="6" <?= ($mahasiswa['alat_transportasi'] ?? '') == '6' ? 'selected' : '' ?>>Ojek</option>
        <option value="7" <?= ($mahasiswa['alat_transportasi'] ?? '') == '7' ? 'selected' : '' ?>>Becak</option>
        <option value="8" <?= ($mahasiswa['alat_transportasi'] ?? '') == '8' ? 'selected' : '' ?>>Perahu</option>
        <option value="11" <?= ($mahasiswa['alat_transportasi'] ?? '') == '11' ? 'selected' : '' ?>>Kuda</option>
        <option value="12" <?= ($mahasiswa['alat_transportasi'] ?? '') == '12' ? 'selected' : '' ?>>Sepeda</option>
        <option value="13" <?= ($mahasiswa['alat_transportasi'] ?? '') == '13' ? 'selected' : '' ?>>Sepeda Motor</option>
        <option value="14" <?= ($mahasiswa['alat_transportasi'] ?? '') == '14' ? 'selected' : '' ?>>Mobil</option>
        <option value="99" <?= ($mahasiswa['alat_transportasi'] ?? '') == '99' ? 'selected' : '' ?>>Lainnya</option>
      </select>
      <?php if (isset($errors['alat_transportasi'])): ?>
          <div class="invalid-feedback"><?= $errors['alat_transportasi'] ?></div>
      <?php endif; ?>
    </div>

  </div>

  <div class="mt-4">
    <button type="submit" class="btn btn-primary">Simpan Biodata</button>
  </div>
</form>


                                  
                                    </div>
                                  </div>
                                </div>
                            </div>                        
                        </div>
                  </div>
                </div>
              </div>
            </div>

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