<?php
// get_saved_checklists.php
require_once '../includes/auth.php';
require_once '../includes/db.php';

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

if (!isPenguji()) {
    echo json_encode(['success' => false, 'message' => 'Unauthorized']);
    exit;
}

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

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

try {
    $stmt = $pdo->prepare("
        SELECT aspek_id, checklist_item, status, catatan 
        FROM checklist_verifikasi 
        WHERE ujian_id = ? AND peserta_id = ? AND station_id = ? AND penguji_id = ?
    ");
    $stmt->execute([$ujian_id, $peserta_id, $station_id, $penguji_id]);
    $checklists = $stmt->fetchAll();
    
    echo json_encode(['success' => true, 'checklists' => $checklists]);
} catch (Exception $e) {
    echo json_encode(['success' => false, 'message' => $e->getMessage()]);
}
?>