<?php

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

$term = $_GET['term'] ?? '';
$project_id = isset($_GET['project_id']) ? (int)$_GET['project_id'] : 0;
if (strlen($term) < 2) {
    echo json_encode([]);
    exit;
}

$stmt = $conn->prepare("
    SELECT id, boq_code, system_description, quantity, uom, type, sitc_rate, supply_rate, itc_rate, remarks
    FROM boq_master 
    WHERE (boq_code LIKE CONCAT('%', ?, '%') 
       OR system_description LIKE CONCAT('%', ?, '%')) AND project_id = $project_id 
    LIMIT 10
");
$stmt->bind_param("ss", $term, $term);
$stmt->execute();

$result = $stmt->get_result();

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

header('Content-Type: application/json');
echo json_encode($data);
?>