<?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" id="v-pills-tabContent">

                <div class="tab-pane fade show active" id="v-pills-profile" role="tabpanel" aria-labelledby="v-pills-profile-tab">

                  <?php if (!$mahasiswa): ?>
                    <div class="alert alert-danger">Data mahasiswa tidak ditemukan.</div>
                  <?php else: ?>
                  <div class="p-4">
                    <div class="mb-4">
                      <h5 class="mb-0 fw-bold">Profile Mahasiswa</h5>
                    </div>

                    <!-- Data Pribadi -->
                    <div class="card shadow-sm mb-4">
                      <div class="card-header bg-primary text-white">Data Pribadi</div>
                      <div class="card-body">
                        <form method="POST" action="/dashboard/mahasiswa-update-pribadi">
                          <div class="row">
                            <div class="col-md-6 mb-3">
                              <label for="nama" class="form-label">Nama</label>
                              <input type="text" class="form-control" value="<?= strtoupper(htmlspecialchars($mahasiswa['nama'])) ?>" disabled>
                              <input type="hidden" name="nama" value="<?= strtoupper(htmlspecialchars($mahasiswa['nama'])) ?>">
                            </div>
                            <div class="col-md-6 mb-3">
                              <label for="nik" class="form-label">NIK</label>
                              <input type="text" class="form-control" id="nik" name="nik" value="<?= htmlspecialchars($mahasiswa['nik'] ?? '') ?>">
                            </div>
                            <div class="col-md-6 mb-3">
                              <label for="tmpt_lahir" class="form-label">Tempat Lahir</label>
                              <input type="text" class="form-control" id="tmpt_lahir" name="tmpt_lahir" value="<?= htmlspecialchars($mahasiswa['tmpt_lahir'] ?? '') ?>">
                            </div>
                            <div class="col-md-6 mb-3">
                              <label for="tgl_lahir" class="form-label">Tanggal Lahir</label>
                              <input type="date" class="form-control" id="tgl_lahir" name="tgl_lahir" value="<?= htmlspecialchars($mahasiswa['tgl_lahir'] ?? '') ?>">
                            </div>
                            <div class="col-md-6 mb-3">
                              <label for="agama" class="form-label">Agama</label>
                              <select class="form-select" id="agama" name="agama">
                                <?php
                                $agamaku = ['Islam', 'Kristen', 'Katolik', 'Hindu', 'Buddha', 'Konghucu', 'Lainnya'];
                                foreach ($agamaku as $agama) {
                                  $selected = ($mahasiswa['agama'] == $agama) ? 'selected' : '';
                                  echo "<option value='$agama' $selected>$agama</option>";
                                }
                                ?>
                              </select>
                            </div>
                            <div class="col-md-6 mb-3">
                              <label for="jenis_kelamin" class="form-label">Jenis Kelamin</label>
                              <select class="form-select" id="jenis_kelamin" name="jenis_kelamin">
                                <option value="L" <?= ($mahasiswa['jenis_kelamin'] ?? '') == 'L' ? 'selected' : '' ?>>Laki-laki</option>
                                <option value="P" <?= ($mahasiswa['jenis_kelamin'] ?? '') == 'P' ? 'selected' : '' ?>>Perempuan</option>
                              </select>
                            </div>
                            <div class="col-md-6 mb-3">
                              <label for="npwp" class="form-label">NPWP</label>
                              <input type="text" class="form-control" id="npwp" name="npwp" value="<?= htmlspecialchars($mahasiswa['npwp'] ?? '') ?>">
                            </div>
                          </div>
                          <button type="submit" class="btn btn-primary">Update Data Pribadi</button>
                        </form>
                      </div>
                    </div>

                    <!-- Alamat & Kontak -->
                    <div class="card shadow-sm mb-4">
                      <div class="card-header bg-success text-white">Alamat & Kontak</div>
                      <div class="card-body">
                        <form method="POST" action="/dashboard/mahasiswa-update-alamat">
                          <div class="row">
                            <div class="col-md-6 mb-3">
                              <label for="jalan" class="form-label">Jalan</label>
                              <input type="text" class="form-control" id="jalan" name="jalan" value="<?= htmlspecialchars($mahasiswa['jalan'] ?? '') ?>">
                            </div>
                            <div class="col-md-2 mb-3">
                              <label for="rt" class="form-label">RT</label>
                              <input type="text" class="form-control" id="rt" name="rt" value="<?= htmlspecialchars($mahasiswa['rt'] ?? '') ?>">
                            </div>
                            <div class="col-md-2 mb-3">
                              <label for="rw" class="form-label">RW</label>
                              <input type="text" class="form-control" id="rw" name="rw" value="<?= htmlspecialchars($mahasiswa['rw'] ?? '') ?>">
                            </div>
                            <div class="col-md-2 mb-3">
                              <label for="kode_pos" class="form-label">Kode Pos</label>
                              <input type="text" class="form-control" id="kode_pos" name="kode_pos" value="<?= htmlspecialchars($mahasiswa['kode_pos'] ?? '') ?>">
                            </div>
                            <div class="col-md-6 mb-3">
                              <label for="nama_dusun" class="form-label">Dusun</label>
                              <input type="text" class="form-control" id="nama_dusun" name="nama_dusun" value="<?= htmlspecialchars($mahasiswa['nama_dusun'] ?? '') ?>">
                            </div>
                            <div class="col-md-6 mb-3">
                              <label for="kelurahan" class="form-label">Kelurahan</label>
                              <input type="text" class="form-control" id="kelurahan" name="kelurahan" value="<?= htmlspecialchars($mahasiswa['kelurahan'] ?? '') ?>">
                            </div>
                            <div class="col-md-6 mb-3 position-relative">
                              <label for="kecamatan_input" class="form-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_display'] ?? '') ?>" 
                                  autocomplete="off"
                                  required
                                >
                                
                                <input type="hidden" name="kecamatan" id="kecamatan_hidden" 
                                       value="<?= htmlspecialchars($mahasiswa['kecamatan'] ?? '') ?>">
                            
                              <div id="suggestions" class="list-group position-absolute w-100 shadow-sm" style="z-index: 1000;"></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>

                            <div class="col-md-6 mb-3">
                              <label class="form-label">Jenis Tinggal</label>
                              <select class="form-select" name="jenis_tinggal">
                                <?php
                                $jenisTinggal = [1=>"Orang Tua",2=>"Wali",3=>"Kost",4=>"Asrama",5=>"Panti Asuhan",99=>"Lainnya"];
                                foreach ($jenisTinggal as $id=>$nama) {
                                  $sel = ($mahasiswa['jenis_tinggal']==$id)?'selected':'';
                                  echo "<option value='$id' $sel>$nama</option>";
                                }
                                ?>
                              </select>
                            </div>
                            <div class="col-md-6 mb-3">
                              <label class="form-label">Alat Transportasi</label>
                              <select class="form-select" name="alat_transportasi">
                                <?php
                                $transport = [1=>"Jalan Kaki",3=>"Angkutan umum",4=>"Bus Jemputan",5=>"Kereta Api",6=>"Ojek",7=>"Becak/Andong",8=>"Perahu",11=>"Kuda",12=>"Sepeda",13=>"Sepeda Motor",14=>"Mobil Pribadi",99=>"Lainnya"];
                                foreach ($transport as $id=>$nama) {
                                  $sel = ($mahasiswa['alat_transportasi']==$id)?'selected':'';
                                  echo "<option value='$id' $sel>$nama</option>";
                                }
                                ?>
                              </select>
                            </div>
                            <div class="col-md-6 mb-3">
                              <label for="telp_rumah" class="form-label">Telp Rumah</label>
                              <input type="text" class="form-control" id="telp_rumah" name="telp_rumah" value="<?= htmlspecialchars($mahasiswa['telp_rumah'] ?? '') ?>">
                            </div>
                            <div class="col-md-6 mb-3">
                              <label for="no_hp" class="form-label">No HP</label>
                              <input type="text" class="form-control" id="no_hp" name="no_hp" value="<?= htmlspecialchars($mahasiswa['no_hp'] ?? '') ?>">
                            </div>
                            <div class="col-md-6 mb-3">
                              <label for="email" class="form-label">Email</label>
                              <input type="email" class="form-control" id="email" name="email" value="<?= htmlspecialchars($mahasiswa['email'] ?? '') ?>">
                            </div>
                            <div class="col-md-6 mb-3">
                              <label class="form-label">Penerima KPS</label>
                              <select class="form-select" name="terima_kps">
                                <option value="" <?= !$mahasiswa['terima_kps']?'selected':'' ?>>-</option>
                                <option value="1" <?= $mahasiswa['terima_kps']=='1'?'selected':'' ?>>Ya</option>
                                <option value="0" <?= $mahasiswa['terima_kps']=='0'?'selected':'' ?>>Tidak</option>
                              </select>
                            </div>
                            <div class="col-md-6 mb-3">
                              <label for="no_kps" class="form-label">No KPS</label>
                              <input type="text" class="form-control" id="no_kps" name="no_kps" value="<?= htmlspecialchars($mahasiswa['no_kps'] ?? '') ?>">
                            </div>
                          </div>
                          <button type="submit" class="btn btn-success">Update Alamat & Kontak</button>
                        </form>
                      </div>
                    </div>

                    <!-- Data Orang Tua / Wali -->
                    <div class="card shadow-sm mb-4">
                      <div class="card-header bg-warning text-dark">Data Orang Tua / Wali</div>
                      <div class="card-body">
                        <form method="POST" action="/dashboard/mahasiswa-update-ortu">
                          <div class="row">
                            <!-- Ayah -->
                            <h6 class="fw-bold">Data Ayah</h6>
                            <div class="col-md-6 mb-3">
                              <label for="nik_ayah" class="form-label">NIK Ayah</label>
                              <input type="text" class="form-control" id="nik_ayah" name="nik_ayah" value="<?= htmlspecialchars($mahasiswa['nik_ayah'] ?? '') ?>">
                            </div>
                            <div class="col-md-6 mb-3">
                              <label for="nama_ayah" class="form-label">Nama Ayah</label>
                              <input type="text" class="form-control" id="nama_ayah" name="nama_ayah" 
                                               value="<?= htmlspecialchars($mahasiswa['nama_ayah'] ?? '') ?>" 
                                               style="text-transform: uppercase;">
                            </div>
                            <div class="col-md-6 mb-3">
                              <label for="tgl_lahir_ayah" class="form-label">Tanggal Lahir Ayah</label>
                              <input type="date" class="form-control" id="tgl_lahir_ayah" name="tgl_lahir_ayah" value="<?= htmlspecialchars($mahasiswa['tgl_lahir_ayah'] ?? '') ?>">
                            </div>
                            <div class="col-md-6 mb-3">
                              <label class="form-label">Pendidikan Ayah</label>
                              <select class="form-select" name="pendidikan_ayah">
                                <?php
                                $pendidikan=[1=>"SD",2=>"SMP",3=>"SMA",4=>"Diploma",5=>"S1",6=>"S2",7=>"S3",99=>"Lainnya"];
                                foreach($pendidikan as $id=>$nm){
                                  $sel=($mahasiswa['pendidikan_ayah']==$id)?'selected':'';
                                  echo"<option value='$id' $sel>$nm</option>";
                                }
                                ?>
                              </select>
                            </div>
                            <div class="col-md-6 mb-3">
                              <label class="form-label">Pekerjaan Ayah</label>
                              <select class="form-select" name="pekerjaan_ayah">
                                <option value="">-- Pilih Pekerjaan Ayah --</option>
                                <?php
                                  $listPekerjaan = [
                                    "1" => "Tidak bekerja",
                                    "2" => "Nelayan",
                                    "3" => "Petani",
                                    "4" => "Peternak",
                                    "5" => "PNS/TNI/Polri",
                                    "6" => "Karyawan Swasta",
                                    "7" => "Pedagang Kecil",
                                    "8" => "Pedagang Besar",
                                    "9" => "Wiraswasta",
                                    "10" => "Wirausaha",
                                    "11" => "Buruh",
                                    "12" => "Pensiunan",
                                    "13" => "Peneliti",
                                    "14" => "Tim Ahli / Konsultan",
                                    "15" => "Magang",
                                    "16" => "Tenaga Pengajar / Instruktur / Fasilitator",
                                    "17" => "Pimpinan / Manajerial",
                                    "98" => "Sudah Meninggal",
                                    "99" => "Lainnya",
                                  ];
                            
                                  $selected = $mahasiswa['pekerjaan_ayah'] ?? '';
                                  foreach ($listPekerjaan as $key => $value) {
                                      $isSelected = ($selected == $key) ? 'selected' : '';
                                      echo "<option value=\"$key\" $isSelected>$value</option>";
                                  }
                                ?>
                              </select>
                            </div>
                            <div class="col-md-6 mb-3">
                              <label for="penghasilan_ayah" class="form-label">Penghasilan Ayah</label>
                              <select class="form-select" id="penghasilan_ayah" name="penghasilan_ayah">
                                <option value="">-- Pilih Penghasilan Ayah --</option>
                                <?php
                                  $listPenghasilan = [
                                    "0"  => "Tidak ada / Tidak berpenghasilan",
                                    "11" => "Kurang dari Rp. 500,000",
                                    "12" => "Rp. 500,000 - Rp. 999,999",
                                    "13" => "Rp. 1,000,000 - Rp. 1,999,999",
                                    "14" => "Rp. 2,000,000 - Rp. 4,999,999",
                                    "15" => "Rp. 5,000,000 - Rp. 20,000,000",
                                    "16" => "Lebih dari Rp. 20,000,000",
                                  ];
                            
                                  $selected = $mahasiswa['penghasilan_ayah'] ?? '';
                                  foreach ($listPenghasilan as $key => $value) {
                                      $isSelected = ($selected == $key) ? 'selected' : '';
                                      echo "<option value=\"$key\" $isSelected>$value</option>";
                                  }
                                ?>
                              </select>
                            </div>

                            <!-- Ibu -->
                            <h6 class="fw-bold mt-4">Data Ibu</h6>
                            <div class="col-md-6 mb-3">
                              <label for="nik_ibu" class="form-label">NIK Ibu</label>
                              <input type="text" class="form-control" id="nik_ibu" name="nik_ibu" value="<?= htmlspecialchars($mahasiswa['nik_ibu'] ?? '') ?>">
                            </div>
                            <div class="col-md-6 mb-3">
                              <label for="nama_ibu" class="form-label">Nama Ibu</label>
                              <input type="text" class="form-control" id="nama_ibu" name="nama_ibu" 
                                     value="<?= htmlspecialchars($mahasiswa['nama_ibu'] ?? '') ?>" 
                                     style="text-transform: uppercase;">
                            </div>
                            <div class="col-md-6 mb-3">
                              <label for="tgl_lahir_ibu" class="form-label">Tanggal Lahir Ibu</label>
                              <input type="date" class="form-control" id="tgl_lahir_ibu" name="tgl_lahir_ibu" value="<?= htmlspecialchars($mahasiswa['tgl_lahir_ibu'] ?? '') ?>">
                            </div>
                            <div class="col-md-6 mb-3">
                              <label class="form-label">Pendidikan Ibu</label>
                              <select class="form-select" name="pendidikan_ibu">
                                <option value="">-- Pilih Pendidikan Ibu --</option>
                                <?php
                                  $pendidikan=[1=>"SD",2=>"SMP",3=>"SMA",4=>"Diploma",5=>"S1",6=>"S2",7=>"S3",99=>"Lainnya"];
                                  foreach($pendidikan as $id=>$nm){
                                    $sel=($mahasiswa['pendidikan_ibu']==$id)?'selected':'';
                                    echo"<option value='$id' $sel>$nm</option>";
                                  }
                                ?>
                              </select>
                            </div>
                            <div class="col-md-6 mb-3">
                              <label class="form-label">Pekerjaan Ibu</label>
                              <select class="form-select" name="pekerjaan_ibu">
                                <option value="">-- Pilih Pekerjaan Ibu --</option>
                                <?php
                                  $listPekerjaan = [
                                    "1" => "Tidak bekerja",
                                    "2" => "Nelayan",
                                    "3" => "Petani",
                                    "4" => "Peternak",
                                    "5" => "PNS/TNI/Polri",
                                    "6" => "Karyawan Swasta",
                                    "7" => "Pedagang Kecil",
                                    "8" => "Pedagang Besar",
                                    "9" => "Wiraswasta",
                                    "10" => "Wirausaha",
                                    "11" => "Buruh",
                                    "12" => "Pensiunan",
                                    "13" => "Peneliti",
                                    "14" => "Tim Ahli / Konsultan",
                                    "15" => "Magang",
                                    "16" => "Tenaga Pengajar / Instruktur / Fasilitator",
                                    "17" => "Pimpinan / Manajerial",
                                    "98" => "Sudah Meninggal",
                                    "99" => "Lainnya",
                                  ];
                                  $selected = $mahasiswa['pekerjaan_ibu'] ?? '';
                                  foreach ($listPekerjaan as $key => $value) {
                                      $isSelected = ($selected == $key) ? 'selected' : '';
                                      echo "<option value=\"$key\" $isSelected>$value</option>";
                                  }
                                ?>
                              </select>
                            </div>
                            <div class="col-md-6 mb-3">
                              <label for="penghasilan_ibu" class="form-label">Penghasilan Ibu</label>
                              <select class="form-select" id="penghasilan_ibu" name="penghasilan_ibu">
                                <option value="">-- Pilih Penghasilan Ibu --</option>
                                <?php
                                  $listPenghasilan = [
                                    "0"  => "Tidak ada / Tidak berpenghasilan",
                                    "11" => "Kurang dari Rp. 500,000",
                                    "12" => "Rp. 500,000 - Rp. 999,999",
                                    "13" => "Rp. 1,000,000 - Rp. 1,999,999",
                                    "14" => "Rp. 2,000,000 - Rp. 4,999,999",
                                    "15" => "Rp. 5,000,000 - Rp. 20,000,000",
                                    "16" => "Lebih dari Rp. 20,000,000",
                                  ];
                                  $selected = $mahasiswa['penghasilan_ibu'] ?? '';
                                  foreach ($listPenghasilan as $key => $value) {
                                      $isSelected = ($selected == $key) ? 'selected' : '';
                                      echo "<option value=\"$key\" $isSelected>$value</option>";
                                  }
                                ?>
                              </select>
                            </div>
                            
                            <!-- Wali -->
                            <h6 class="fw-bold mt-4">Data Wali</h6>
                            <div class="col-md-6 mb-3">
                              <label for="nama_wali" class="form-label">Nama Wali</label>
                              <input type="text" class="form-control" id="nama_wali" name="nama_wali" value="<?= htmlspecialchars($mahasiswa['nama_wali'] ?? '') ?>">
                            </div>
                            <div class="col-md-6 mb-3">
                              <label for="tgl_lahir_wali" class="form-label">Tanggal Lahir Wali</label>
                              <input type="date" class="form-control" id="tgl_lahir_wali" name="tgl_lahir_wali" value="<?= htmlspecialchars($mahasiswa['tgl_lahir_wali'] ?? '') ?>">
                            </div>
                            <div class="col-md-6 mb-3">
                              <label class="form-label">Pendidikan Wali</label>
                              <select class="form-select" name="pendidikan_wali">
                                <option value="">-- Pilih Pendidikan Wali --</option>
                                <?php
                                  foreach($pendidikan as $id=>$nm){
                                    $sel=($mahasiswa['pendidikan_wali']==$id)?'selected':'';
                                    echo"<option value='$id' $sel>$nm</option>";
                                  }
                                ?>
                              </select>
                            </div>
                            <div class="col-md-6 mb-3">
                              <label class="form-label">Pekerjaan Wali</label>
                              <select class="form-select" name="pekerjaan_wali">
                                <option value="">-- Pilih Pekerjaan Wali --</option>
                                <?php
                                  $selected = $mahasiswa['pekerjaan_wali'] ?? '';
                                  foreach ($listPekerjaan as $key => $value) {
                                      $isSelected = ($selected == $key) ? 'selected' : '';
                                      echo "<option value=\"$key\" $isSelected>$value</option>";
                                  }
                                ?>
                              </select>
                            </div>
                            <div class="col-md-6 mb-3">
                              <label for="penghasilan_wali" class="form-label">Penghasilan Wali</label>
                              <select class="form-select" id="penghasilan_wali" name="penghasilan_wali">
                                <option value="">-- Pilih Penghasilan Wali --</option>
                                <?php
                                  $selected = $mahasiswa['penghasilan_wali'] ?? '';
                                  foreach ($listPenghasilan as $key => $value) {
                                      $isSelected = ($selected == $key) ? 'selected' : '';
                                      echo "<option value=\"$key\" $isSelected>$value</option>";
                                  }
                                ?>
                              </select>
                            </div>

                          </div>
                          <button type="submit" class="btn btn-warning">Update Data Orang Tua / Wali</button>
                        </form>
                      </div>
                    </div>

                  </div>
                  <?php endif; ?>

                </div>

              </div>
            </div>

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