<?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-home" role="tabpanel">
                  <div class="p-4">
                    <div class="mb-4">
                      <h5 class="mb-0 fw-bold">Manajemen Data Ujian Kompre</h5>
                    </div>

<!-- Status Kompre -->
<div class="mb-3">
  <form method="post">
    <?php if ($status_kompre == 'Aktif'): ?>
      <button type="submit" name="toggle_status" class="btn btn-success">
        <i class="bi bi-unlock"></i> Kompre Aktif
      </button>
    <?php else: ?>
      <button type="submit" name="toggle_status" class="btn btn-danger">
        <i class="bi bi-lock"></i> Kompre Ditutup
      </button>
    <?php endif; ?>
  </form>
</div>

                    <div class="row mb-3 align-items-end">
                      <!-- Form Pencarian -->
                      <div class="col-md-6">
                        <form class="row g-2" method="get" action="">
                          <div class="col-8">
                            <input type="text" name="search" value="<?= isset($_GET['search']) ? htmlspecialchars($_GET['search']) : '' ?>" class="form-control" placeholder="Cari NIM atau Nama...">
                          </div>
                          <div class="col-4">
                            <div class="d-flex gap-2">
                              <button class="btn btn-primary" type="submit">Cari</button>
                              <a href="/dashboard/pegawai-manage_kompre" class="btn btn-secondary">Reset</a>
                            </div>
                          </div>
                        </form>
                      </div>
                    
                      <!-- Form Download -->
                      <div class="col-md-6">
                        <form class="row g-2 justify-content-end" method="get" action="/dashboard/pegawai-download-kompre">
                          <div class="col-8">
                            <label class="form-label">Pilih Tanggal Ujian</label>
                            <select name="tanggal" class="form-select" required>
                              <?php
                                function formatTanggalIndonesia($tanggal) {
                                  $bulan = [
                                    '01' => 'Januari', '02' => 'Februari', '03' => 'Maret',
                                    '04' => 'April', '05' => 'Mei', '06' => 'Juni',
                                    '07' => 'Juli', '08' => 'Agustus', '09' => 'September',
                                    '10' => 'Oktober', '11' => 'November', '12' => 'Desember'
                                  ];
                                  $tgl = date('d', strtotime($tanggal));
                                  $bln = date('m', strtotime($tanggal));
                                  $thn = date('Y', strtotime($tanggal));
                                  return "$tgl {$bulan[$bln]} $thn";
                                }
                    
                                // Ambil tanggal unik dari tb_ujiankompre
                                $stmt = $this->db->query("SELECT DISTINCT DATE(waktu_mulai) AS tanggal FROM tb_ujiankompre ORDER BY tanggal DESC");
                                $tanggalList = $stmt->fetchAll(PDO::FETCH_ASSOC);
                                foreach ($tanggalList as $row) {
                                  $value = $row['tanggal'];
                                  echo "<option value='$value'>" . formatTanggalIndonesia($value) . "</option>";
                                }
                              ?>
                            </select>
                          </div>
                          <div class="col-4 d-flex align-items-end">
                            <button class="btn btn-success w-100" type="submit">
                              <i class="bi bi-download"></i> Download Excel
                            </button>
                          </div>
                        </form>
                      </div>
                    </div>


                    <script>
                      function validateBulan() {
                        const awal = parseInt(document.getElementById('bulan_awal').value);
                        const akhir = parseInt(document.getElementById('bulan_akhir').value);
                        if (awal > akhir) {
                          alert("Bulan awal tidak boleh lebih besar dari bulan akhir.");
                          return false;
                        }
                        return true;
                      }
                    </script>

                    <div class="table-responsive">
                      <table class="table table-bordered table-striped">
                        <thead class="table-light">
                          <tr>
                            <th>No</th>
                            <th>NIM</th>
                            <th>Nama</th>
                            <th>Prodi</th>
                            <th>JK</th>
                            <th>No HP</th>
                            <th>Skor</th>
                            <th>Token</th>
                          </tr>
                        </thead>
                        <tbody>
                          <?php if (!empty($data_kompre)): ?>
                            <?php foreach ($data_kompre as $i => $row): ?>
                              <tr>
                                <td><?= $i + 1 ?></td>
                                <td><?= htmlspecialchars($row['nim']) ?></td>
                                <td><?= htmlspecialchars($row['nama']) ?></td>
                                <td><?= htmlspecialchars($row['id_prodi']) ?></td>
                                <td><?= htmlspecialchars($row['jenis_kelamin']) ?></td>
                                <td><?= htmlspecialchars($row['telpon']) ?></td>
                                <td><?= htmlspecialchars($row['skor']) ?></td>
                                <td><?= htmlspecialchars($row['token']) ?></td>
                              </tr>
                            <?php endforeach ?>
                          <?php else: ?>
                            <tr><td colspan="12" class="text-center">Tidak ada data.</td></tr>
                          <?php endif ?>
                        </tbody>
                      </table>
                    </div>

                    <!-- Modal Edit -->
                    <div class="modal fade" id="modalEdit" tabindex="-1" aria-labelledby="modalEditLabel" aria-hidden="true">
                      <div class="modal-dialog modal-lg">
                        <div class="modal-content">
                          <form method="post">
                            <div class="modal-header">
                              <h5 class="modal-title" id="modalEditLabel">Edit Data Ujian Kompre</h5>
                              <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                            </div>
                            <div class="modal-body row g-3">
                              <input type="hidden" name="id_ujiankompre" id="edit_id">
                              <div class="col-md-6">
                                <label>NIM</label>
                                <input type="text" class="form-control" name="nim" id="edit_nim">
                              </div>
                              <div class="col-md-6">
                                <label>Waktu Mulai</label>
                                <input type="datetime-local" class="form-control" name="waktu_mulai" id="edit_mulai">
                              </div>
                              <div class="col-md-6">
                                <label>Waktu Selesai</label>
                                <input type="datetime-local" class="form-control" name="waktu_selesai" id="edit_selesai">
                              </div>
                              <div class="col-md-6">
                                <label>Skor</label>
                                <input type="text" class="form-control" name="skor" id="edit_skor">
                              </div>
                            </div>
                            <div class="modal-footer">
                              <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
                              <button type="submit" name="edit_submit" class="btn btn-primary">Simpan</button>
                            </div>
                          </form>
                        </div>
                      </div>
                    </div>

                    <script>
                      function editData(data) {
                        document.getElementById('edit_id').value = data.id_ujiankompre;
                        document.getElementById('edit_nim').value = data.nim;
                        document.getElementById('edit_mulai').value = data.waktu_mulai.replace(' ', 'T');
                        document.getElementById('edit_selesai').value = data.waktu_selesai.replace(' ', 'T');
                        document.getElementById('edit_skor').value = data.skor;

                        let modal = new bootstrap.Modal(document.getElementById('modalEdit'));
                        modal.show();
                      }
                    </script>

                  </div>
                </div>

              </div>
            </div>

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