<?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="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" aria-labelledby="v-pills-home-tab">
                <div class="p-4">
                  <div class="mb-4">
                    <h5 class="mb-0 fw-bold">Download Nilai</h5>
                  </div>



                <div class="row g-3">
                  <div class="col-12">
                    <h6 class="mb-3">Transkip Nilai Sementara</h6>
                        <form method="GET" action="/dashboard/dosen-transkip/export">
                          <div class="row g-2 align-items-end">
                        
                            <!-- ✅ Prodi otomatis dari login -->
                            <input type="hidden" name="prodi" value="<?= htmlspecialchars($prodi ?? '', ENT_QUOTES, 'UTF-8') ?>">
                        
                            <div class="col-md-6">
                              <label class="form-label">Tahun Angkatan</label>
                              <select name="tahun_masuk" class="form-select" required>
                                <option value="">Pilih Tahun Angkatan</option>
                                <?php foreach ($tahunAngkatanList as $tahun): ?>
                                  <option value="<?= $tahun['tahun_masuk'] ?>">
                                    <?= htmlspecialchars($tahun['tahun_masuk'] ?? '', ENT_QUOTES, 'UTF-8') ?>
                                  </option>
                                <?php endforeach; ?>
                              </select>
                            </div>
                        
                            <div class="col-md-6">
                              <label class="form-label d-block">Kurikulum</label>
                              <div class="input-group">
                                <select name="id_kurikulum" class="form-select rounded-start-pill" required>
                                  <option value="">Pilih Kurikulum</option>
                                  <?php foreach ($kurikulumList as $kur): ?>
                                    <?php if ($kur['id_prodi'] == $prodi): ?>
                                      <option value="<?= $kur['id_kurikulum'] ?>">
                                        <?= htmlspecialchars($kur['keterangan']) ?>
                                      </option>
                                    <?php endif; ?>
                                  <?php endforeach; ?>
                                </select>
                                <button type="submit" class="btn btn-primary rounded-end-pill">Export</button>
                              </div>
                            </div>
                        
                          </div>
                        </form>

                  </div>
                </div>
                </br>
                <div class="row g-3 mb-4">
                  <div class="col-md-6">
                    <h6 class="mb-3">Cari Dosen Untuk Download/MK Smt Ini</h6>                      
                    <form method="GET" class="d-flex">
                      <input 
                        type="text" 
                        class="form-control me-2" 
                        placeholder="Ketik nama dosen..." 
                        name="search" 
                        value="<?= htmlspecialchars($search ?? '') ?>"
                      >
                      <button class="btn btn-outline-primary" type="submit">Search</button>
                    </form>
                  </div>
                </div>
                   
                  <div class="bg-white rounded shadow-sm p-4">
                    <table class="table table-bordered-dinamis">
                      <thead>
                        <tr>
                          <th scope="col">Dosen</th>
                          <th scope="col">MK</th>
                          <th scope="col">SMT</th>
                          <th scope="col">Kelas</th>
                          <th scope="col">Unduh</th>
                        </tr>
                      </thead>
                      <tbody>
                        <?php if (!empty($mahasiswa)) : ?>
                          <?php foreach ($mahasiswa as $row) : ?>
                            <tr>
                              <td><?= htmlspecialchars($row['nama']) ?></td>
                              <td><?= htmlspecialchars($row['singkatan']) ?></td>
                              <td><?= htmlspecialchars($row['smt']) ?></td>
                              <td><?= htmlspecialchars($row['kode_kelas']) ?></td>
                              <td>
                                <form action="/dashboard/dosen-download-nilai" method="post" target="_blank">
                                  <input type="hidden" name="nama" value="<?= htmlspecialchars($row['nama']) ?>">
                                  <input type="hidden" name="smt" value="<?= htmlspecialchars($row['smt']) ?>">
                                  <input type="hidden" name="nama_mk" value="<?= htmlspecialchars($row['singkatan']) ?>">
                                  <input type="hidden" name="kode_mk" value="<?= htmlspecialchars($row['kode_mk']) ?>">
                                  <input type="hidden" name="kode_kelas" value="<?= htmlspecialchars($row['kode_kelas']) ?>">
                                  <input type="hidden" name="niy" value="<?= htmlspecialchars($row['niy']) ?>">
                                  <input type="hidden" name="tahun_akademik" value="<?= htmlspecialchars($row['tahun_akademik']) ?>">
                                  <button type="submit" name="simpan" class="btn btn-sm btn-info">
                                    <i class="bi bi-file-earmark-spreadsheet"></i>
                                  </button>
                                </form>
                              </td>
                            </tr>
                          <?php endforeach; ?>
                        <?php else : ?>
                          <tr>
                            <td colspan="5" class="text-center">Data tidak ditemukan.</td>
                          </tr>
                        <?php endif; ?>
                      </tbody>
                    </table>

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

						  <!-- Tombol First -->
						  <li class="page-item <?= $currentPage == 1 ? 'disabled' : '' ?>">
							<a class="page-link" href="/dashboard/dosen-download-nilai?page=1&search=<?= urlencode($search ?? '') ?>">First</a>
						  </li>

						  <!-- Tombol Prev -->
						  <li class="page-item <?= $currentPage == 1 ? 'disabled' : '' ?>">
							<a class="page-link" href="/dashboard/dosen-download-nilai?page=<?= $currentPage - 1 ?>&search=<?= urlencode($search ?? '') ?>">Prev</a>
						  </li>

						  <?php
							$range = 10; // maksimal jumlah halaman yang ditampilkan
							$start = max(1, $currentPage - floor($range / 2));
							$end = min($totalPages, $start + $range - 1);

							// sesuaikan start jika end berada di ujung
							if ($end - $start < $range - 1) {
							  $start = max(1, $end - $range + 1);
							}

							for ($i = $start; $i <= $end; $i++): 
						  ?>
							<li class="page-item <?= $i == $currentPage ? 'active' : '' ?>">
							  <a class="page-link" href="/dashboard/dosen-download-nilai?page=<?= $i ?>&search=<?= urlencode($search ?? '') ?>"><?= $i ?></a>
							</li>
						  <?php endfor; ?>

						  <!-- Tombol Next -->
						  <li class="page-item <?= $currentPage == $totalPages ? 'disabled' : '' ?>">
							<a class="page-link" href="/dashboard/dosen-download-nilai?page=<?= $currentPage + 1 ?>&search=<?= urlencode($search ?? '') ?>">Next</a>
						  </li>

						  <!-- Tombol Last -->
						  <li class="page-item <?= $currentPage == $totalPages ? 'disabled' : '' ?>">
							<a class="page-link" href="/dashboard/dosen-download-nilai?page=<?= $totalPages ?>&search=<?= urlencode($search ?? '') ?>">Last</a>
						  </li>

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

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

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