<?php
require 'shikanisha.kts.php'; // Adjust this to your actual database connection script

$query = $_POST['query'];

$sql = "
    (SELECT 'Beverage' AS category, beverage_id AS id, beverage_name AS name, buying_price, 'Pieces' AS unit,t_availability AS availability FROM beverages WHERE beverage_name LIKE '%$query%')
    UNION
    (SELECT 'Ingredient' AS category, id, name, buying_price,  unit,'No' AS availability FROM ingredients WHERE name LIKE '%$query%')
";

$result = $conn->query($sql);

$rows = array();
if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        $rows[] = $row;
    }
}

$conn->close();

// Log the JSON response before sending it
error_log("JSON Response: " . json_encode($rows));

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