<?php
include 'db.php';

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

if ($search) {
  $stmt = $conn->prepare("SELECT i.id, i.name, i.cost_price, i.retail_price, i.bulk_price, i.bulk_qty, i.item_type,
                          COALESCE(tr.rate, 0) AS tax_rate
                          FROM items i
                          LEFT JOIN tax_rates tr ON i.tax_rate_id = tr.id
                          WHERE i.item_code = ? OR i.sku_number = ? OR i.name LIKE CONCAT('%', ?, '%')
                          LIMIT 1");
  $stmt->bind_param("sss", $search, $search, $search);
  $stmt->execute();
  $res = $stmt->get_result();
  $data = $res->fetch_assoc() ?: [];
}

echo json_encode($data);
?>
