<?php
// search_by_model.php
// This file is used by both Delivery Challan and Purchase Order creation pages

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

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

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

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

// Search by model number (partial match)
$stmt = $conn->prepare("
    SELECT 
        id, 
        name, 
        model_no, 
        hsn_sac, 
        uom,
        description
    FROM inventory_master 
    WHERE model_no LIKE ? 
    ORDER BY model_no ASC
    LIMIT 10
");

$search_term = "%" . $model . "%";
$stmt->bind_param("s", $search_term);
$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();
?>