<?php
/*
Author: Javed Ur Rehman
Website: https://www.allphptricks.com
*/

if(!isset($_SESSION)){
    session_start();
}
$status="";
if (isset($_POST['action']) && $_POST['action']=="remove"){
if(!empty($_SESSION["shopping_cart"])) {
	foreach($_SESSION["shopping_cart"] as $key => $value) {
		
		
		if($_POST["code"] == $key){
		unset($_SESSION["shopping_cart"][$key]);
		$status = "<div class='box' style='color:red;'>
		Product is removed from your cart!</div>";
		}
		if(empty($_SESSION["shopping_cart"]))
		unset($_SESSION["shopping_cart"]);
			}		
		}
				unset($_COOKIE["shopping_cart"][$key]);

}

if (isset($_POST['action']) && $_POST['action']=="change"){
  foreach($_SESSION["shopping_cart"] as &$value){
    if($value['code'] === $_POST["code"]){
        $value['quantity'] = $_POST["quantity"];
        break; // Stop the loop after we've found the product
    }
}
  	
}
?>


<div>

<h2> Shopping Cart Window</h2>   
<a  id="pull-right" href="<?php echo base_url();?>viewproductlist" class="btn btn-primary" >
Go back /Shop More </a>	
<?php
if(!empty($_SESSION["shopping_cart"])) {
$cart_count = count(array_keys($_SESSION["shopping_cart"]));
?>
<div class="cart_div">
<a href="<?php echo base_url(); ?>cart">
<img src="<?php echo base_url(); ?>images/img/cart-icon.png" /> Cart
<span><?php echo $cart_count; ?></span></a>
</div>
<?php
}
?>

<div class="cart">
<?php
if(isset($_SESSION["shopping_cart"])){
    $total_price = 0;
?>	
  <table cellpadding="2" cellspacing="2" border="1"id="sortable" style="width:90%; margin:10px;"  >

<tbody>
<tr>
<td></td>
<td>ITEM NAME</td>
<td>QUANTITY</td>
<td>UNIT PRICE</td>
<td>ITEMS TOTAL</td>
</tr>	
<?php		
foreach ($_SESSION["shopping_cart"] as $product){
	
		 $img_id=$product['image'];
	$img1='photoimghod/';		 
						$usr_result= $this->usermodel->populatepostimages($img_id);
						$url="";
							 $usr_result = $this->usermodel->populateallarray('alldocuments','img_id',$img_id);
if($usr_result[0]['name']!=""){
	
		$img=$usr_result[0]['name'];
						$url = base_url().$img1.$img;
}else{
	
		$img="images/img/cart-icon.png";
						$url =  base_url() .$img;
	
	
}

						
?>
<tr>
<td><img src='<?php echo $url; ?>' width="50" height="40" /></td>
<td><?php echo $product["name"]; ?><br />

		
<form method='post' action=''>
<input type='hidden' name='code' value="<?php echo $product["code"]; ?>" />

<input type='hidden' name='action' value="remove" />
<button type='submit' class='remove'>Remove Item</button>
</form>
</td>
<td>
<form method='post' action=''>
<input type='hidden' name='code' value="<?php echo $product["code"]; ?>" />
<input type='hidden' name='action' value="change" />
<select name='quantity' class='quantity' onchange="this.form.submit()">
<option <?php if($product["quantity"]==1) echo "selected";?> value="1">1</option>
<option <?php if($product["quantity"]==2) echo "selected";?> value="2">2</option>
<option <?php if($product["quantity"]==3) echo "selected";?> value="3">3</option>
<option <?php if($product["quantity"]==4) echo "selected";?> value="4">4</option>
<option <?php if($product["quantity"]==5) echo "selected";?> value="5">5</option>
</select>
</form>
</td>
<td><?php echo $product["price"]; ?></td>
<td><?php echo $product["price"]*$product["quantity"]; ?></td>
</tr>
<?php
$total_price += ($product["price"]*$product["quantity"]);
}
?>
<tr>
<td colspan="5" align="right">
<strong>TOTAL: <?php echo $total_price; ?></strong>
</td>
</tr>
</tbody>
</table>		
  <?php
}else{
	echo "<h3>Your cart is empty!</h3>";
	}
?>
</div>

 <a  id="pull-right" href="<?php echo base_url();?>cartcheckout" class="btn btn-warning" >
Confirm Order</a>	
   
<div style="clear:both;"></div>

<div class="message_box" style="margin:10px 0px;">
<?php echo $status; ?>
</div>


