<?php
/**
 * SISPENRA - Daftar Siswa
 */
require_once '../../includes/functions.php';
requireAdmin();

$pageTitle = 'Data Siswa';
$activeMenu = 'siswa';

$pengaturan = getPengaturan();
$search   = trim($_GET['search'] ?? '');
$kelasFilter = intval($_GET['kelas_id'] ?? 0);
$page     = max(1, intval($_GET['page'] ?? 1));
$perPage  = 20;
$offset   = ($page - 1) * $perPage;

$params = [];
$where  = "WHERE s.status = 'aktif'";

if ($search) {
    $where .= " AND (s.nama LIKE ? OR s.nis LIKE ? OR s.nisn LIKE ?)";
    $params[] = "%$search%"; $params[] = "%$search%"; $params[] = "%$search%";
}
if ($kelasFilter) {
    $where .= " AND s.kelas_id = ?";
    $params[] = $kelasFilter;
}

$total = db_fetch("SELECT COUNT(*) as c FROM siswa s $where", $params)['c'];

$siswas = db_fetchAll(
    "SELECT s.*, k.nama_kelas FROM siswa s LEFT JOIN kelas k ON k.id = s.kelas_id $where ORDER BY k.nama_kelas, s.nama LIMIT $perPage OFFSET $offset",
    $params
);

$kelasList = db_fetchAll("SELECT * FROM kelas WHERE tahun_ajaran = ? AND semester = ? ORDER BY tingkat, nama_kelas",
    [$pengaturan['tahun_ajaran_aktif'], $pengaturan['semester_aktif']]);

include '../../templates/header.php';
?>

<div class="card">
  <div class="card-header">
    <span class="card-title"><i class="fas fa-users" style="color:var(--primary);margin-right:8px;"></i>Data Siswa
      <span class="badge badge-blue" style="margin-left:8px;"><?= $total ?> siswa</span>
    </span>
    <div class="d-flex gap-8">
      <a href="import.php" class="btn btn-outline btn-sm"><i class="fas fa-file-excel"></i> Import Excel</a>
      <a href="add.php" class="btn btn-primary btn-sm"><i class="fas fa-plus"></i> Tambah Siswa</a>
    </div>
  </div>

  <div class="card-body" style="border-bottom:1px solid var(--border);">
    <form method="GET" style="display:flex;gap:10px;flex-wrap:wrap;">
      <input type="text" name="search" class="form-control" style="max-width:250px;" placeholder="Cari nama / NIS..." value="<?= htmlspecialchars($search) ?>">
      <select name="kelas_id" class="form-control" style="max-width:180px;">
        <option value="">Semua Kelas</option>
        <?php foreach ($kelasList as $k): ?>
        <option value="<?= $k['id'] ?>" <?= $kelasFilter == $k['id'] ? 'selected' : '' ?>><?= htmlspecialchars($k['nama_kelas']) ?></option>
        <?php endforeach; ?>
      </select>
      <button type="submit" class="btn btn-outline btn-sm"><i class="fas fa-search"></i> Cari</button>
      <a href="list.php" class="btn btn-outline btn-sm"><i class="fas fa-times"></i> Reset</a>
    </form>
  </div>

  <div class="table-responsive">
    <table class="table" id="mainTable">
      <thead>
        <tr>
          <th width="40">No</th>
          <th>NIS</th>
          <th>Nama Siswa</th>
          <th>L/P</th>
          <th>Kelas</th>
          <th>Status</th>
          <th width="120">Aksi</th>
        </tr>
      </thead>
      <tbody>
        <?php if (empty($siswas)): ?>
        <tr><td colspan="7" class="text-center" style="padding:32px;color:var(--text-muted);">
          <i class="fas fa-search" style="font-size:28px;display:block;margin-bottom:8px;opacity:.3;"></i>
          Tidak ada data siswa
        </td></tr>
        <?php else: ?>
        <?php foreach ($siswas as $i => $s): ?>
        <tr>
          <td class="text-muted"><?= $offset + $i + 1 ?></td>
          <td><code style="font-size:12px;"><?= htmlspecialchars($s['nis']) ?></code></td>
          <td>
            <div style="font-weight:600;"><?= htmlspecialchars($s['nama']) ?></div>
            <?php if ($s['nisn']): ?><div style="font-size:11px;color:var(--text-muted);">NISN: <?= $s['nisn'] ?></div><?php endif; ?>
          </td>
          <td>
            <span class="badge <?= $s['jenis_kelamin'] === 'L' ? 'badge-info' : 'badge-warning' ?>">
              <?= $s['jenis_kelamin'] === 'L' ? 'Laki-laki' : 'Perempuan' ?>
            </span>
          </td>
          <td><?= htmlspecialchars($s['nama_kelas'] ?? '-') ?></td>
          <td><span class="badge badge-success">Aktif</span></td>
          <td>
            <a href="edit.php?id=<?= $s['id'] ?>" class="btn btn-outline btn-xs" title="Edit"><i class="fas fa-edit"></i></a>
            <a href="delete.php?id=<?= $s['id'] ?>" class="btn btn-danger btn-xs" title="Hapus"
               data-confirm="Yakin hapus siswa <?= htmlspecialchars($s['nama']) ?>?">
              <i class="fas fa-trash"></i>
            </a>
          </td>
        </tr>
        <?php endforeach; ?>
        <?php endif; ?>
      </tbody>
    </table>
  </div>

  <?php if ($total > $perPage): ?>
  <div class="card-body" style="border-top:1px solid var(--border);">
    <?= paginate($total, $perPage, $page, '?search=' . urlencode($search) . '&kelas_id=' . $kelasFilter) ?>
  </div>
  <?php endif; ?>
</div>

<?php include '../../templates/footer.php'; ?>
