<div class="row">
    <div class="col-lg-12">
        <h3 class="page-header"><strong>Data Siswa</strong></h3>
    </div>
</div>

<div class="row">
    <div class="col-lg-12">
        <div class="panel panel-primary">

            <div class="panel-heading">
<?php
// Validasi parameter kelas
$klas = isset($_GET['kls']) ? $_GET['kls'] : 'semua';

if ($klas == "semua") {
    echo "Data Semua Siswa";
} else {
    $claris = mysqli_query($koneksi, "SELECT * FROM kelas WHERE idk='$klas'");
    $click  = mysqli_fetch_assoc($claris);
    $namaKelas = isset($click['nama']) ? $click['nama'] : '-';
    echo "Data Siswa Kelas {$namaKelas}";
}
?>
            </div>

            <div class="panel-body">
                <div class="table-responsive">
                    <table class="table table-striped table-bordered table-hover" id="dataTables-example">
                        <thead>
                            <tr>
                                <th class="text-center">NIS</th>
                                <th class="text-center" width="25%">Nama</th>
                                <th class="text-center">JK</th>
                                <th class="text-center">Kelas</th>
                                <th class="text-center">No Telepon</th>
                                <th class="text-center" width="20%">Aksi</th>
                            </tr>
                        </thead>
                        <tbody>

<?php
// Query Data Siswa
if ($klas == "semua") {
    $sql = mysqli_query($koneksi, "SELECT * FROM siswa ORDER BY nis ASC");
} else {
    $sql = mysqli_query($koneksi, "SELECT * FROM siswa WHERE idk='$klas' ORDER BY nis ASC");
}

while ($rs = mysqli_fetch_assoc($sql)) {

    // Ambil Nama Kelas
    $kelasQ = mysqli_query($koneksi, "SELECT * FROM kelas WHERE idk='{$rs['idk']}'");
    $rsw    = mysqli_fetch_assoc($kelasQ);
    $nama_kelas = isset($rsw['nama']) ? $rsw['nama'] : '-';

    // (Opsional) Filter Sekolah jika login sebagai admin_guru tertentu
    // Jika tidak perlu filter sekolah, bagian ini bisa dihapus
    if (isset($_SESSION['level']) && $_SESSION['level'] == "admin_guru") {
        if (isset($rsw['id']) && isset($sekolahQ)) {
             $sekolahQ = mysqli_query($koneksi, "SELECT * FROM sekolah WHERE id='{$rsw['id']}'");
             $rsb = mysqli_fetch_assoc($sekolahQ);
             if ($rsb['id'] != $_SESSION['id']) {
                 continue; // Lewati siswa ini jika bukan dari sekolah admin yang login
             }
        }
    }
?>
                            <tr>
                                <td class="text-center"><?= $rs['nis']; ?></td>
                                <td><?= $rs['nama']; ?></td>
                                <td class="text-center">
                                    <?= ($rs['jk'] == "L") ? "Laki-Laki" : "Perempuan"; ?>
                                </td>
                                <td class="text-center"><?= $nama_kelas; ?></td>
                                <td class="text-center"><?= $rs['tlp']; ?></td>
                                <td class="text-center">
                                    
                                    <a href="media.php?module=detail_siswa&act=details&ids=<?= $rs['ids']; ?>" 
                                       class="btn btn-warning btn-sm" title="Detail">
                                        <i class="fa fa-info-circle"></i> Detail
                                    </a>

                                    <a href="media.php?module=input_siswa&act=edit&ids=<?= $rs['ids']; ?>" 
                                       class="btn btn-info btn-sm" title="Edit">
                                        <i class="fa fa-edit"></i> Edit
                                    </a>

                                    <a href="./module/simpan.php?act=hapus_siswa&ids=<?= $rs['ids']; ?>" 
                                       class="btn btn-danger btn-sm" 
                                       onclick="return confirm('Yakin ingin menghapus data siswa bernama <?= $rs['nama']; ?>?')" 
                                       title="Hapus">
                                        <i class="fa fa-trash"></i> Hapus
                                    </a>

                                </td>
                            </tr>
<?php } ?>

                        </tbody>
                    </table>
                </div>
            </div>

        </div>
    </div>
</div>