<?php 
include('includes/connectdb.php');
include('includes/head.php');
?>
<body>
    <div class="page-wrapper">
        <?php include('includes/header.php') ?>
        <main class="main">
            <nav aria-label="breadcrumb" class="breadcrumb-nav">
                <div class="container">
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item"><a href="index.html"><i class="icon-home"></i></a></li>
                        <li class="breadcrumb-item active" aria-current="page">Shopping Cart</li>
                    </ol>
                </div><!-- End .container -->
            </nav>

            <div class="container">
                <div class="row">
                    <div class="col-lg-8">
                        <div class="cart-table-container">
                            <table class="table table-cart">
                                <thead>
                                    <tr>
                                        <th class="product-col">Product</th>
                                        <th class="price-col">Price</th>
                                        <th class="qty-col">Qty</th>
                                        <th>Subtotal</th>
                                    </tr>
                                </thead>
                                <tbody>
								    <?php 
									    foreach($_SESSION['cart_item'] as $key => $val):  
										$resPrice = $_SESSION['cart_item'][$key]['price'] * $_SESSION['cart_item'][$key]['qty'];
									?>
                                    <tr class="product-row">
                                        <td class="product-col">
                                            <figure class="product-image-container">
                                                <a href="product?pid=<?php echo $_SESSION['cart_item'][$key]['pid']; ?>" class="product-image">
                                                    <img src="<?php echo $_SESSION['cart_item'][$key]['image'];  ?>" alt="product">
                                                </a>
                                            </figure>
                                            <h2 class="product-title">
                                                <a href="product?pid=<?php echo $_SESSION['cart_item'][$key]['pid']; ?>"><?php echo $_SESSION['cart_item'][$key]['name'];  ?></a>
                                            </h2>
                                        </td>
                                        <td>$<?php echo number_format($_SESSION['cart_item'][$key]['price']); ?></td>
                                        <td>
                                            <input class="vertical-quantity form-control" id="qty_<?php echo $_SESSION['cart_item'][$key]['pid']; ?>" onchange="updateCart('<?php echo $_SESSION['cart_item'][$key]['pid'] ?>',<?php echo $_SESSION['cart_item'][$key]['price'] ?>,'<?php echo $_SESSION['cart_item'][$key]['name'] ?>','<?php echo $_SESSION['cart_item'][$key]['image'] ?>')" value="<?php echo $_SESSION['cart_item'][$key]['qty'] ?>" type="text">
                                        </td>
                                        <td>$<?php echo number_format($_SESSION['cart_item'][$key]['price'] * $_SESSION['cart_item'][$key]['qty']); ?></td>
                                    </tr>
                                    <tr class="product-action-row">
                                        <td colspan="4" class="clearfix">
                                            <div class="float-right">
                                                <a onclick="removeFromCart('<?php echo $_SESSION['cart_item'][$key]['pid']?>' ,<?php echo $_SESSION['cart_item'][$key]['price'] ?>, '<?php echo $_SESSION['cart_item'][$key]['name'] ?>', '<?php echo $_SESSION['cart_item'][$key]['image']?>')" title="Remove product" class="btn-remove"><span class="sr-only">Remove</span></a>
                                            </div><!-- End .float-right -->
                                        </td>
                                    </tr>
                                    <?php endforeach  ?>
                                </tbody>

                                <tfoot>
                                    <tr>
                                        <td colspan="4" class="clearfix">
                                            <div class="float-right">
                                                <a href="index" class="btn btn-outline-secondary btn-update-cart">Continue Shopping</a>
                                            </div><!-- End .float-right -->
                                        </td>
                                    </tr>
                                </tfoot>
                            </table>
                        </div><!-- End .cart-table-container -->

                        <div class="cart-discount">
                            <h4>Apply Discount Code</h4>
                            <form action="#">
                                <div class="input-group">
                                    <input type="text" class="form-control form-control-sm" placeholder="Enter discount code"  required>
                                    <div class="input-group-append">
                                        <button class="btn btn-sm btn-primary" type="submit">Apply Discount</button>
                                    </div>
                                </div><!-- End .input-group -->
                            </form>
                        </div><!-- End .cart-discount -->
                    </div><!-- End .col-lg-8 -->

                    <div class="col-lg-4">
                        <div class="cart-summary">
                            <h3>Summary</h3>

                            <table class="table table-totals">
                                <tbody>
                                    <tr>
                                        <td>Subtotal</td>
                                        <td><?php echo '$' . number_format($total_price) ?></td>
                                    </tr>

                                    <tr>
                                        <td>Tax</td>
                                        <td>$0.00</td>
                                    </tr>
                                </tbody>
                                <tfoot>
                                    <tr>
                                        <td>Order Total</td>
                                        <td><?php echo '$' . number_format($total_price); ?></td>
                                    </tr>
                                </tfoot>
                            </table>

                            <div class="checkout-methods">
                                <a href="checkout-shipping-2" class="btn btn-block btn-sm btn-primary">Go to Checkout</a>
                               <!-- <a href="#" class="btn btn-link btn-block">Check Out with Multiple Addresses</a> -->
                            </div><!-- End .checkout-methods -->
                        </div><!-- End .cart-summary -->
                    </div><!-- End .col-lg-4 -->
                </div><!-- End .row -->
            </div><!-- End .container -->

            <div class="mb-6"></div><!-- margin -->
        </main><!-- End .main -->

        <?php include('includes/footer.php') ?>
    </div><!-- End .page-wrapper -->

    <div class="mobile-menu-overlay"></div><!-- End .mobil-menu-overlay -->

    <?php include('includes/mobile-menu.php') ?>

    <a id="scroll-top" href="#top" title="Top" role="button"><i class="icon-angle-up"></i></a>

    <!-- Plugins JS File -->
    <script src="assets/js/jquery.min.js"></script>
    <script src="assets/js/bootstrap.bundle.min.js"></script>
    <script src="assets/js/plugins.min.js"></script>

    <!-- Main JS File -->
    <script src="assets/js/main.min.js"></script>
</body>

</html>
<script>
function updateCart(pid, price, product_name, product_image){
	
	var query = 'replace';
	var quantity = $('#qty_'+pid).val();
	var action = 'replace';
	
	$.ajax({
		type: "POST",
		url: "process.php",
		data: {
			query_cart : 1,
			query : query,
			product_id : pid,
			product_name : product_name,
			product_image : product_image,
			price: price,
			quantity : quantity,
			action : action
		},
		success: function(data){
			    location.reload(true);
		}
	});
}

function removeFromCart(pid, price, product_name, product_image){
	
	var query = 'remove';
	var quantity = 1;
	var action = '';
	
	$.ajax({
		type: "POST",
		url: "process.php",
		data: {
			query_cart : 1,
			query : query,
			product_id : pid,
			product_name : product_name,
			product_image : product_image,
			price: price,
			quantity : quantity,
			action : action
		},
		success: function(data){
				location.reload(true);
		}
	});
}
</script>