<?php
header('Content-Type: application/json');
require_once("../../includes/config.php");

// 1. Get the class_id from the GET request
$class_id = filter_input(INPUT_GET, 'class_id', FILTER_VALIDATE_INT);

try {
    if ($class_id) {
        // 2. Fetch only sections linked to the specific class
        $stmt = $pdo->prepare("SELECT id, section_name FROM sections WHERE class_id = ? AND status = 1 ORDER BY section_name ASC");
        $stmt->execute([$class_id]);
    } else {
        // Fallback: If no class_id is provided, you might want to return nothing or all active sections
        $stmt = $pdo->query("SELECT id, section_name FROM sections WHERE status = 1 ORDER BY section_name ASC");
    }

    $sections = $stmt->fetchAll(PDO::FETCH_ASSOC);
    echo json_encode(['status' => 'success', 'data' => $sections]);

} catch (PDOException $e) {
    echo json_encode(['status' => 'error', 'message' => $e->getMessage()]);
}
?>