<?php
// get_sections.php

include('session_validator.php');
include('includes/db_pdo.php');

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

if (isset($_POST['class_id'])) {
    $class_id = $_POST['class_id'];
    
    try {
        // Get sections for the selected class from class_section table
        $sql = "SELECT s.section_id, s.section_name 
                FROM sections s 
                INNER JOIN class_section cs ON s.section_id = cs.section_id 
                WHERE cs.class_id = :class_id AND cs.status = 1 AND s.status = 1
                ORDER BY s.section_name";
        
        $stmt = $conn->prepare($sql);
        $stmt->bindParam(':class_id', $class_id, PDO::PARAM_INT);
        $stmt->execute();
        
        $sections = $stmt->fetchAll(PDO::FETCH_ASSOC);
        
        echo json_encode([
            'status' => 'success',
            'sections' => $sections
        ]);
    } catch (Exception $e) {
        echo json_encode([
            'status' => 'error',
            'message' => 'Failed to load sections'
        ]);
    }
} else {
    echo json_encode([
        'status' => 'error',
        'message' => 'Class ID not provided'
    ]);
}
?>