<?php
include 'db.php';

$q = $_GET['q'] ?? '';
$search = '%' . $q . '%';

// Prepare the query to search by item_code, sku_number, or name
$stmt = $conn->prepare("
  SELECT 
    id, 
    CONCAT(name, ' (', item_code, ')') AS text 
  FROM items 
  WHERE name LIKE ? OR item_code LIKE ? OR sku_number LIKE ? 
  LIMIT 10
");

$stmt->bind_param("sss", $search, $search, $search);
$stmt->execute();
$result = $stmt->get_result();

// Return results as JSON array
$items = [];
while ($row = $result->fetch_assoc()) {
    $items[] = $row;
}

echo json_encode($items);
