<?php
// Include your database connection file
include('shikanisha.kts.php'); // Update with your actual database connection file

// Get the data from the POST request
$beverage_id = isset($_POST['beverage_id']) ? intval($_POST['beverage_id']) : 0;
$beverage_name = isset($_POST['beverage_name']) ? $_POST['beverage_name'] : '';
$buying_price = isset($_POST['buying_price']) ? floatval($_POST['buying_price']) : 0;
$selling_price = isset($_POST['selling_price']) ? floatval($_POST['selling_price']) : 0;
$qtyper_bottle = isset($_POST['qtyper_bottle']) ? intval($_POST['qtyper_bottle']) : 0;
$qbprice = isset($_POST['qbprice']) ? floatval($_POST['qbprice']) : 0;
$qbselling_price = isset($_POST['qbselling_price']) ? floatval($_POST['qbselling_price']) : 0;
$t_availability = isset($_POST['t_availability']) ? $_POST['t_availability'] : '';
$description = isset($_POST['description']) ? $_POST['description'] : '';

// Validate the input
if ($beverage_id > 0 && !empty($beverage_name)) {
    // Prepare the SQL query to update the beverage details
    $query = "UPDATE beverages
              SET beverage_name = ?, buying_price = ?, selling_price = ?, qtyper_bottle = ?, qbprice = ?, qbselling_price = ?, t_availability = ?, description = ?
              WHERE beverage_id = ?";

    // Create a prepared statement
    if ($stmt = $conn->prepare($query)) {
        // Bind parameters
        $stmt->bind_param('sddidddsi', $beverage_name, $buying_price, $selling_price, $qtyper_bottle, $qbprice, $qbselling_price, $t_availability, $description, $beverage_id);

        // Execute the query
        if ($stmt->execute()) {
            $response = [
                'status' => 'success',
                'message' => 'Beverage details updated successfully.'
            ];
        } else {
            $response = [
                'status' => 'error',
                'message' => 'Failed to update beverage details.'
            ];
        }

        // Close the statement
        $stmt->close();
    } else {
        $response = [
            'status' => 'error',
            'message' => 'Failed to prepare the SQL query.'
        ];
    }
} else {
    $response = [
        'status' => 'error',
        'message' => 'Invalid input data.'
    ];
}

// Close the database connection
$conn->close();

// Output the response in JSON format
header('Content-Type: application/json');
echo json_encode($response);
?>
