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

require_once "../../includes/db.php"; // adjust path if needed
session_start(['read_and_close' => true]);

$coid   = $_SESSION['co_id'];

session_write_close();

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

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

$stmt = $pdo->prepare("
    SELECT id, acc_name
    FROM accounts
    WHERE acc_name LIKE :search
    AND co_id = :coid
    AND acc_categ='cash_wallet'
    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;