<?php
require_once '../includes/auth.php';
require_once '../includes/config.php';

if (!isPenguji()) {
    header('Content-Type: application/json');
    echo json_encode(['success' => false, 'message' => 'Unauthorized']);
    exit;
}

$penguji_id = $_SESSION['user_id'];
$ujian_id = $_POST['ujian_id'] ?? null;
$peserta_id = $_POST['peserta_id'] ?? null;
$station_id = $_POST['station_id'] ?? null;

if (!$ujian_id || !$peserta_id || !$station_id) {
    echo json_encode(['success' => false, 'message' => 'Missing parameters']);
    exit;
}

// Ambil data global performance yang sudah disimpan
$stmt = $pdo->prepare("
    SELECT global_performance 
    FROM penilaian_global 
    WHERE ujian_id = ? AND peserta_id = ? AND station_id = ? AND penguji_id = ?
");
$stmt->execute([$ujian_id, $peserta_id, $station_id, $penguji_id]);
$global_data = $stmt->fetch();

header('Content-Type: application/json');
if ($global_data) {
    echo json_encode([
        'success' => true,
        'global_performance' => $global_data['global_performance']
    ]);
} else {
    echo json_encode([
        'success' => true,
        'global_performance' => null
    ]);
}
?>