<?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">

                <!-- Konten Utama -->
                <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">Daftar Data Mahasiswa Baru</h5>
                    </div>

                    <!-- Form Pencarian dan Filter Tahun Masuk -->
                    <form method="GET" class="mb-4">
                      <div class="row">
                        <div class="col-md-6 mb-3 mb-md-0">
                          <label for="search" class="form-label">Cari Mahasiswa</label>
                          <input type="text" id="search" name="search" class="form-control" placeholder="Nama atau NISN" value="<?= htmlspecialchars($search ?? '') ?>">
                        </div>
                        <div class="col-md-2 d-flex align-items-end">
                          <button type="submit" class="btn btn-primary w-100">Filter</button>
                        </div>
                      </div>
                    </form>

                    <!-- Tombol Clear dan Download -->
                    <div class="d-flex align-items-center mb-4 gap-2">
                      
                      <!-- Tombol Clear -->
                      <form method="GET" action="/dashboard/staff-data-maba" class="m-0">
                        <button type="submit" class="btn btn-warning">Clear</button>
                      </form>
                    
                      <!-- Form Download -->
                      <form method="GET" action="/dashboard/staff-download-maba" class="d-flex align-items-center gap-2 m-0">
                        <input type="text" name="tahun_masuk" class="form-control" placeholder="Tahun Angkatan" style="max-width: 200px;" required>
                        <button type="submit" class="btn btn-info">Download Data</button>
                      </form>
                    
                    </div>

                    <!-- Tabel Data Mahasiswa -->
                    <div class="table-responsive">
                      <table class="table table-striped">
                        <thead>
                          <tr>
                            <th>No</th>
                            <th>Nama</th>
                            <th>NSN</th>
                            <th>Kelas</th>
                            <th>Pusat/Pokjar</th>
                          </tr>
                        </thead>
                            <tbody>
                              <?php if (!empty($data)): ?>
                                <?php foreach ($data as $index => $row): ?>
                                  <tr>
                                    <td><?= $offset + $index + 1 ?></td>
                                    <td><?= htmlspecialchars($row['nama']) ?></td>
                                    <td><?= htmlspecialchars($row['nisn']) ?></td>
                                    <td><?= htmlspecialchars($row['kelas'] ?? '') ?></td>
                                    <td><?= htmlspecialchars($row['pusat_pokjar'] ?? '') ?></td>
                                  </tr>
                                <?php endforeach; ?>
                              <?php else: ?>
                                <tr>
                                  <td colspan="5" class="text-center">Tidak ada data</td>
                                </tr>
                              <?php endif; ?>
                            </tbody>
                      </table>
                    </div>

					<!-- Paging Controls -->
					<?php if ($totalPages > 1): ?>
					  <div class="d-flex justify-content-end">
						<nav aria-label="Page navigation">
						  <ul class="pagination">

							<!-- First -->
							<li class="page-item <?= ($page <= 1) ? 'disabled' : '' ?>">
							  <a class="page-link" href="/dashboard/staff-data-maba?page=1&tahun_masuk=<?= urlencode($tahun_masuk ?? '') ?>&search=<?= urlencode($search ?? '') ?>"><<</a>
							</li>

							<!-- Previous -->
							<li class="page-item <?= ($page <= 1) ? 'disabled' : '' ?>">
							  <a class="page-link" href="/dashboard/staff-data-maba?page=<?= $page - 1 ?>&tahun_masuk=<?= urlencode($tahun_masuk ?? '') ?>&search=<?= urlencode($search ?? '') ?>"><</a>
							</li>

							<?php
							  $visiblePages = 5;
							  $startPage = max(1, $page - floor($visiblePages / 2));
							  $endPage = min($totalPages, $startPage + $visiblePages - 1);

							  if ($endPage - $startPage < $visiblePages - 1) {
								  $startPage = max(1, $endPage - $visiblePages + 1);
							  }
							?>

							<?php for ($i = $startPage; $i <= $endPage; $i++): ?>
							  <li class="page-item <?= ($i == $page) ? 'active' : '' ?>">
								<a class="page-link" href="/dashboard/staff-data-maba?page=<?= $i ?>&tahun_masuk=<?= urlencode($tahun_masuk ?? '') ?>&search=<?= urlencode($search ?? '') ?>"><?= $i ?></a>
							  </li>
							<?php endfor; ?>

							<!-- Next -->
							<li class="page-item <?= ($page >= $totalPages) ? 'disabled' : '' ?>">
							  <a class="page-link" href="/dashboard/staff-data-maba?page=<?= $page + 1 ?>&tahun_masuk=<?= urlencode($tahun_masuk ?? '') ?>&search=<?= urlencode($search ?? '') ?>">></a>
							</li>

							<!-- Last -->
							<li class="page-item <?= ($page >= $totalPages) ? 'disabled' : '' ?>">
							  <a class="page-link" href="/dashboard/staff-data-maba?page=<?= $totalPages ?>&tahun_masuk=<?= urlencode($tahun_masuk ?? '') ?>&search=<?= urlencode($search ?? '') ?>">>></a>
							</li>

						  </ul>
						</nav>
					  </div>
					<?php endif; ?>


                  </div>
                </div>

              </div>
            </div>

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