<?php
// ajax/get_sections.php
include('../includes/db.php');

if (isset($_GET['class_id'])) {
    $class_id = (int)$_GET['class_id'];
    
    // Query to get sections available for the selected class
    $query = "SELECT DISTINCT s.section_id, s.section_name 
              FROM sections s 
              JOIN class_section cs ON s.section_id = cs.section_id 
              WHERE cs.class_id = $class_id 
              AND cs.status = 1
              ORDER BY s.section_name";
    
    $result = $conn->query($query);
    
    $sections = array();
    if ($result->num_rows > 0) {
        while ($row = $result->fetch_assoc()) {
            $sections[] = $row;
        }
    }
    
    echo json_encode($sections);
} else {
    echo json_encode([]);
}
?>