<?php
session_start();

// Database connection
include('shikanisha.kts.php');

// Check if POST data is available
if (isset($_POST['id']) && isset($_POST['name']) && isset($_POST['category']) && isset($_POST['quantity'])) {
    $id = intval($_POST['id']);
    $name = $_POST['name'];
    $category = $_POST['category'];
    $quantity = floatval($_POST['quantity']);
    
    // Initialize session variable if not set
    if (!isset($_SESSION['dispose_items'])) {
        $_SESSION['dispose_items'] = [];
    }

    // Check if item already exists in the session
    $exists = false;
    foreach ($_SESSION['dispose_items'] as &$item) {
        if ($item['id'] === $id) {
            $item['quantity'] += $quantity;
            $exists = true;
            break;
        }
    }

    // If item does not exist, add it
    if (!$exists) {
        $_SESSION['dispose_items'][] = [
            'id' => $id,
            'name' => $name,
            'category' => $category,
            'quantity' => $quantity
        ];
    }

    // Send success response
    $response = ['status' => 'success'];
    echo json_encode($response);
} else {
    // Send error response
    $response = ['status' => 'error', 'message' => 'Invalid input data.'];
    echo json_encode($response);
}
?>
