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

// --- DEFINISIKAN VARIABEL DI SINI, SEBELUM DIGUNAKAN ---
$q_sem = mysqli_query($koneksi, "SELECT id_semester FROM semester WHERE status = 'Aktif' LIMIT 1");
$sem = mysqli_fetch_array($q_sem);
$id_sem_aktif = $sem['id_semester'] ?? 0; // Inisialisasi

$q_tp = mysqli_query($koneksi, "SELECT id_tp_tahun FROM tahun_pelajaran WHERE status = 'Aktif' LIMIT 1");
$tp = mysqli_fetch_array($q_tp);
$id_tp_aktif = $tp['id_tp_tahun'] ?? 0; // Inisialisasi
$id_rombel = isset($_GET['id']) ? (int)$_GET['id'] : 0;

$query_rombel = mysqli_query($koneksi, "SELECT * FROM rombel WHERE id_rombel = '$id_rombel'");
$data_rombel = mysqli_fetch_array($query_rombel);

if (!$data_rombel) {
    echo "<div class='alert alert-danger'>Data kelas tidak ditemukan!</div>";
    include 'layout_footer.php';
    exit;
}
?>

<div class="container-fluid">
    <div class="mb-3">
        <h3>Edit Anggota Rombel: <?= htmlspecialchars($data_rombel['kelas'] ?? 'Kelas Tidak Ditemukan') ?></h3>
        <p class="text-muted small"><em>Tips: Klik dua kali untuk memindahkan siswa.</em></p>
    </div>
    
    <div class="row">
        <div class="col-md-6">
            <div class="card border-danger shadow-sm">
                <div class="card-header bg-danger text-white">Siswa Belum Masuk (Semester Aktif)</div>
                <ul class="list-group list-group-flush">
                    <?php 
                    // Siswa yang belum masuk di semester & TP aktif
                    $sql_blm = "SELECT * FROM master_siswa 
            WHERE status = 'Aktif' 
            AND id_siswa NOT IN (
                SELECT id_siswa FROM anggota_rombel 
                WHERE id_semester = '$id_sem_aktif' AND id_tp_tahun = '$id_tp_aktif'
            ) ORDER BY nama ASC";
                    $s = mysqli_query($koneksi, $sql_blm);
                    while($d = mysqli_fetch_array($s)) {
                        $url = "rombel_aksi.php?tambah=$d[id_siswa]&id_rombel=$id_rombel";
                        echo "<li class='list-group-item siswa-item' data-url='$url' style='cursor:pointer;'>$d[nama]</li>";
                    }
                    ?>
                </ul>
            </div>
        </div>

        <div class="col-md-6">
            <div class="card border-success shadow-sm">
                <div class="card-header bg-success text-white">Siswa Dalam Kelas</div>
                <ul class="list-group list-group-flush">
                    <?php 
                    // Siswa yang sudah masuk di semester & TP aktif
                    $sql_sdh = "SELECT s.nama, s.id_siswa 
                                FROM anggota_rombel a 
                                JOIN master_siswa s ON a.id_siswa=s.id_siswa 
                                WHERE a.id_rombel='$id_rombel' 
                                AND a.id_semester = '$id_sem_aktif' 
                                AND a.id_tp_tahun = '$id_tp_aktif' 
                                ORDER BY s.nama ASC";
                    $a = mysqli_query($koneksi, $sql_sdh);
                    while($d = mysqli_fetch_array($a)) {
                        $url = "rombel_aksi.php?hapus=$d[id_siswa]&id_rombel=$id_rombel";
                        echo "<li class='list-group-item list-group-item-success siswa-item' data-url='$url' style='cursor:pointer;'>$d[nama]</li>";
                    }
                    ?>
                </ul>
            </div>
        </div>
    </div>
</div>

<script>
document.querySelectorAll('.siswa-item').forEach(item => {
    item.addEventListener('dblclick', function() {
        this.innerHTML = '<div class="spinner-border spinner-border-sm text-primary" role="status"></div> Memproses...';
        this.style.pointerEvents = 'none';
        window.location.href = this.getAttribute('data-url');
    });
});
</script>

<?php 
include 'layout_footer.php'; 
ob_end_flush();
?>