<?php
require_once '../config/database.php';

if (isset($_GET['id_tahunajaran'])) {
    $id_tahunajaran = mysqli_real_escape_string($conn, $_GET['id_tahunajaran']);
    
    // Query untuk mendapatkan data siswa berdasarkan tahun ajaran
    $query = "SELECT s.*, ds.status 
              FROM bind_siswa s 
              JOIN bind_datasiswa ds ON s.id_siswa = ds.id_siswa 
              WHERE ds.id_tahunajaran = '$id_tahunajaran' 
              ORDER BY s.nama_lengkap ASC";
    $result = mysqli_query($conn, $query);
    
    if ($result && mysqli_num_rows($result) > 0) {
        echo '<div class="table-responsive">';
        echo '<table class="table table-sm table-striped">';
        echo '<thead>';
        echo '<tr>';
        echo '<th>No</th>';
        echo '<th>NISN</th>';
        echo '<th>Nama Lengkap</th>';
        echo '<th>Jenis Kelamin</th>';
        echo '<th>Status</th>';
        echo '</tr>';
        echo '</thead>';
        echo '<tbody>';
        
        $no = 1;
        while ($row = mysqli_fetch_assoc($result)) {
            $jenis_kelamin = $row['jenis_kelamin'] == 'L' ? 'Laki-laki' : 'Perempuan';
            $status_badge = '';
            
            switch($row['status']) {
                case 'Aktif':
                    $status_badge = '<span class="badge bg-success">Aktif</span>';
                    break;
                case 'Lulus':
                    $status_badge = '<span class="badge bg-primary">Lulus</span>';
                    break;
                case 'Keluar':
                    $status_badge = '<span class="badge bg-warning">Keluar</span>';
                    break;
                case 'Mutasi':
                    $status_badge = '<span class="badge bg-info">Mutasi</span>';
                    break;
                default:
                    $status_badge = '<span class="badge bg-secondary">' . $row['status'] . '</span>';
            }
            
            echo "<tr>";
            echo "<td>{$no}</td>";
            echo "<td>" . htmlspecialchars($row['nisn']) . "</td>";
            echo "<td>" . htmlspecialchars($row['nama_lengkap']) . "</td>";
            echo "<td>{$jenis_kelamin}</td>";
            echo "<td>{$status_badge}</td>";
            echo "</tr>";
            $no++;
        }
        
        echo '</tbody>';
        echo '</table>';
        echo '</div>';
    } else {
        echo '<div class="alert alert-info">Tidak ada data siswa untuk tahun ajaran ini.</div>';
    }
} else {
    echo '<div class="alert alert-danger">ID Tahun Ajaran tidak valid.</div>';
}
?>