<?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">
                <!-- wates -->
                <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">Data Transkip Nilai</h5>
                    </div>

                    <div class="container">
                      <div class="row mb-3">

                        <!-- Filter Form -->
                        <form method="GET" action="/dashboard/pegawai-transkip/export" class="mb-4">
                          <div class="d-flex justify-content-end">
                            <select name="prodi" class="form-select w-25 me-2" required>
                              <option value="">Pilih Program Studi</option>
                              <option value="86207">PGPAUD</option>
                              <option value="86206">PGSD</option>
                            </select>
                            <select name="tahun_masuk" class="form-select w-25 me-2" 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>
                            <select name="id_kurikulum" class="form-select w-25 me-2" required>
                              <option value="">Pilih Kurikulum</option>
                              <?php foreach ($kurikulumList as $kur): ?>
                                <option value="<?= $kur['id_kurikulum'] ?>">
                                  <?= $kur['id_prodi'] == '86206' ? 'PGSD' : 'PGPAUD' ?> - <?= htmlspecialchars($kur['keterangan']) ?>
                                </option>
                              <?php endforeach; ?>
                            </select>
                            <button type="submit" class="btn btn-primary">Export Excel</button>
                          </div>
                        </form>

                        <!-- Pencarian -->
                        <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 ?? '', ENT_QUOTES, 'UTF-8') ?>">
                            </div>
                            <div class="col-md-2 d-flex align-items-end">
                              <button type="submit" class="btn btn-primary w-100">Cari</button>
                            </div>
                          </div>
                        </form>

                        <!-- Tabel Data Transkip -->
                        <table class="table table-bordered mt-3">
                          <thead>
                            <tr>
                              <th>No</th>
                              <th>NIM</th>
                              <th>Nama</th>
                              <th>Download</th>
                            </tr>
                          </thead>
                          <tbody>
                            <?php 
                            $no = $offset + 1;
                            foreach ($result as $row): ?>
                              <tr>
                                <td><?= $no++ ?></td>
                                <td><?= htmlspecialchars($row['nim']) ?></td>
                                <td><?= htmlspecialchars($row['nama']) ?></td>
                                <td class="text-center">
                                  <a href="/dashboard/pegawai-transkip/pdf?nim=<?= urlencode($row['nim']) ?>" class="btn btn-warning btn-sm" target="_BLANK">
                                    <i class="bi bi-file-earmark-excel"></i>
                                  </a>
                                  <a href="/dashboard/pegawai-transkip/xls?nim=<?= urlencode($row['nim']) ?>" class="btn btn-success btn-sm">
                                    <i class="bi bi-filetype-csv"></i>
                                  </a>
                                </td>
                              </tr>
                            <?php endforeach; ?>
                          </tbody>
                        </table>

                        <!-- Pagination -->
                        <nav aria-label="Page navigation">
                          <ul class="pagination justify-content-end">
                            <li class="page-item <?= ($page <= 1) ? 'disabled' : '' ?>">
                              <a class="page-link" href="?<?= http_build_query(array_merge($_GET, ['page' => $page - 1])) ?>">Previous</a>
                            </li>
                            <?php 
                            $startPage = max(1, $page - 5);
                            $endPage = min($totalPages, $page + 4);
                            for ($i = $startPage; $i <= $endPage; $i++): ?>
                              <li class="page-item <?= ($i == $page) ? 'active' : '' ?>">
                                <a class="page-link" href="?<?= http_build_query(array_merge($_GET, ['page' => $i])) ?>"><?= $i ?></a>
                              </li>
                            <?php endfor; ?>
                            <li class="page-item <?= ($page >= $totalPages) ? 'disabled' : '' ?>">
                              <a class="page-link" href="?<?= http_build_query(array_merge($_GET, ['page' => $page + 1])) ?>">Next</a>
                            </li>
                          </ul>
                        </nav>

                      </div>
                    </div>

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

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

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