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

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

$type  = trim($_GET['type'] ?? '');
$query = trim($_GET['query'] ?? '');

if (strlen($query) < 2 || !in_array($type, ['model'])) {
    echo json_encode(['success' => false, 'items' => []]);
    exit;
}

// 🔍 Model Search
$stmt = $conn->prepare("
    SELECT id, name, model_no 
    FROM inventory_master 
    WHERE model_no LIKE ? 
    ORDER BY model_no ASC 
    LIMIT 12
");

$search = "%" . $query . "%";
$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();
?>