<?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 Aktif</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 NIM" value="<?= htmlspecialchars($search ?? '') ?>">
                        </div>
                        <div class="col-md-4 mb-3 mb-md-0">
                          <label for="tahun_masuk" class="form-label">Pilih Tahun Masuk</label>
                          <select id="tahun_masuk" name="tahun_masuk" class="form-select">
                            <option value="">Semua Tahun</option>
                            <?php foreach ($daftar_tahun as $tahun): ?>
                              <option value="<?= $tahun ?>" <?= ($tahun == ($tahun_masuk ?? '')) ? 'selected' : '' ?>>
                                <?= $tahun ?>
                              </option>
                            <?php endforeach; ?>
                          </select>
                        </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 mb-4">
                      <form method="GET" action="/dashboard/pegawai-data-mhs" class="me-2">
                        <button type="submit" class="btn btn-warning">Clear</button>
                      </form>

                      <form method="GET" action="/dashboard/pegawai-download-mhs">
                        <input type="hidden" name="tahun_masuk" value="<?= htmlspecialchars($tahun_masuk ?? '') ?>">
                        <input type="hidden" name="search" value="<?= htmlspecialchars($search ?? '') ?>">
                        <button type="submit" class="btn btn-info">Download Data</button>
                      </form>
                    </div>

                    <hr>
                    
                    <h6 class="fw-bold mb-3">Import Data Mahasiswa (Excel)</h6>
                    
                    <form method="POST"
                          action="/dashboard/pegawai-import-mhs"
                          enctype="multipart/form-data">
                    
                      <input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?>">
                    
                      <div class="row align-items-end">
                    
                        <!-- File + Import (SATU GROUP) -->
                        <div class="col-md-6">
                          <div class="input-group">
                            <input type="file"
                                   name="file_excel"
                                   class="form-control"
                                   accept=".xls,.xlsx"
                                   required>
                    
                            <button class="btn btn-success" type="submit">
                              <i class="bi bi-upload"></i> Import
                            </button>
                          </div>
                        </div>
                    
                        <!-- Download Template -->
                        <div class="col-md-3">
                          <a href="/download_format?file=template_import_mahasiswa.xlsx"
                             class="btn btn-outline-primary w-0"
                             target="_blank">
                            <i class="bi bi-file-earmark-excel"></i> Download Template
                          </a>
                        </div>
                    
                      </div>
                    </form>



                    <!-- Tabel Data Mahasiswa -->
                    <div class="table-responsive">
                      <table class="table table-striped">
                        <thead>
                          <tr>
                            <th>No</th>
                            <th>Nama</th>
                            <th>NIM</th>
                            <th>Program Studi</th>
                          </tr>
                        </thead>
                        <tbody>
                          <?php if (empty($mahasiswa)): ?>
                            <tr>
                              <td colspan="4" class="text-center">Tidak ada data ditemukan.</td>
                            </tr>
                          <?php else: ?>
                            <?php foreach ($mahasiswa as $index => $mhs): ?>
                              <tr>
                                <td><?= $offset + $index + 1 ?></td>
                                <td><?= htmlspecialchars($mhs['nama']) ?></td>
                                <td><?= htmlspecialchars($mhs['nim']) ?></td>
                                <td><?= htmlspecialchars($mhs['nama_prodi']) ?></td>
                              </tr>
                            <?php endforeach; ?>
                          <?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/pegawai-data-mhs?page=1&tahun_masuk=<?= urlencode($tahun_masuk ?? '') ?>&search=<?= urlencode($search ?? '') ?>">First</a>
							</li>

							<!-- Previous -->
							<li class="page-item <?= ($page <= 1) ? 'disabled' : '' ?>">
							  <a class="page-link" href="/dashboard/pegawai-data-mhs?page=<?= $page - 1 ?>&tahun_masuk=<?= urlencode($tahun_masuk ?? '') ?>&search=<?= urlencode($search ?? '') ?>">Previous</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/pegawai-data-mhs?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/pegawai-data-mhs?page=<?= $page + 1 ?>&tahun_masuk=<?= urlencode($tahun_masuk ?? '') ?>&search=<?= urlencode($search ?? '') ?>">Next</a>
							</li>

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

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


                  </div>
                </div>

              </div>
            </div>

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