<?php require_once("header.php");?>

<?php
//DELETE ITEM FROM CART
if(isset($_GET['action']) && $_GET['action'] == 'delete')
{
	$sql_delete = "DELETE FROM `og_temp_cart` WHERE `cart_id` = '".$_REQUEST['cart_id']."'";
	$exe_delete = mysqli_query($connection,$sql_delete);
	
	$_SESSION['del_succ_msg'] = 'Item deleted successfully from your cart.';
	header("location: view-cart.php");
	exit;
}

//UPDATE ITEM FROM CART
if(isset($_POST['send']))
{
    $qty = $_REQUEST['qty'];
    $price = $_REQUEST['hid_price'];
	
	$updated_subtotal = $qty*$price;
	$sql_update = "UPDATE `og_temp_cart` SET `quantity` = '$qty',`subtotal` = '$updated_subtotal' WHERE `cart_id` = '".$_REQUEST['hid_cart_id']."'";
	$exe_update = mysqli_query($connection,$sql_update);

	$_SESSION['edit_succ_msg'] = 'Your cart has been updated successfully.';
	header("location: view-cart.php");
	exit;
}
?>

			<!-- page template area start -->
			<section id="about" style="background:#eee;">
				<div class="container mt-0 pb-10 pt-10">
					<div class="row">
						<div class="col-lg-8">
						<h1 class="title mt-5 mb-20" style="text-align:left;">My Cart</h1>
							<?php if(isset($_SESSION['del_succ_msg'])) { ?>
							<p style="text-align:left;"><img src="images/tick.png" alt="" /> <?php echo $_SESSION['del_succ_msg'];?></p>
							<?php
							unset($_SESSION['del_succ_msg']);
							}
							?>
							<?php if(isset($_SESSION['edit_succ_msg'])) { ?>
							<p style="text-align:left;"><img src="images/tick.png" alt="" /> <?php echo $_SESSION['edit_succ_msg'];?></p>
							<?php
							unset($_SESSION['edit_succ_msg']);
							}
							?>
							<?php if(isset($_SESSION['ver_succ_msg'])) { ?>
							<p style="text-align:left;"><img src="images/cross.png" alt="" /> <?php echo $_SESSION['ver_succ_msg'];?></p>
							<?php
							unset($_SESSION['ver_succ_msg']);
							}
							?>
							<?php
							$sql_cart = "SELECT * FROM `og_temp_cart` WHERE `unique_id` = '".$session_id."' ORDER BY `cart_id` DESC";
							$exe_cart = mysqli_query($connection,$sql_cart);
							$num_cart = mysqli_num_rows($exe_cart);
							if($num_cart>0)
							{
								while($arr_cart = mysqli_fetch_array($exe_cart))
								{
									$fetch_product = mysqli_fetch_array(mysqli_query($connection,"SELECT * FROM `og_products` WHERE `product_id` = '".$arr_cart['product_id']."'"));
								?>		
								<form action="" method="post" enctype="multipart/form-data">
									<input type="hidden" name="send" id="send" value="1" />
									<input type="hidden" name="hid_cart_id" id="hid_cart_id" value="<?php echo stripslashes($arr_cart['cart_id']);?>" />
									<input type="hidden" name="hid_price" id="hid_price" value="<?php echo stripslashes($arr_cart['unit_price']);?>" />
									<div class="col-sm-12 col-md-12 mb-20" style="border:1px solid #ddd; background-color:#fff; padding:10px;">
										<div class="col-sm-12 col-md-12" style="padding:5px;">
											<div class="col-sm-12 col-md-3">
												<img alt="" src="uploads/product/<?php echo $fetch_product['product_photo'];?>" style="width:150px; height:150px;">
											</div>
											<div class="col-sm-12 col-md-9">
												<h4 class="mt-10 mb-5" style="color:#333"><?php echo stripslashes($fetch_product['product_name_english']);?></h4>
												<h4 class="mt-0 mb-5" style="color:#34A853;"><?php echo stripslashes($fetch_product['weight']);?></h4>
												<h4 class="mt-0 mb-5">Price : &pound;<?php echo stripslashes($arr_cart['unit_price']);?></h4>
												<p>Quantity : <select name="qty" id="qty" onchange="this.form.submit();">
												<?php for($h=1;$h<=100;$h++) { ?>
												<option value="<?php echo $h;?>" <?php if($arr_cart['quantity'] == $h) { ?>selected<?php } ?>><?php echo $h;?></option>
												<?php } ?>
												</select>
												</p>
												<h4 class="mt-0 mb-5">Subtotal : &pound;<?php echo stripslashes($arr_cart['subtotal']);?></h4>
												<p style="margin-bottom:0;"><!--<button type="submit" class="btn btn-flat btn-theme-colored border-left-theme-color-2-4px">Update Cart</button>&nbsp;--><a href="view-cart.php?cart_id=<?php echo stripslashes($arr_cart['cart_id']);?>&action=delete" onclick="return confirm('Are you sure you want to delete this item?');"><i class="fa fa-trash-o" style="color: #ff0000; font-size: 25px; position: absolute; top: 73px; right: 10px;"></i></a></p>
											</div>
										</div>
									</div>
									</form>
								<?php
								}
							}
							else
							{
							?>
							<p style="color:#ff0000;">Your cart is empty!</p>
							<?php
							}
							?>
							</div>
							<?php
							$sum_item = mysqli_fetch_array(mysqli_query($connection,"SELECT SUM(`quantity`) as `qtyy` FROM `og_temp_cart` WHERE `unique_id` = '".$session_id."'"));
							$grands_item = $sum_item['qtyy'];
							if($grands_item!="")
							{
								$grands_item = $grands_item;
							}
							else
							{
								$grands_item = 0;
							}
							
							$sum_total = mysqli_fetch_array(mysqli_query($connection,"SELECT SUM(`subtotal`) as `stot` FROM `og_temp_cart` WHERE `unique_id` = '".$session_id."'"));
							$total_price = $sum_total['stot'];
							
							$delivery_charge_display = $shipping_charge;
							
							if(isset($_SESSION['USER_ID']))
							{
							    $num_purchase = mysqli_num_rows(mysqli_query($connection,"SELECT * FROM `og_orders` WHERE `user_id` = '".$_SESSION['USER_ID']."' AND `transaction_completed` = 'Yes'"));
							    if($num_purchase>0)
							    {
							        $discount_display = number_format((($total_price*$discount)/100),2);
							        $grand_total = ($total_price-$discount_display)+$delivery_charge_display;
							    }
							    else
							    {
							       $discount_display = '0.00';
							       $grand_total = $total_price+$delivery_charge_display;
							    }
							}
							else
							{
							    $discount_display = '0.00';
							    $grand_total = $total_price+$delivery_charge_display;
							}
							?>
							<?php if($grands_item>0) { ?>
							<div class="col-lg-4">
								<div class="row">
									<div class="col-xs-12 col-sm-12 col-md-12 mt-10">
										<div class="media-body" style="border:1px solid #ddd; padding:5px; background:#fff;">
											<h2 style="margin:5px;">Cart Summary</h2>
											<table width="100%" border="0" cellspacing="2" cellpadding="2">
												<tr>
													<td style="margin:5px; padding:5px;">Total Quantity : <?php echo $grands_item;?></td>
												</tr>
												<tr>
													<td style="margin:5px; padding:5px;">Total Amount : &pound;<?php echo $total_price;?></td>
												</tr>
												<tr>
													<td style="margin:5px; padding:5px;">Discount : &pound;<?php echo $discount_display;?></td>
												</tr>
												<tr>
													<td style="margin:5px; padding:5px;">Shipping Charge : &pound;<?php echo $delivery_charge_display;?></td>
												</tr>
												<tr>
													<td style="margin:5px; padding:5px;"><strong>Grand Total : &pound;<?php echo number_format($grand_total,2);?></strong></td>
												</tr>
											</table>
											<p style="text-align:left;"><button type="button" class="btn btn-flat btn-theme-colored2 border-left-theme-color-2-4px" style="padding:8px 11px;" onclick="window.location.href='<?php echo $redirect_url;?>'">Continue Shopping</button>&nbsp;&nbsp;<button type="button" class="btn btn-flat btn-theme-colored border-left-theme-color-2-4px" onclick="window.location.href='checkout.php'">Checkout</button></p>
										</div>
									</div>
								</div>
							</div>
							<?php } ?>
						</div>
					</div>
				</section>
			<!-- page template area end -->
  
<?php require_once("footer.php");?>
