<?php
require_once '../includes/config.php';

$school_id = isset($_GET['school_id']) ? intval($_GET['school_id']) : 0;

$departments = [];
if ($school_id > 0) {
    $sql = "SELECT id, code, name FROM departments 
            WHERE school_id = ? AND status = 1 
            ORDER BY code";
    
    $stmt = mysqli_prepare($conn, $sql);
    mysqli_stmt_bind_param($stmt, 'i', $school_id);
    mysqli_stmt_execute($stmt);
    $result = mysqli_stmt_get_result($stmt);
    
    while ($row = mysqli_fetch_assoc($result)) {
        $departments[] = $row;
    }
    mysqli_stmt_close($stmt);
}

header('Content-Type: application/json');
echo json_encode($departments);
?>