<?php
// Direct connection
include('../includes/db.php');

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

if (isset($_GET['exam_id']) && isset($_GET['class_id'])) {
    $exam_id = intval($_GET['exam_id']);
    $class_id = intval($_GET['class_id']);
    
    // Get all sections for this class from class_section table
    $query = "SELECT s.section_id, s.section_name
              FROM class_section cs
              INNER JOIN sections s ON cs.section_id = s.section_id
              WHERE cs.class_id = ?
                AND cs.status = 1
                AND s.status = 1
              ORDER BY s.section_name";
    
    $stmt = $conn->prepare($query);
    $stmt->bind_param("i", $class_id);
    $stmt->execute();
    $result = $stmt->get_result();
    
    $sections = [];
    while ($row = $result->fetch_assoc()) {
        $sections[] = [
            'section_id' => $row['section_id'],
            'section_name' => $row['section_name']
        ];
    }
    
    echo json_encode($sections);
    
    $stmt->close();
} else {
    echo json_encode([]);
}

$conn->close();
?>