<?php
require_once '../../config/config.php';

header('Content-Type: application/json');

try {
    $allocationId = (int)($_GET['allocation_id'] ?? 0);
    
    if ($allocationId <= 0) {
        throw new Exception('Valid allocation ID is required');
    }
    
    // Get invigilation duties for this allocation
    $stmt = $pdo->prepare("
        SELECT 
            id.duty_id,
            id.staff_id,
            id.role,
            id.status,
            s.staff_code,
            CONCAT(s.first_name, ' ', s.last_name) as staff_name,
            s.email,
            s.phone,
            h.hall_name,
            et.exam_date,
            et.session,
            et.start_time,
            et.end_time,
            sub.subject_code,
            sub.subject_name
        FROM invigilation_duty id
        JOIN staff s ON id.staff_id = s.staff_id
        JOIN halls h ON id.hall_id = h.hall_id
        JOIN exam_timetable et ON id.timetable_id = et.timetable_id
        JOIN exam_subjects es ON et.exam_subject_id = es.exam_subject_id
        JOIN subjects sub ON es.subject_id = sub.subject_id
        JOIN hall_allocations ha ON id.timetable_id = ha.timetable_id AND id.hall_id = ha.hall_id
        WHERE ha.allocation_id = ?
        ORDER BY id.role DESC, s.first_name
    ");
    
    $stmt->execute([$allocationId]);
    $duties = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    echo json_encode([
        'success' => true,
        'data' => $duties,
        'total' => count($duties)
    ]);
    
} catch (Exception $e) {
    http_response_code(500);
    echo json_encode([
        'success' => false,
        'message' => 'Error fetching invigilation duties: ' . $e->getMessage()
    ]);
}
?>




























