<?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">Manajemen Data Sidang Skripsi</h5>
                    </div>

                    <form class="row mb-3" method="get" action="">
                      <div class="col-md-4">
                        <input type="text" name="search" value="<?= isset($_GET['search']) ? htmlspecialchars($_GET['search']) : '' ?>" class="form-control" placeholder="Cari NIM atau Nama...">
                      </div>
                      <div class="col-md-2">
                        <button class="btn btn-primary" type="submit">Cari</button>
                        <a href="/dashboard/pegawai-manage_skripsi" class="btn btn-secondary">Reset</a>
                      </div>
                    </form>

                    <form class="row mb-3 g-2" method="get" action="/dashboard/pegawai-download-skripsi" onsubmit="return validateBulan()">
                      <div class="col-md-2">
                        <label class="form-label">Dari Bulan</label>
                        <select name="bulan_awal" id="bulan_awal" class="form-select">
                          <?php
                            $blnNow = (int)date('m');
                            foreach (range(1, 12) as $b) {
                              $selected = ($b == $blnNow) ? 'selected' : '';
                              printf('<option value="%02d" %s>%s</option>', $b, $selected, date('F', mktime(0, 0, 0, $b, 1)));
                            }
                          ?>
                        </select>
                      </div>
                      <div class="col-md-2">
                        <label class="form-label">Hingga Bulan</label>
                        <select name="bulan_akhir" id="bulan_akhir" class="form-select">
                          <?php
                            foreach (range(1, 12) as $b) {
                              $selected = ($b == $blnNow) ? 'selected' : '';
                              printf('<option value="%02d" %s>%s</option>', $b, $selected, date('F', mktime(0, 0, 0, $b, 1)));
                            }
                          ?>
                        </select>
                      </div>
                      <div class="col-md-2">
                        <label class="form-label">Tahun</label>
                        <select name="tahun" class="form-select">
                          <?php
                            $yearNow = (int)date('Y');
                            for ($y = $yearNow; $y >= $yearNow - 10; $y--) {
                              echo "<option value='$y'" . ($y == $yearNow ? ' selected' : '') . ">$y</option>";
                            }
                          ?>
                        </select>
                      </div>
                      <div class="col-md-2 d-flex align-items-end">
                        <button class="btn btn-success w-100" type="submit"><i class="bi bi-download"></i> Download Excel</button>
                      </div>
                    </form>

                    <script>
                      function validateBulan() {
                        const awal = parseInt(document.getElementById('bulan_awal').value);
                        const akhir = parseInt(document.getElementById('bulan_akhir').value);
                        if (awal > akhir) {
                          alert("Bulan awal tidak boleh lebih besar dari bulan akhir.");
                          return false;
                        }
                        return true;
                      }
                    </script>

                    <!-- Tabel Data -->
                    <div class="table-responsive">
                      <table class="table table-bordered table-striped">
                        <thead class="table-light">
                          <tr>
                            <th>No</th>
                            <th>NIM</th>
                            <th>Nama</th>
                            <th>Prodi</th>
                            <th>Pembimbing 1</th>
                            <th>Pembimbing 2</th>
                          </tr>
                        </thead>
                        <tbody>
                          <?php if (!empty($data_skripsi)): ?>
                            <?php foreach ($data_skripsi as $i => $row): ?>
                              <tr>
                                <td><?= $i + 1 ?></td>
                                <td><?= htmlspecialchars($row['nim']) ?></td>
                                <td style="font-size: 11px;"><?= htmlspecialchars($row['nama']) ?></td>
                                <td>
                                  <?php
                                    $prodi = $row['prodi'];
                                    if ($prodi === 'Pendidikan Guru Sekolah Dasar') {
                                        echo 'PGSD';
                                    } elseif ($prodi === 'Pendidikan Guru Pendidikan Anak Usia Dini') {
                                        echo 'PGPAUD';
                                    } else {
                                        echo htmlspecialchars($prodi);
                                    }
                                  ?>
                                </td>
                                <td style="font-size: 11px; "><?= htmlspecialchars($row['pembimbing1']) ?></td>
                                <td style="font-size: 11px; "><?= htmlspecialchars($row['pembimbing2']) ?></td>
                              </tr>
                            <?php endforeach ?>
                          <?php else: ?>
                            <tr><td colspan="9" class="text-center">Tidak ada data.</td></tr>
                          <?php endif ?>
                        </tbody>
                      </table>
                    </div>

                    <!-- Paginasi -->
                    <?php
                      $currentPage = isset($_GET['page']) ? (int) $_GET['page'] : 1;
                      $totalPages = isset($totalPages) ? $totalPages : 1;
                      $maxButtons = 8;
                      $start = max(1, $currentPage - floor($maxButtons / 2));
                      $end = min($totalPages, $start + $maxButtons - 1);
                      $start = max(1, $end - $maxButtons + 1);
                    ?>
                    <nav>
                      <ul class="pagination justify-content-center mt-4">
                        <li class="page-item <?= $currentPage <= 1 ? 'disabled' : '' ?>">
                          <a class="page-link" href="/dashboard/pegawai-manage_skripsi?page=1">&laquo;</a>
                        </li>
                        <li class="page-item <?= $currentPage <= 1 ? 'disabled' : '' ?>">
                          <a class="page-link" href="/dashboard/pegawai-manage_skripsi?page=<?= $currentPage - 1 ?>">&lsaquo;</a>
                        </li>
                        <?php for ($i = $start; $i <= $end; $i++): ?>
                          <li class="page-item <?= $i == $currentPage ? 'active' : '' ?>">
                            <a class="page-link" href="/dashboard/pegawai-manage_skripsi?page=<?= $i ?>"><?= $i ?></a>
                          </li>
                        <?php endfor ?>
                        <li class="page-item <?= $currentPage >= $totalPages ? 'disabled' : '' ?>">
                          <a class="page-link" href="/dashboard/pegawai-manage_skripsi?page=<?= $currentPage + 1 ?>">&rsaquo;</a>
                        </li>
                        <li class="page-item <?= $currentPage >= $totalPages ? 'disabled' : '' ?>">
                          <a class="page-link" href="/dashboard/pegawai-manage_skripsi?page=<?= $totalPages ?>">&raquo;</a>
                        </li>
                      </ul>
                    </nav>

                  </div>
                </div>

              </div>
            </div>

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