<?php
session_start();
include '../koneksi.php';
include 'layout_header.php';

// 1. Ambil data periode aktif
$q_aktif = mysqli_query($koneksi, "SELECT t.id_tp_tahun, t.tahun_pelajaran, s.id_semester, s.nama_semester 
                                   FROM tahun_pelajaran t, semester s 
                                   WHERE t.status = 'Aktif' AND s.status = 'Aktif' LIMIT 1");
$aktif = mysqli_fetch_array($q_aktif);

// Gunakan nilai default jika tidak ada yang aktif
$id_tp_aktif = $aktif['id_tp_tahun'] ?? 1;
$id_sem_aktif = $aktif['id_semester'] ?? 1;
$nama_tahun = $aktif['tahun_pelajaran'] ?? 'Belum Set';
$nama_semester = $aktif['nama_semester'] ?? 'Belum Set';

// 2. Query Data Rombel dengan filter periode
$query = "SELECT r.*, g.nama as nama_guru,
          (SELECT COUNT(*) FROM anggota_rombel 
           WHERE id_rombel = r.id_rombel 
           AND id_semester = '$id_sem_aktif' 
           AND id_tp_tahun = '$id_tp_aktif') AS jml_siswa
          FROM rombel r
          LEFT JOIN master_guru g ON r.wali_kelas = g.id_guru
          WHERE r.id_semester = '$id_sem_aktif' 
          AND r.id_tp_tahun = '$id_tp_aktif'
          ORDER BY r.kelas ASC";
$sql = mysqli_query($koneksi, $query);
?>

<div class="container-fluid">
    <h3>Data Rombongan Belajar (Rombel)</h3>
    
    <div class="alert alert-primary shadow-sm">
        <div class="row">
            <div class="col-md-6">
                <h5 class="mb-0"><i class="fa fa-calendar"></i> Tahun Pelajaran: <strong><?= $nama_tahun ?></strong></h5>
            </div>
            <div class="col-md-6 text-md-end">
                <h5 class="mb-0"><i class="fa fa-book"></i> Semester: <strong><?= $nama_semester ?></strong></h5>
            </div>
        </div>
    </div>

    <div class="card shadow-sm p-4">
        <div class="d-flex justify-content-between mb-3">
            <a href="rombel_form.php" class="btn btn-success"><i class="fa fa-plus"></i> Tambah Rombel Baru</a>
        </div>

        <table class="table table-bordered table-hover">
            <thead class="bg-light">
                <tr>
                    <th>No</th>
                    <th>Nama Kelas</th>
                    <th>Wali Kelas</th>
                    <th>Jumlah Siswa</th>
                    <th>Aksi</th>
                </tr>
            </thead>
            <tbody>
                <?php
                $no = 1;
                while($d = mysqli_fetch_array($sql)) {
                ?>
                <tr>
                    <td><?php echo $no; ?></td>
                    <td><?php echo $d['kelas']; ?></td>
                    <td><?php echo $d['nama_guru'] ?? '-'; ?></td>
                    <td><?php echo $d['jml_siswa']; ?></td>
                    <td>
                        <a href="edit_rombel.php?id=<?php echo $d['id_rombel']; ?>" class="btn btn-sm btn-info" title="Lihat Daftar Siswa"><i class="fa fa-eye"></i></a>
                        <a href="rombel_form.php?id=<?php echo $d['id_rombel']; ?>" class="btn btn-sm btn-warning" title="Edit Rombel"><i class="fa fa-edit"></i></a>
                        <a href="rombel_aksi.php?hapus=<?php echo $d['id_rombel']; ?>" class="btn btn-sm btn-danger" onclick="return confirm('Yakin hapus rombel ini?')" title="Hapus Rombel"><i class="fa fa-trash"></i></a>
                    </td>
                </tr>
                <?php 
                    $no++;
                } 
                ?>
            </tbody>
        </table>
    </div>
</div>

<?php include 'layout_footer.php'; ?>