<?php
require_once __DIR__ . '/config/db.php';

$q = trim($_GET['q'] ?? '');
$out = ['success'=>false,'results'=>[]];

if($q){
  $stmt = $pdo->prepare("SELECT id, sku, name_ar AS name, price, cost, stock FROM products 
                         WHERE name_ar LIKE ? OR name_en LIKE ? OR sku LIKE ? LIMIT 15");
  $stmt->execute(["%$q%","%$q%","%$q%"]);
  $out['results'] = $stmt->fetchAll(PDO::FETCH_ASSOC);
  $out['success'] = true;
}

header('Content-Type: application/json');
echo json_encode($out);
