<?php
// search_by_model.php
require_once("../config/config.php");

header('Content-Type: application/json');

$model = trim($_GET['model'] ?? '');

if (empty($model)) {
    echo json_encode(['success' => false]);
    exit;
}

$stmt = $conn->prepare("
    SELECT id, name, model_no, uom, hsn_sac, description 
    FROM inventory_master
    WHERE model_no LIKE ? 
    ORDER BY model_no ASC
    LIMIT 10
");
$search = "%" . $model . "%";
$stmt->bind_param("s", $search);
$stmt->execute();
$result = $stmt->get_result();

$items = [];
while ($row = $result->fetch_assoc()) {
    $items[] = $row;
}

echo json_encode([
    'success' => !empty($items),
    'items' => $items
]);

$stmt->close();
$conn->close();
?>