<?php
require_once('source/session.php');
require_once('source/error.php');
require_once('source/dbconfig.php');
require_once('source/function.php');
require_once('source/restrict_admin.php');

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

if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
    echo json_encode(['success' => false, 'message' => 'Invalid result ID']);
    exit();
}

$result_id = $_GET['id'];

try {
    $sql = "SELECT *, subjective_score, theory_score, practical_score, total_score, grade, remark 
            FROM tbl_cbt_results 
            WHERE id = :id";
    
    $stmt = $DBH->prepare($sql);
    $stmt->execute([':id' => $result_id]);
    $result = $stmt->fetch(PDO::FETCH_ASSOC);
    
    if ($result) {
        echo json_encode(['success' => true, 'data' => $result]);
    } else {
        echo json_encode(['success' => false, 'message' => 'Result not found']);
    }
} catch (PDOException $e) {
    echo json_encode(['success' => false, 'message' => 'Database error: ' . $e->getMessage()]);
}
?>