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

require_once "../../includes/db.php"; // adjust path if needed

$search = isset($_GET['search']) ? trim($_GET['search']) : '';

if (strlen($search) < 1) {
    echo json_encode([]);
    exit;
}

$stmt = $pdo->prepare("
    SELECT id, firstname, secondname
    FROM users
    WHERE firstname LIKE :search
       OR secondname LIKE :search
    LIMIT 20
");

$like = "$search%";

$stmt->bindParam(':search', $like, PDO::PARAM_STR);
$stmt->execute();

$data = $stmt->fetchAll(PDO::FETCH_ASSOC);

echo json_encode($data);
exit;