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

<?php
//DELETE ITEM FROM CART
if(isset($_GET['action']) && $_GET['action'] == 'delete')
{
	$sql_delete = "DELETE FROM `ac_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 `ac_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'] = 'Cart has been updated successfully.';
	header("location: view-cart.php");
	exit;
}
?>
  
			<!-- page template area start -->
			<section id="about">
				<div class="container mt-0 pb-0 pt-5">
					<div class="">
						<div class="col-md-8">
						<h1 class="title mt-0 mb-10" 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
							$cart_counter = 1;
							$sql_cart = "SELECT * FROM `ac_temp_cart` WHERE `unique_id` = '".$unique_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 `ac_products` WHERE `product_id` = '".$arr_cart['product_id']."'"));
									
									$arrPhoto = mysqli_fetch_array(mysqli_query($connection,"SELECT * FROM `ac_product_photos` WHERE `product_id` = '".$fetch_product['product_id']."' AND `photo` <> '' ORDER BY `photo_id` ASC LIMIT 0,1"));
									
									
									if($arr_cart['size_id']!=0)
									{
									    $fetch_size = mysqli_fetch_array(mysqli_query($connection,"SELECT * FROM `ac_product_sizes` WHERE `size_id` = '".$arr_cart['size_id']."'"));
									}
									if($arr_cart['color_id']!=0)
									{
									    $fetch_color = mysqli_fetch_array(mysqli_query($connection,"SELECT * FROM `ac_product_colors` WHERE `color_id` = '".$arr_cart['color_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" style="border:1px solid #dbd0be; 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 stripslashes($arrPhoto['photo']);?>" style="width:150px; height:150px;">
											</div>
											<div class="col-sm-12 col-md-9">
												<p style="color:#333333; font-size:15px; margin-bottom:5px; margin-top:5px;"><b><?php echo stripslashes($fetch_product['product_name_english']);?></b></p>
												<p style="margin-bottom:5px;">Model No - <?php echo $fetch_product['model_no'];?></p>
												<?php if($arr_cart['size_id']!="0") { ?>
											    <p style="margin-bottom:5px;">Size : <?php echo $fetch_size['size'];?></p>
											    <?php } ?>
											    <?php if($arr_cart['color_id']!=0)
									{ ?>
												<p style="margin-bottom:5px;">Colour : <?php echo stripslashes($fetch_color['color']);?></p>
												<?php } ?>
												<p style="margin-bottom:5px;"><b>Price : &#8377;<?php echo stripslashes($arr_cart['unit_price']);?></b></p>
												<p>Quantity : <select name="qty" id="qty" class="form-control" style="">
												<?php 
												for($h=1;$h<=$fetch_product['total_stock'];$h++) { ?>
												<option value="<?php echo $h;?>" <?php if($arr_cart['quantity'] == $h) { ?>selected<?php } ?>><?php echo $h;?></option>
												<?php } ?>
												</select>
												</p>
												<p style="margin-bottom:5px;"><b>Subtotal : &#8377;<?php echo stripslashes($arr_cart['subtotal']);?></b></p>
												<p style="margin-bottom:0;"><button type="submit" class="btn btn-flat btn-theme-colored border-left-theme-color-2-4px">Update Cart</button><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?');"><i class="fa fa-trash-o" style="color: #ff0000; font-size: 25px; position: absolute; top: 0px; right: 10px;"></i></a></p>
											</div>
										</div>
									</div>
									<?php if($num_cart>$cart_counter) { ?>
									<div style="clear:both;">&nbsp;</div>
									<?php } ?>
									</form>
								<?php
								$cart_counter++;
								}
							}
							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 `ac_temp_cart` WHERE `unique_id` = '".$unique_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 `ac_temp_cart` WHERE `unique_id` = '".$unique_id."'"));
							$total_price = $sum_total['stot'];
							?>
							<?php if($grands_item>0) { ?>
							<div class="col-md-4">
								<div class="row">
									<div class="col-xs-12 col-sm-12 col-md-12">
										<div class="media-body" style="padding:5px;">
											<h2 style="margin:11px 0 0 0; color:#333;">Cart Summary</h2>
											<table width="100%" border="0" cellspacing="2" cellpadding="2">
												<tr>
													<td style="margin:5px; padding:5px 5px 5px 0;">Total Quantity : <?php echo $grands_item;?></td>
												</tr>
												<tr>
													<td style="margin:5px; padding:5px 5px 5px 0;">Total Amount : &#8377;<?php echo $total_price;?></td>
												</tr>
										        <tr>
											<td style="margin:5px; padding:5px 5px 5px 0;">Discount : &#8377;0.00</td>
										</tr>
										        <tr>
													<td style="margin:5px; padding:5px 5px 5px 0;">Delivery Charge : &#8377;0.00</td>
												</tr>
												<tr>
													<td style="margin:5px; padding:5px 5px 5px 0;"><strong>Grand Total : &#8377;<?php echo number_format($total_price,2);?></strong></td>
												</tr>
											</table>
											<p style="text-align:left; margin-top:5px;"><button type="button" class="btn btn-flat btn-theme-colored4 border-left-theme-color-2-4px" style="border-radius:50px; width:58%;" onclick="window.location.href='dashboard.php'">Continue Shopping</button>&nbsp;&nbsp;&nbsp;<?php if(isset($_SESSION['USER_ID'])) { ?><button type="button" class="btn btn-flat btn-theme-colored border-left-theme-color-2-4px" onclick="window.location.href='checkout.php'" style="border-radius:50px; width:38%;">Checkout</button><?php } else { ?><a class="btn btn-colored btn-theme-colored" href="login.php" style="border-radius:50px; width:38%;">Checkout</a><?php } ?></p>
										</div>
									</div>
								</div>
							</div>
							<?php } ?>
						</div>
					</div>
				</section>
			<!-- page template area end -->
  
<?php require_once("footer.php");?>
