<?php
require 'db.php'; // your PDO connection

// Get search query
$search = $_GET['q'] ?? '';

// Prepare PDO query to fetch distinct departments
$stmt = $pdo->prepare(
    "SELECT DISTINCT name
     FROM departments
     WHERE name IS NOT NULL AND name != ''
       AND name LIKE ?
     ORDER BY name
     LIMIT 20"
);

$stmt->execute(["%$search%"]);
$departments = $stmt->fetchAll(PDO::FETCH_COLUMN);

// Return as JSON
header('Content-Type: application/json');
echo json_encode($departments);
