<?php
// Include your database connection
include 'shikanisha.kts.php';

// Check if 'query' is set in the POST request
$query = isset($_POST['query']) ? $_POST['query'] : '';

// Query for food table
$sql_food = "SELECT `food_id` AS id, `name` AS product_name, `price` AS selling_price, NULL AS qbselling_price, 'Food' AS cat 
             FROM `food` 
             WHERE `name` LIKE '%$query%'
             ORDER BY `name` ASC";

// Query for beverages table
$sql_beverages = "SELECT `beverage_id` AS id, `beverage_name` AS product_name, `selling_price`, `qbselling_price`, 'Beverage' AS cat 
                  FROM `beverages` 
                  WHERE `beverage_name` LIKE '%$query%'
                  ORDER BY `beverage_name` ASC";

// Perform the queries
$result_food = mysqli_query($conn, $sql_food);
$result_beverages = mysqli_query($conn, $sql_beverages);

echo '<div class="section">';
echo '<div class="container">';
echo '<div class="row">';
echo '<div class="col-md-12 col-xs-12">';
echo '<div class="section-title">';
echo '<h4 class="title">Search Results</h4>';
echo '<div class="section-nav">';
echo '<div id="slick-nav-3" class="products-slick-nav"></div>';
echo '</div>';
echo '</div>';

// Check if there are results from either table
if (mysqli_num_rows($result_food) > 0 || mysqli_num_rows($result_beverages) > 0) {
    echo '<table class="table table-bordered">';
    echo '<thead>';
    echo '<tr>';
    echo '<th>Category</th>';
    echo '<th>Product Name</th>';
    echo '<th>Selling Price</th>';
    echo '<th>Action</th>';
    echo '</tr>';
    echo '</thead>';
    echo '<tbody>';
    
    // Output results from the food table
    while ($row = mysqli_fetch_assoc($result_food)) {
        echo '<tr>';
        echo '<td>' . $row['cat'] . '</td>'; 
        echo '<td>' . $row['product_name'] . '</td>';
        echo '<td>' . $row['selling_price'] . '</td>';
        // Add to cart button for food
        echo '<td><button name="cart" class="primary-btn add-to-cart-btn cta-btn" data-product-id="' . $row['id'] . '" data-product-name="' .  $row['product_name'] . '" data-selling-price="' . $row['selling_price'] . '" data-cat="' . $row['cat'] . '"><i class="fa fa-shopping-cart"></i> <b>+</b> Cart</button></td>';
        echo '</tr>';
    }
    
    // Output results from the beverages table
    while ($row = mysqli_fetch_assoc($result_beverages)) {
        echo '<tr>';
        echo '<td>' . $row['cat'] . '</td>'; 
        echo '<td>' . $row['product_name'] . '</td>';
        echo '<td>';
        echo '<select name="price_option" onchange="updatePrice(this.value, ' . $row['id'] . ')">
                <option value="' . $row['selling_price'] . '">Bottle (' . $row['selling_price'] . ')</option>';
        if (isset($row['qbselling_price'])) {
            echo '<option value="' . $row['qbselling_price'] . '">Shot (' . $row['qbselling_price'] . ')</option>';
        }
        echo '</select>';
        echo '</td>';
        // Add to cart button for beverages
        echo '<td><button name="cart" class="primary-btn add-to-cart-btn cta-btn" data-product-id="' . $row['id'] . '" data-product-name="' .  $row['product_name'] . '" data-cat="' . $row['cat'] . '"> <i class="fa fa-shopping-cart"></i> <b>+</b> Cart</button></td>';
        echo '</tr>';
    }
    
    echo '</tbody>';
    echo '</table>';

} else {
    // No results
    echo 'No results found.';
}

echo '</div>'; // Close col-md-12
echo '</div>'; // Close row
echo '</div>'; // Close container
echo '</div>'; // Close section

// Close the database connection
mysqli_close($conn);
?>
