<?php  
session_start();
include 'config.php';
if(isset($_POST['update'])){				   
    $qty=$_POST['quant'];//exit;
//   print_r($qty);
    //print_r($cat_arr);exit;
    // echo "add";
    function updateQuantity($productId, $quantity)
	{
		if (isset($_SESSION['cart'][$productId])) {
			if ($quantity > 0) {
			    
				$_SESSION['cart'][$productId]['quantity'] = $quantity;
			} else {
				// If quantity is 0 or less, remove the product from the cart
				// removeProduct($productId);
			}
		}
	}
    foreach($qty as $productId=>$p_det)
    { 
     $quantity=addslashes(trim($p_det['qty']));//exit;
       
        updateQuantity($productId, $quantity);
    }    
 
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <?php include_once("assets/include/head.php")?>
</head>
<body>
    <div id="main">
        <?php include_once("assets/include/header.php")?>
        <!-- sub-banner started... -->
        <section class="breadcrumb-area">
            <picture>
                <source media="(min-width: 768px)" srcset="assets/images/banner-2.jpg">
                <source media="(max-width: 767px)" srcset="assets/images/banner-2.jpg">
                <img src="#" alt="Sub Banner image">
            </picture>
            <div class="container">
                <div class="row">
                    <div class="col-md-12">
                        <div class="breadcrumb-content">
                            <ul class="breadcrumb-links">
                                <li><a href="index.php">Home</a></li>
                                <li>Cart</li>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </section>
        <!-- sub-banner ended... -->
        <!-- cart area start -->
        <div class="cart-main-area mtb-60px">
            <div class="container">
                <h3 class="cart-page-title">Your cart items</h3>
                <div class="row">
                    <div class="col-lg-12 col-md-12 col-sm-12 col-12">
                        <form action="#" method="POST">
                            <div class="table-content table-responsive cart-table-content">
                                <table>
                                    <thead>
                                        <tr>
                                            <th>Image</th>
                                            <th>Product Name</th>
                                            <th>Until Price</th>
                                            <th>Qty</th>
                                            <th>Subtotal</th>
                                            <th>Action</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php $a=0;
                                            	 foreach ($_SESSION['cart'] as $pid => $value){
                                                 $que = mysqli_query($con, "SELECT * FROM tbl_product where p_id='$pid'");
                                                 $row1 = mysqli_fetch_array($que);
                                         ?>
                                        
                                        <tr>
                                            <td class="product-thumbnail">
                                                <a href="#"><img src="<?php path() ?>/assets/images/product-image/<?php echo $value['image']; ?>" alt="" /></a>
                                            </td>
                                            <td class="product-name"><a href="#"><?php echo $row1['p_name']; ?></a></td>
                                            <td class="product-price-cart"><span class="amount">INR <b><?php echo $row1['p_current_price'];?></b></span></td>
                                            <td class="product-quantity">
                                                <div class="cart-plus-minus">
                                                    <input class="cart-plus-minus-box" type="text" name="quant[<?php echo $row1['p_id'];?>][qty]" value="<?php echo $value['quantity']; ?>" />
                                                </div>
                                            </td>
                                            <td class="product-subtotal">INR <b><?php echo $t = $row1['p_current_price'] * $value['quantity']; ?></b></td>
                                            <td class="product-remove">
                                                <div class="shopping-cart-delete" data-id="<?php echo $pid  ?>">
                                                    <button onclick="removeCart(this)" class="remove"><i class="fa fa-times"></i></button>
                                                    </div>
                                                </div>
                                               
                                            </td>
                                        </tr>
                                        <?php $a=$a+$t;} ?>
                                        
                                    </tbody>
                                </table>
                            </div>
                            <div class="row">
                                <div class="col-lg-12">
                                    <div class="cart-shiping-update-wrapper">
                                        <div class="cart-shiping-update">
                                            <a href="<?php  path(); ?>">Continue Shopping</a>
                                        </div>
                                        <div class="cart-clear">
                                            <button type="submit" name="update" class="clear_shopping">Update Cart</button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </form>
                        <div class="row">
                            <div class="col-lg-6">
                                <img class="responsive-img h-100" src="assets/images/banner-2.jpg" alt="">
                            </div>
                            <div class="col-lg-6">
                                <div class="grand-totall">
                                    <div class="title-wrap">
                                        <h4 class="cart-bottom-title section-bg-gary-cart">Cart Total</h4>
                                    </div>
                                    <h4 class="grand-totall-title">Grand Total <span>INR <b><?php echo $a; ?></b></span></h4>
                                    <a href="<?php path();?>/checkout.php">Proceed to Checkout</a>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <!-- cart area end -->
        <?php include_once("assets/include/footer.php")?>
    </div>
</body>
</html>