<?php
session_start();
require_once '../config.php';

// Cek apakah user sudah login dan role-nya guru
if (!isset($_SESSION['user']) || $_SESSION['user']['role'] !== 'guru') {
    header('Content-Type: application/json');
    echo json_encode([
        'success' => false,
        'message' => 'Akses ditolak. Hanya guru yang dapat mengakses ini.'
    ]);
    exit();
}

header('Content-Type: application/json');

if (isset($_GET['nis'])) {
    $nis = $conn->real_escape_string($_GET['nis']);
    
    $sql = "SELECT * FROM ujn_siswa WHERE nis = '$nis'";
    $result = $conn->query($sql);
    
    if ($result && $result->num_rows > 0) {
        $siswa = $result->fetch_assoc();
        echo json_encode([
            'success' => true,
            'siswa' => $siswa
        ]);
    } else {
        echo json_encode([
            'success' => false,
            'message' => 'Siswa dengan NIS ' . $nis . ' tidak ditemukan'
        ]);
    }
} else {
    echo json_encode([
        'success' => false,
        'message' => 'Parameter NIS tidak ditemukan'
    ]);
}

$conn->close();
?>