<?php
session_start();
header('Content-Type: application/json');

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $itemId = intval($_POST['id']);
    $sessionType = $_POST['session']; // "dispose" or "request"
    $itemCategory = $_POST['category']; // Category for item identification

    // Determine session variable name based on session type
    if ($sessionType === 'dispose') {
        $sessionVar = 'dispose_items';
    } elseif ($sessionType === 'request') {
        $sessionVar = 'request';
    } else {
        echo json_encode(['status' => 'error', 'message' => 'Invalid session type.']);
        exit;
    }

    // Check if the session variable is set
    if (isset($_SESSION[$sessionVar])) {
        $items = &$_SESSION[$sessionVar];

        foreach ($items as $index => $item) {
            if ($item['id'] == $itemId && $item['category'] == $itemCategory) {
                unset($items[$index]);
                $_SESSION[$sessionVar] = array_values($items); // Re-index the array
                echo json_encode(['status' => 'success']);
                exit;
            }
        }
    }

    echo json_encode(['status' => 'error', 'message' => 'Item not found in session.']);
} else {
    echo json_encode(['status' => 'error', 'message' => 'Invalid request method.']);
}
?>
