<?php
require_once '../../includes/functions.php';
requireAdmin();
$pageTitle = 'Data Kelas';
$activeMenu = 'kelas';
$pengaturan = getPengaturan();
$kelasList = db_fetchAll("
    SELECT k.*, g.nama as wali_kelas, COUNT(s.id) as jumlah_siswa
    FROM kelas k
    LEFT JOIN guru g ON g.id = k.wali_kelas_id
    LEFT JOIN siswa s ON s.kelas_id = k.id AND s.status='aktif'
    WHERE k.tahun_ajaran = ?
    GROUP BY k.id
    ORDER BY k.tingkat, k.nama_kelas
", [$pengaturan['tahun_ajaran_aktif']]);
include '../../templates/header.php';
?>
<div class="card">
  <div class="card-header">
    <span class="card-title"><i class="fas fa-door-open" style="color:var(--primary);margin-right:8px;"></i>Data Kelas</span>
    <a href="add.php" class="btn btn-primary btn-sm"><i class="fas fa-plus"></i> Tambah Kelas</a>
  </div>
  <div class="table-responsive">
    <table class="table">
      <thead>
        <tr><th>Nama Kelas</th><th>Tingkat</th><th>Wali Kelas</th><th>Jumlah Siswa</th><th>Tahun Ajaran</th><th>Aksi</th></tr>
      </thead>
      <tbody>
        <?php if (empty($kelasList)): ?>
        <tr><td colspan="6" class="text-center" style="padding:32px;color:var(--text-muted);">Belum ada kelas</td></tr>
        <?php else: ?>
        <?php foreach ($kelasList as $k): ?>
        <tr>
          <td><span class="badge badge-blue"><?= htmlspecialchars($k['nama_kelas']) ?></span></td>
          <td>Kelas <?= $k['tingkat'] ?></td>
          <td><?= htmlspecialchars($k['wali_kelas'] ?? '-') ?></td>
          <td><strong><?= $k['jumlah_siswa'] ?></strong> / <?= $k['kapasitas'] ?> siswa</td>
          <td><?= $k['tahun_ajaran'] ?> Sem <?= $k['semester'] ?></td>
          <td>
            <a href="edit.php?id=<?= $k['id'] ?>" class="btn btn-outline btn-xs"><i class="fas fa-edit"></i></a>
            <a href="<?= BASE_URL ?>/modules/siswa/list.php?kelas_id=<?= $k['id'] ?>" class="btn btn-primary btn-xs"><i class="fas fa-users"></i> Siswa</a>
            <a href="delete.php?id=<?= $k['id'] ?>" class="btn btn-danger btn-xs" data-confirm="Hapus kelas ini?"><i class="fas fa-trash"></i></a>
          </td>
        </tr>
        <?php endforeach; ?>
        <?php endif; ?>
      </tbody>
    </table>
  </div>
</div>
<?php include '../../templates/footer.php'; ?>
