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

$search = trim($_GET['search'] ?? '');
$sql = "SELECT product_name, purchased_qty, unit FROM stock WHERE 1=1";
$params = [];

if ($search !== '') {
    $sql .= " AND product_name LIKE ?";
    $params[] = "%$search%";
}

$sql .= " ORDER BY product_name ASC";
$stmt = $pdo->prepare($sql);
$stmt->execute($params);
echo json_encode($stmt->fetchAll());