<?php
session_start();
include('includes/dbcon.php');

if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["item_id"]) && isset($_POST["quantity"])) {
    $item_id = intval($_POST["item_id"]);
    $quantity = intval($_POST["quantity"]);
    //$image_path = intval($_POST["image_path"]);

    // Ensure quantity is at least 1
    if ($quantity < 1) {
        echo "Invalid quantity";
        exit;
    }

    // Fetch item details
    $sql = "SELECT item_name, sales_price,image_path FROM item_master WHERE item_id = $item_id";
    $result = mysqli_query($con, $sql);

    if ($row = mysqli_fetch_assoc($result)) {
        $item_name = $row['item_name'];
        $sales_price = $row['sales_price'];
         $image_path = $row['image_path'];

        // Store in session (cart)
        if (!isset($_SESSION['cart'][$item_id])) {
            $_SESSION['cart'][$item_id] = [
                "item_name" => $item_name,
                "sales_price" => $sales_price,
                "quantity" => $quantity,
                "image_path" => $image_path
            ];
        } else {
            $_SESSION['cart'][$item_id]['quantity'] += $quantity; // Add more quantity
        }

        echo "Success";
    } else {
        echo "Product not found";
    }
} else {
    echo "Invalid request";
}
?>
