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

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

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

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