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

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

$coid   = $_SESSION['co_id'];

$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) AND org_id = :coid
    LIMIT 20
");

$like = "$search%";

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

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

echo json_encode($data);
exit;