<?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 d-flex justify-content-between align-items-center">
                      <h5 class="mb-0 fw-bold">Monitoring Pendaftaran KIP</h5>
                      <form method="get" class="d-flex">
                        <input type="text" name="search" class="form-control form-control-sm me-2" placeholder="Cari nama / NIK" value="<?= htmlspecialchars($search) ?>">
                        <button type="submit" class="btn btn-sm btn-primary">Cari</button>
                      </form>
                    </div>

                    <div class="border rounded p-4 mb-3">
                      <table class="table table-bordered table-striped">
                        <thead>
                          <tr>
                            <th>No</th>
                            <th>Nama</th>
                            <th>NIK</th>
                            <th>Tahun Lulus</th>
                            <th>Status</th>
                            <th>File</th>
                          </tr>
                        </thead>
                        <tbody>
                          <?php if (empty($pendaftar_list)) : ?>
                            <tr>
                              <td colspan="6" class="text-center">Belum ada pendaftar</td>
                            </tr>
                          <?php else : ?>
                            <?php $no = ($page-1)*15 + 1; foreach ($pendaftar_list as $row) : ?>
                              <tr>
                                <td><?= $no++ ?></td>
                                <td><?= htmlspecialchars($row['nama_camaba']) ?></td>
                                <td><?= htmlspecialchars($row['nik_ayah']) ?></td>
                                <td><?= htmlspecialchars($row['tahun_lulus']) ?></td>
                                <td><?= htmlspecialchars($row['status_ayah']) ?></td>
                                <td>
                                  <!-- Tombol Lihat Detail -->
                                    <button type="button" 
                                            class="btn btn-sm btn-info text-white" 
                                            data-bs-toggle="modal" 
                                            data-bs-target="#detailModal<?= $row['id_reg_kip'] ?>">
                                      <i class="bi bi-eye"></i>
                                    </button>
                                                                    
                                  <?php if (!empty($row['berkas'])): ?>
                                      <a href="https://portalstkipbn.gerbangberkah.co.id/api/download-berkas?file=<?= urlencode($row['berkas']) ?>&token=TOKEN_RAHASIA" 
                                         class="btn btn-sm btn-success">
                                         Download
                                      </a>
                                  <?php else: ?>
                                      <span class="text-muted">Tidak ada file</span>
                                  <?php endif; ?>
                                </td>
                              </tr>
                            <?php endforeach; ?>
                          <?php endif; ?>
                        </tbody>
                      </table>

                    <?php foreach ($pendaftar_list as $row): ?>
                    <div class="modal fade" id="detailModal<?= $row['id_reg_kip'] ?>" tabindex="-1" aria-hidden="true">
                      <div class="modal-dialog modal-lg modal-dialog-centered">
                        <div class="modal-content">
                          <div class="modal-header bg-primary text-white">
                            <h5 class="modal-title">Detail Pendaftar: <?= htmlspecialchars($row['nama_camaba']) ?></h5>
                            <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
                          </div>
                          <div class="modal-body">
                            <table class="table table-bordered">
                              <tr><th>NISN</th><td><?= htmlspecialchars($row['nisn']) ?></td></tr>
                              <tr><th>Nama</th><td><?= htmlspecialchars($row['nama_camaba']) ?></td></tr>
                              <tr><th>Tahun Lulus</th><td><?= htmlspecialchars($row['tahun_lulus']) ?></td></tr>
                              <tr><th>Asal Sekolah</th><td><?= htmlspecialchars($row['asal_sekolah']) ?></td></tr>
                              <tr><th>NPSN</th><td><?= htmlspecialchars($row['npsn']) ?></td></tr>
                              <tr><th>NIK</th><td><?= htmlspecialchars($row['nik']) ?></td></tr>
                              <tr><th>Daya Listrik</th><td><?= htmlspecialchars($row['daya_listrik']) ?></td></tr>
                              <tr><th>Pajak Rumah</th><td><?= htmlspecialchars($row['pajak_rumah']) ?></td></tr>
                              <tr><th>Alamat</th><td><?= htmlspecialchars($row['alamat']) ?></td></tr>
                              <tr><th>Nama Ibu</th><td><?= htmlspecialchars($row['nama_ibu']) ?></td></tr>
                              <tr><th>NIK Ibu</th><td><?= htmlspecialchars($row['nik_ibu']) ?></td></tr>
                              <tr><th>Status Ibu</th><td><?= htmlspecialchars($row['status_ibu']) ?></td></tr>
                              <tr><th>Pekerjaan Ibu</th><td><?= htmlspecialchars($row['pekerjaan_ibu']) ?></td></tr>
                              <tr><th>Nama Ayah</th><td><?= htmlspecialchars($row['nama_ayah']) ?></td></tr>
                              <tr><th>NIK Ayah</th><td><?= htmlspecialchars($row['nik_ayah']) ?></td></tr>
                              <tr><th>Hubungan Ayah</th><td><?= htmlspecialchars($row['hubungan_ayah']) ?></td></tr>
                              <tr><th>Status Ayah</th><td><?= htmlspecialchars($row['status_ayah']) ?></td></tr>
                              <tr><th>Pekerjaan Ayah</th><td><?= htmlspecialchars($row['pekerjaan_ayah']) ?></td></tr>
                            </table>
                          </div>
                          <div class="modal-footer">
                            <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Tutup</button>
                          </div>
                        </div>
                      </div>
                    </div>
                    <?php endforeach; ?>

                      <!-- Pagination -->
                      <?php if ($totalPages > 1): ?>
                      <nav>
                        <ul class="pagination justify-content-center">

                          <!-- First -->
                          <li class="page-item <?= $page <= 1 ? 'disabled' : '' ?>">
                            <a class="page-link" href="/dashboard/admin-monitoring_kip?search=<?= urlencode($search) ?>&page=1">&lt;&lt;</a>
                          </li>

                          <!-- Prev -->
                          <li class="page-item <?= $page <= 1 ? 'disabled' : '' ?>">
                            <a class="page-link" href="/dashboard/admin-monitoring_kip?search=<?= urlencode($search) ?>&page=<?= $page-1 ?>">&lt;</a>
                          </li>

                          <?php
                          $start = max(1, $page - 3);
                          $end   = min($totalPages, $start + 6); // maksimal 7 nomor halaman
                          for ($i = $start; $i <= $end; $i++):
                          ?>
                          <li class="page-item <?= $page==$i?'active':'' ?>">
                            <a class="page-link" href="/dashboard/admin-monitoring_kip?search=<?= urlencode($search) ?>&page=<?= $i ?>"><?= $i ?></a>
                          </li>
                          <?php endfor; ?>

                          <!-- Next -->
                          <li class="page-item <?= $page >= $totalPages ? 'disabled' : '' ?>">
                            <a class="page-link" href="/dashboard/admin-monitoring_kip?search=<?= urlencode($search) ?>&page=<?= $page+1 ?>">&gt;</a>
                          </li>

                          <!-- Last -->
                          <li class="page-item <?= $page >= $totalPages ? 'disabled' : '' ?>">
                            <a class="page-link" href="/dashboard/admin-monitoring_kip?search=<?= urlencode($search) ?>&page=<?= $totalPages ?>">&gt;&gt;</a>
                          </li>
                        </ul>
                      </nav>
                      <?php endif; ?>

                    </div>

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

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

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