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

header('Content-Type: application/json');

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Retrieve POST data
    $memberName = $_POST['member_name'];
    $action = $_POST['action']; // Determine if it's 'dispose-products' or 'request-products'
    $sessionType = 'dispose_items'; // This should be set according to the session type

    // Validate action
    if ($action !== 'dispose') {
        echo json_encode(['status' => 'error', 'message' => 'Invalid action.']);
        exit;
    }

    // Check if session items exist
    if (!isset($_SESSION[$sessionType]) || empty($_SESSION[$sessionType])) {
        echo json_encode(['status' => 'error', 'message' => 'No items to dispose.']);
        exit;
    }

   

    // Insert into `dispose` table
    $query = "INSERT INTO `dispose` (`disposed_by`, `date`) VALUES (?, NOW())";
    $stmt = mysqli_prepare($conn, $query);
    mysqli_stmt_bind_param($stmt, 's', $memberName);
    $success = mysqli_stmt_execute($stmt);
    $disposeId = mysqli_insert_id($conn);

    if (!$success) {
        echo json_encode(['status' => 'error', 'message' => 'Failed to insert disposal record.']);
        exit;
    }

    // Prepare to insert into `disposed_items` and update stock
    $disposeItems = $_SESSION[$sessionType];
    foreach ($disposeItems as $item) {
        $itemId = $item['id'];
        $quantity = $item['quantity'];
        $category = $item['category'];

        // Update quantity based on category
        switch ($category) {
            case 'Beverage':
                $query = "UPDATE `beverages` SET `quantity` = `quantity` - ? WHERE `beverage_id` = ?";
                break;
            case 'Ingredient':
                $query = "UPDATE `ingredients` SET `quantity` = `quantity` - ? WHERE `id` = ?";
                break;
            case 'Tots':
                $query = "UPDATE `tots` SET `quantity` = `quantity` - ? WHERE `tot_id` = ?";
                break;
            default:
                echo json_encode(['status' => 'error', 'message' => 'Invalid category.']);
                exit;
        }

        $stmt = mysqli_prepare($conn, $query);
        mysqli_stmt_bind_param($stmt, 'ii', $quantity, $itemId);
        $success = mysqli_stmt_execute($stmt);

        if (!$success) {
            echo json_encode(['status' => 'error', 'message' => 'Failed to update quantity.']);
            exit;
        }

        // Insert into `disposed_items` table
        $query = "INSERT INTO `disposed_items` (`product_name`, `quantity`, `dispose_id`, `product_id`, `category`) VALUES (?, ?, ?, ?, ?)";
        $stmt = mysqli_prepare($conn, $query);
        mysqli_stmt_bind_param($stmt, 'siiss', $item['name'], $quantity, $disposeId, $itemId, $category);
        $success = mysqli_stmt_execute($stmt);

        if (!$success) {
            echo json_encode(['status' => 'error', 'message' => 'Failed to insert disposed item record.']);
            exit;
        }
    }

    // Clear the session after successful disposal
    unset($_SESSION[$sessionType]);

    echo json_encode(['status' => 'success']);
} else {
    echo json_encode(['status' => 'error', 'message' => 'Invalid request method.']);
}
?>
