<?php
/**
 * Get list of active departments for routing after consultation
 */
session_start();
include '../layouts/dbconn.php';

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

if (!isset($_SESSION['user_id'])) {
    echo json_encode(['success' => false, 'message' => 'Unauthorized']);
    exit();
}

try {
    // Get all active departments, excluding triage (department_id = 2 typically)
    $sql = "SELECT department_id, department_name, department_code 
            FROM departments 
            WHERE is_active = 1 
            ORDER BY display_order, department_name";
    
    $result = $conn->query($sql);
    $departments = [];
    
    while ($row = $result->fetch_assoc()) {
        $departments[] = [
            'id' => (int)$row['department_id'],
            'name' => $row['department_name'],
            'code' => $row['department_code']
        ];
    }
    
    echo json_encode([
        'success' => true,
        'departments' => $departments
    ]);
    
} catch (Exception $e) {
    echo json_encode([
        'success' => false,
        'message' => $e->getMessage()
    ]);
}
?>
