<?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="container mt-5">

                  <?php if (!empty($data)) : ?>
                    <!-- ================= FORM EDIT DATA ================= -->
                    <div class="card">
                      <div class="card-body">
                        <h5 class="card-title text-center mb-4">Form Edit Data PPLK</h5>

                        <div class="mb-3">
                          <a href="/dashboard/mahasiswa-frm_pplk" target="_blank"
                             class="btn btn-warning float-end mb-3">
                            <i class="bi bi-printer"></i> Print
                          </a>
                        </div>

                        <form method="POST">
                          <div class="mb-3">
                            <label>NIM</label>
                            <input type="text" class="form-control" value="<?= $data['nim'] ?>" readonly>
                          </div>

                          <div class="mb-3">
                            <label>Nama</label>
                            <input type="text" class="form-control" value="<?= $data['nama'] ?>" readonly>
                          </div>

                          <div class="mb-3">
                            <label>Tempat Lahir</label>
                            <input type="text" name="tmpt_lahir" class="form-control" value="<?= $data['tmpt_lahir'] ?>">
                          </div>

                          <div class="mb-3">
                            <label>Tanggal Lahir</label>
                            <input type="date" name="tgl_lahir" class="form-control" value="<?= $data['tgl_lahir'] ?>">
                          </div>

                          <div class="mb-3">
                            <label>Program Studi</label>
                            <input type="text" class="form-control" value="<?= $data['prodi'] ?>" readonly>
                          </div>

                          <div class="mb-3">
                            <label>Semester / Kelas</label>
                            <input type="text" name="semester" class="form-control" value="<?= $data['semester'] ?>">
                          </div>

                          <div class="mb-3">
                            <label>Jenis Kelamin</label>
                            <select name="jenis_kelamin" class="form-control">
                              <option value="Laki-Laki" <?= $data['jenis_kelamin']=='Laki-Laki'?'selected':'' ?>>Laki-Laki</option>
                              <option value="Perempuan" <?= $data['jenis_kelamin']=='Perempuan'?'selected':'' ?>>Perempuan</option>
                            </select>
                          </div>

                          <div class="mb-3">
                            <label>No HP</label>
                            <input type="text" name="no_hp" class="form-control" value="<?= $data['no_hp'] ?>">
                          </div>

                          <div class="mb-3">
                            <label>Alamat</label>
                            <textarea name="alamat" class="form-control"><?= $data['alamat'] ?></textarea>
                          </div>

                          <div class="mb-3">
                            <label>Status Pekerjaan</label>
                            <select name="status_pekerjaan" class="form-control">
                              <option value="">Pilih...</option>
                              <?php foreach (['PNS','Honorer','Karyawan Swasta','Wirausaha','Belum Bekerja'] as $sp): ?>
                                <option value="<?= $sp ?>" <?= ($data['status_pekerjaan']==$sp)?'selected':'' ?>><?= $sp ?></option>
                              <?php endforeach; ?>
                            </select>
                          </div>

                          <div class="mb-3">
                            <label>Status Perkawinan</label>
                            <select name="status_perkawinan" class="form-control">
                              <option value="">Pilih...</option>
                              <option value="Menikah" <?= $data['status_perkawinan']=='Menikah'?'selected':'' ?>>Menikah</option>
                              <option value="Belum Menikah" <?= $data['status_perkawinan']=='Belum Menikah'?'selected':'' ?>>Belum Menikah</option>
                            </select>
                          </div>

                          <button type="submit" name="update" value="1" class="btn btn-success">
                            Update Data
                          </button>
                        </form>
                      </div>
                    </div>

                  <?php else : ?>
                    <!-- ================= FORM INPUT TOKEN ================= -->
                    <div class="card">
                      <div class="card-body">
                        <h5 class="card-title text-center mb-4">Masukkan Token PPLK</h5>

                        <form method="POST">
                          <div class="mb-3">
                            <label>Token</label>
                            <input type="text" name="token" class="form-control" required>
                          </div>
                          <button type="submit" class="btn btn-primary">Daftar</button>
                        </form>
                      </div>
                    </div>
                  <?php endif; ?>

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

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

<!-- ================= MODAL NOTIFIKASI ================= -->
<div class="modal fade" id="notifModal" tabindex="-1">
  <div class="modal-dialog modal-dialog-centered">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="notifTitle"></h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
      </div>
      <div class="modal-body" id="notifBody"></div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Tutup</button>
      </div>
    </div>
  </div>
</div>

<?php if (isset($_SESSION['success']) || isset($_SESSION['error'])): ?>
<script>
document.addEventListener('DOMContentLoaded', function () {
  const modal = new bootstrap.Modal(document.getElementById('notifModal'));
  const title = document.getElementById('notifTitle');
  const body  = document.getElementById('notifBody');

  <?php if (isset($_SESSION['success'])): ?>
    title.innerHTML = '<i class="bi bi-check-circle-fill text-success"></i> Berhasil';
    body.innerHTML  = '<?= addslashes($_SESSION['success']) ?>';
  <?php unset($_SESSION['success']); endif; ?>

  <?php if (isset($_SESSION['error'])): ?>
    title.innerHTML = '<i class="bi bi-x-circle-fill text-danger"></i> Gagal';
    body.innerHTML  = '<?= addslashes($_SESSION['error']) ?>';
  <?php unset($_SESSION['error']); endif; ?>

  modal.show();
});
</script>
<?php endif; ?>

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