<?php
session_start();
header('Content-Type: application/json');

if (!isset($_SESSION['user_logged_in']) || $_SESSION['user_type'] != 'siswa') {
    http_response_code(403);
    echo json_encode(['error' => 'Akses ditolak.']);
    exit();
}

include '../config/koneksi.php';

try {
    $stmt = $pdo->query("SELECT s.id_siswa, s.nama_siswa, k.nama_kelas FROM siswa s JOIN kelas k ON s.id_kelas = k.id_kelas ORDER BY s.nama_siswa");
    $siswa_list = $stmt->fetchAll(PDO::FETCH_ASSOC);
    echo json_encode($siswa_list);
} catch (PDOException $e) {
    http_response_code(500);
    echo json_encode(['error' => 'Gagal memuat data siswa: ' . $e->getMessage()]);
}
?>