<?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 KHS</h5>
                    </div>
 
                    <!-- pastikan sudah ada bootstrap + icon (fontawesome/bootstrap icon) -->
                    <form method="get" action="/dashboard/admin-khs" class="mb-3 d-inline-block">
                      <div class="input-group">
                        <!-- Input pencarian -->
                        <input type="text" 
                               name="search" 
                               value="<?= htmlspecialchars($_GET['search'] ?? '') ?>" 
                               placeholder="Cari NIM atau Nama"
                               class="form-control"
                               style="width:200px; border-top-left-radius:50px; border-bottom-left-radius:50px; border-top-right-radius:0; border-bottom-right-radius:0;">
                        <!-- Tombol cari -->
                        <button type="submit" 
                                class="btn btn-primary btn-sm"
                                style="border-top-left-radius:0; border-bottom-left-radius:0; border-top-right-radius:50px; border-bottom-right-radius:50px;">
                          <i class="bi bi-search"></i> <!-- atau bi-search kalau pakai bootstrap icon -->
                        </button>
                        <!-- Tombol reload -->
                    
                      </div>
                    </form>
                    <a href="/dashboard/admin-khs" 
                       class="btn btn-secondary btn-sm ms-1"
                       style="border-radius:80px;">
                      <i class="bi bi-github"></i> <!-- icon reload -->
                    </a>

                    <!-- Table Data -->
                    <table class="table table-bordered table-sm">
                      <thead class="table-light">
                        <tr>
                          <th>No</th>
                          <th>Token</th>
                          <th>NIM</th>
                          <th>Nama</th>
                          <th>Status</th>
                          <th>Semester</th>
                          <th>Aksi</th>
                        </tr>
                      </thead>
                      <tbody>
                        <?php 
                        $no = $offset+1; 
                        foreach($data as $row): ?>
                        <tr>
                          <td><?= $no++; ?></td>
                          <td><?= htmlspecialchars($row['token']); ?></td>
                          <td><?= htmlspecialchars($row['nim']); ?></td>
                          <td><?= htmlspecialchars($row['nama']); ?></td>
                          <td><?= htmlspecialchars($row['status']); ?></td>
                          <td><?= htmlspecialchars($row['smt']); ?></td>
                          <td>
                            <button class="btn btn-sm btn-warning" 
                                    data-bs-toggle="modal" 
                                    data-bs-target="#editModal<?= $row['id_token_khs']; ?>">Edit</button>
                            <a href="/dashboard/admin-khs/delete?id=<?= $row['id_token_khs']; ?>" 
                               class="btn btn-sm btn-danger" 
                               onclick="return confirm('Hapus data ini?')">Hapus</a>
                          </td>
                        </tr>

                        <!-- Modal Edit -->
                        <div class="modal fade" id="editModal<?= $row['id_token_khs']; ?>" tabindex="-1">
                          <div class="modal-dialog">
                            <div class="modal-content">
                              <div class="modal-header">
                                <h5 class="modal-title">Edit Semester</h5>
                                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                              </div>
                              <form action="/dashboard/admin-khs-update" method="POST">
                                <div class="modal-body">
                                  <input type="hidden" name="id_token_khs" value="<?= $row['id_token_khs']; ?>">
                                  <div class="mb-2">
                                    <label class="form-label">Token</label>
                                    <input type="text" class="form-control" value="<?= $row['token']; ?>" readonly>
                                  </div>
                                  <div class="mb-2">
                                    <label class="form-label">NIM</label>
                                    <input type="hidden" name="nim" value="<?= $row['nim']; ?>">
                                    <input type="text" class="form-control" value="<?= $row['nim']; ?>" readonly>
                                  </div>
                                  <div class="mb-2">
                                    <label class="form-label">Nama</label>
                                    <input type="text" class="form-control" value="<?= $row['nama']; ?>" readonly>
                                  </div>
                                  <div class="mb-2">
                                    <label class="form-label">Semester</label>
                                    <input type="text" name="smt" class="form-control" value="<?= $row['smt']; ?>">
                                  </div>
                                </div>
                                <div class="modal-footer">
                                  <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
                                  <button type="submit" class="btn btn-primary">Simpan</button>
                                </div>
                              </form>
                            </div>
                          </div>
                        </div>
                        <?php endforeach; ?>
                      </tbody>
                    </table>

                    <!-- Pagination -->
                    <nav>
                      <ul class="pagination pagination-sm">
                        <li class="page-item <?= $page <= 1 ? 'disabled' : '' ?>">
                          <a class="page-link" href="/dashboard/admin-khs?page=1&search=<?= urlencode($search) ?>"><<</a>
                        </li>
                        <li class="page-item <?= $page <= 1 ? 'disabled' : '' ?>">
                          <a class="page-link" href="/dashboard/admin-khs?page=<?= $page-1; ?>&search=<?= urlencode($search) ?>"><</a>
                        </li>
                        <?php 
                        $start = max(1, $page - 2);
                        $end = min($totalPages, $start + 4);
                        for($i=$start;$i<=$end;$i++): ?>
                          <li class="page-item <?= $page == $i ? 'active' : '' ?>">
                            <a class="page-link" href="/dashboard/admin-khs?page=<?= $i; ?>&search=<?= urlencode($search) ?>"><?= $i; ?></a>
                          </li>
                        <?php endfor; ?>
                        <li class="page-item <?= $page >= $totalPages ? 'disabled' : '' ?>">
                          <a class="page-link" href="/dashboard/admin-khs?page=<?= $page+1; ?>&search=<?= urlencode($search) ?>">></a>
                        </li>
                        <li class="page-item <?= $page >= $totalPages ? 'disabled' : '' ?>">
                          <a class="page-link" href="/dashboard/admin-khs?page=<?= $totalPages; ?>&search=<?= urlencode($search) ?>">>></a>
                        </li>
                      </ul>
                    </nav>


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