<?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">Data IPK Mahasiswa</h5>
                    </div>

                    <div class="row row-cols-1 row-cols-md-12 g-4">
                    <div class="mb-3">
                    <form action="/dashboard/pegawai-export" method="get" class="d-flex align-items-center gap-2">
                        <select name="tahun_masuk" id="tahun_masuk"
                                class="form-select form-select-sm"
                                style="width: auto;" required>
                    
                            <option value="">-- Pilih Angkatan --</option>
                    
                            <?php foreach ($tahunMasukList as $tahun): ?>
                                <?php if ($tahun === null || $tahun === '') continue; ?>
                                <option value="<?= htmlspecialchars($tahun) ?>">
                                    <?= htmlspecialchars($tahun) ?>
                                </option>
                            <?php endforeach; ?>
                    
                        </select>
                        <button type="submit" class="btn btn-success btn-sm">Download Excel</button>
                    </form>
                    </div>
                    
                      <!-- Form Pencarian -->
                      <form method="get" class="mb-3">
                        <div class="input-group">
                          <input type="text" name="search" class="form-control" 
                                 placeholder="Cari NIM / Nama Mahasiswa" 
                                 value="<?= htmlspecialchars($data['search']) ?>">
                          <button type="submit" class="btn btn-primary">Cari</button>
                        </div>
                      </form>
                    
                      <table class="table table-bordered table-striped">
                        <thead class="table-light">
                          <tr>
                            <th>NIM</th>
                            <th>Nama</th>
                            <th>Semester</th>
                            <th>Tahun Akademik</th>
                            <th>IPS</th>
                            <th>IPK</th>
                          </tr>
                        </thead>
                        <tbody>
                          <?php if (!empty($data['ipsIpkData'])): ?>
                            <?php foreach ($data['ipsIpkData'] as $row): ?>
                              <tr>
                                <td><?= htmlspecialchars($row['nim']) ?></td>
                                <td><?= htmlspecialchars($row['nama']) ?></td>
                                <td><?= htmlspecialchars($row['smt']) ?></td>
                                <td><?= htmlspecialchars($row['tahun_akademik']) ?></td>
                                <td><?= htmlspecialchars($row['ips']) ?></td>
                                <td><?= htmlspecialchars($row['ipk']) ?></td>
                              </tr>
                            <?php endforeach; ?>
                          <?php else: ?>
                            <tr>
                              <td colspan="6" class="text-center">Belum ada data IPS / IPK mahasiswa.</td>
                            </tr>
                          <?php endif; ?>
                        </tbody>
                      </table>
                    
                      <!-- Pagination -->
                      <?php if ($data['totalPages'] > 1): ?>
                        <nav>
                          <ul class="pagination">
                            <!-- First -->
                            <li class="page-item <?= $data['page'] == 1 ? 'disabled' : '' ?>">
                              <a class="page-link" href="/dashboard/pegawai-ips-ipk?page=1&search=<?= urlencode($data['search']) ?>">First</a>
                            </li>
                            <!-- Prev -->
                            <li class="page-item <?= $data['page'] == 1 ? 'disabled' : '' ?>">
                              <a class="page-link" href="/dashboard/pegawai-ips-ipk?page=<?= max(1, $data['page'] - 1) ?>&search=<?= urlencode($data['search']) ?>">Prev</a>
                            </li>
                    
                            <!-- Numbered Pages (max 5) -->
                            <?php
                              $start = max(1, $data['page'] - 2);
                              $end   = min($data['totalPages'], $start + 4);
                              for ($i = $start; $i <= $end; $i++):
                            ?>
                              <li class="page-item <?= $i == $data['page'] ? 'active' : '' ?>">
                                <a class="page-link" href="/dashboard/pegawai-ips-ipk?page=<?= $i ?>&search=<?= urlencode($data['search']) ?>"><?= $i ?></a>
                              </li>
                            <?php endfor; ?>
                    
                            <!-- Next -->
                            <li class="page-item <?= $data['page'] == $data['totalPages'] ? 'disabled' : '' ?>">
                              <a class="page-link" href="/dashboard/pegawai-ips-ipk?page=<?= min($data['totalPages'], $data['page'] + 1) ?>&search=<?= urlencode($data['search']) ?>">Next</a>
                            </li>
                            <!-- Last -->
                            <li class="page-item <?= $data['page'] == $data['totalPages'] ? 'disabled' : '' ?>">
                              <a class="page-link" href="/dashboard/pegawai-ips-ipk?page=<?= $data['totalPages'] ?>&search=<?= urlencode($data['search']) ?>">Last</a>
                            </li>
                          </ul>
                        </nav>
                      <?php endif; ?>
                    </div>


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

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