<?php
include_once 'shikanisha.kts.php';


// Check for the HTTP request method
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
    // Build the SQL query to get all products
    $sql = "SELECT * FROM products WHERE category IN ('Product', 'Food-Item', 'Consumable');
";

    // Execute the query and fetch the results
    $result = $conn->query($sql);
    $data = [];

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

    // Return the data as JSON
    echo json_encode($data);
} else if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Get the product ID from the AJAX request
    $product_id = isset($_POST['product_id']) ? (int)$_POST['product_id'] : 0;

    // Build the SQL query to get the product
    $sql = "SELECT * FROM products WHERE product_id = $product_id ";

    // Execute the query and fetch the result
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        $row = $result->fetch_assoc();
        // Return the result as JSON
        echo json_encode($row);
    } else {
        // Return an error message
        echo json_encode(['error' => 'Product not found']);
    }
} else {
    // Invalid request method
    http_response_code(405); // Method Not Allowed
    exit();
}

$conn->close();
?>
