<?php
header('Content-Type: application/json');
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

include 'db.php'; // $pdo

$q = $_GET['q'] ?? '';
$q = "%$q%";

try {
    $stmt = $pdo->prepare("SELECT id, name FROM departments WHERE name LIKE :q ORDER BY name ASC");
    $stmt->bindParam(':q', $q, PDO::PARAM_STR);
    $stmt->execute();

    $departments = $stmt->fetchAll();
    echo json_encode($departments);

} catch (PDOException $e) {
    http_response_code(500);
    echo json_encode(['error' => $e->getMessage()]);
}
