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

if (!isAdmin()) {
    header('Content-Type: application/json');
    echo json_encode(['error' => 'Unauthorized']);
    exit;
}

try {
    $pdo = new PDO("mysql:host=localhost;dbname=ujian_system;charset=utf8", "root", "");
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    header('Content-Type: application/json');
    echo json_encode(['error' => 'Database connection failed']);
    exit;
}

$ujian_id = $_GET['ujian_id'] ?? 0;

if (!$ujian_id) {
    header('Content-Type: application/json');
    echo json_encode(['error' => 'Ujian ID required']);
    exit;
}

$stmt = $pdo->prepare("
    SELECT us.station_id, us.urutan, s.nama_station 
    FROM ujian_stations us
    JOIN stations s ON us.station_id = s.id
    WHERE us.ujian_id = ?
    ORDER BY us.urutan
");
$stmt->execute([$ujian_id]);
$stations = $stmt->fetchAll(PDO::FETCH_ASSOC);

header('Content-Type: application/json');
echo json_encode(['stations' => $stations]);
?>