<?php
require_once __DIR__ . '/../config/auth.php';
header('Content-Type: application/json');

if (!isLoggedIn()) { echo json_encode([]); exit; }

$pdo = getDB();
$q = trim($_GET['q'] ?? '');
if (strlen($q) < 1) { echo json_encode([]); exit; }

$stmt = $pdo->prepare("
    SELECT id, product_code, product_name, product_type, cost_price, selling_price, current_stock
    FROM products
    WHERE status = 1 AND (product_name LIKE ? OR product_code LIKE ?)
    ORDER BY product_name
    LIMIT 20
");
$search = "%$q%";
$stmt->execute([$search, $search]);
$products = $stmt->fetchAll();

echo json_encode($products);
