<?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="d-flex justify-content-between align-items-center mb-2">
                      <h5 class="mb-0 fw-bold">Manage NIM Maba</h5>
                    </div>

                    <div class="table-responsive">
                    
                    <?php if (!empty($_SESSION['pesan'])): ?>
                      <div class="alert alert-<?= $_SESSION['status_alert']; ?> alert-dismissible fade show">
                        <?= $_SESSION['pesan']; ?>
                        <button class="btn-close" data-bs-dismiss="alert"></button>
                      </div>
                    <?php unset($_SESSION['pesan'], $_SESSION['status_alert']); endif; ?>
                    
                    <!-- IMPORT -->
                    <div class="card mb-3">
                      <div class="card-body p-3">
                    
                        <div class="row g-2 align-items-end">
                    
                          <!-- FORM IMPORT (BENAR-BENAR MENYATU) -->
                          <div class="col-12 col-md-7 col-lg-6">
                            <form method="POST"
                                  action="/dashboard/admin-nim-mbr/import"
                                  enctype="multipart/form-data">
                    
                              <input type="hidden" name="csrf_token" value="<?= $csrf ?>">
                    
                              <label class="form-label fw-semibold mb-1">Import Excel</label>
                    
                              <div class="input-group input-group-sm">
                                <input type="file"
                                       name="file_excel"
                                       class="form-control"
                                       accept=".xls,.xlsx"
                                       required>
                    
                                <button class="btn btn-primary" type="submit">
                                  Import
                                </button>
                              </div>
                    
                            </form>
                          </div>
                    
                          <!-- DOWNLOAD TEMPLATE (TERPISAH) -->
                          <div class="col-12 col-md-5 col-lg-3">
                            <label class="form-label fw-semibold mb-1 d-none d-md-block">&nbsp;</label>
                            <a href="/download_format?file=template_update_NIM_MABA.xlsx"
                               class="btn btn-outline-primary btn-sm w-100"
                               target="_blank">
                              <i class="bi bi-file-earmark-excel"></i> Download Template
                            </a>
                          </div>
                    
                        </div>
                    
                      </div>
                    </div>

                    <!-- SEARCH -->
                    <form method="GET" class="row g-2 mb-2">
                      <div class="col-12 col-md-6 col-lg-4">
                        <input type="text" name="search"
                               value="<?= htmlspecialchars($search) ?>"
                               class="form-control form-control-sm"
                               placeholder="Cari NIM / NISN / Nama">
                      </div>
                    </form>
                    
                    <!-- TABLE -->
                    <table class="table table-sm table-bordered align-middle">
                      <thead class="table-light text-center">
                        <tr>
                          <th>NIM</th>
                          <th>NISN</th>
                          <th>Nama</th>
                          <th>Kelas</th>
                          <th>SMT</th>
                        </tr>
                      </thead>
                      <tbody>
                        <?php if (empty($data)): ?>
                          <tr>
                            <td colspan="5" class="text-center text-muted">Data tidak ditemukan</td>
                          </tr>
                        <?php endif; ?>
                    
                        <?php foreach ($data as $d): ?>
                          <tr>
                            <td><?= $d['nim'] ?></td>
                            <td><?= $d['nisn'] ?></td>
                            <td><?= $d['nama'] ?></td>
                            <td class="text-center"><?= $d['kode_kelas'] ?? '-' ?></td>
                            <td class="text-center"><?= $d['smt'] ?? '-' ?></td>
                          </tr>
                        <?php endforeach; ?>
                      </tbody>
                    </table>
                    
                    <!-- PAGINATION -->
                    <nav>
                    <ul class="pagination pagination-sm justify-content-center flex-wrap">
                    
                      <li class="page-item <?= $page<=1?'disabled':'' ?>">
                        <a class="page-link" href="/dashboard/admin-nim-mbr?page=1&search=<?= $search ?>">First</a>
                      </li>
                    
                      <li class="page-item <?= $page<=1?'disabled':'' ?>">
                        <a class="page-link" href="/dashboard/admin-nim-mbr?page=<?= $page-1 ?>&search=<?= $search ?>">Prev</a>
                      </li>
                    
                      <?php for ($i=max(1,$page-2); $i<=min($totalPage,$page+2); $i++): ?>
                        <li class="page-item <?= $i==$page?'active':'' ?>">
                          <a class="page-link" href="/dashboard/admin-nim-mbr?page=<?= $i ?>&search=<?= $search ?>">
                            <?= $i ?>
                          </a>
                        </li>
                      <?php endfor; ?>
                    
                      <li class="page-item <?= $page>=$totalPage?'disabled':'' ?>">
                        <a class="page-link" href="/dashboard/admin-nim-mbr?page=<?= $page+1 ?>&search=<?= $search ?>">Next</a>
                      </li>
                    
                      <li class="page-item <?= $page>=$totalPage?'disabled':'' ?>">
                        <a class="page-link" href="/dashboard/admin-nim-mbr?page=<?= $totalPage ?>&search=<?= $search ?>">Last</a>
                      </li>
                    
                    </ul>
                    </nav>
                    
                    </div>


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