<?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($_SESSION["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
    }
}
  	
}

?>

<link href="<?php echo base_url(); ?>assets/autocomplete/jquery-ui.css" rel="stylesheet">
    <link href="<?php echo base_url(); ?>assets/autocomplete/ui.theme.css" rel="stylesheet">
    <link href="<?php echo base_url(); ?>assets/autocomplete/autocomplete.css" rel="stylesheet">
	
 <link href="<?php echo base_url(); ?>assets/select/chosen.min.css" rel="stylesheet" media="screen">
	
		   
		   		   <script src="<?php echo base_url(); ?>assets/select/chosen.jquery.min.js"></script>


 

<script type="text/javascript">
  
		    $(function() {
<!--             $(".datepicker").datepicker(); -->
<!--             $(".uniform_on").uniform() -->;
            $(".chzn-select").chosen();
   <!--          $('.textarea').wysihtml5(); -->

        });
	
	
	</script>
	

<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>	
<a  id="pull-right" href="<?php echo base_url();?>viewproductlist" class="btn btn-warning" >
Edit/Update Shopping List</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;
?>	

<?PHP
	



 echo $this->session->flashdata('msg'); 
                  echo form_open('postneworder' , array('class' => 'form-horizontal form-groups-bordered validate',  'id' => 'signup','enctype' => 'multipart/form-data'));

		 if(validation_errors()):?>

<?php endif;?>
  <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		
$i=0;
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 />

		<input type='hidden' name='myid[]' value="<?php echo $i; ?>" />

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


</td>
<td>

<input type='text' readonly name='quantity[]' value="<?php echo $product["quantity"]; ?>" />

</td>
<td>
<input type='text' readonly name='price[]' value="<?php echo $product["price"]; ?>" />


</td>
<td><?php echo $product["price"]*$product["quantity"]; ?></td>
</tr>
<?php
$total_price += ($product["price"]*$product["quantity"]);
	$i++;	
}
?>
<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>";
	}
	
		$uniqueid = time();
?>
</div>

    <div class="form-group">
    <label for="inputfirstname" class="col-sm-2 control-label">First Name</label>
    <div class="col-sm-4">
			<input type="hidden" name="uniqueid" value="<?php echo set_value('uniqueid',$uniqueid) ;?>" id="firstname" class="form-control" required placeholder="Enter uniqueid">

		<input type="text" name="firstname" value="<?php echo set_value('firstname') ;?>" id="firstname" class="form-control" required placeholder="Enter your Other Names">
       <span class="text-danger"><?php echo form_error('firstname'); ?></span>
   </div>
   
  <label for="inputphone" class="col-sm-2 control-label">Phone Number</label>
    <div class="col-sm-4">
		<input type="text" name="phone" value="<?php echo set_value('phone') ;?>" id="othername" class="form-control" required placeholder="Enter your phone Number e.g 254716567890">
       <span class="text-danger">Enter your phone Number e.g 254716567890<?php echo form_error('phone'); ?></span>
   </div>

 </div>
  
  
   
  
  
   <div class="form-group">
    <label for="inputidno" class="col-sm-2 control-label">ID Number</label>
    <div class="col-sm-4">
		<input type="number" name="idno" value="<?php echo set_value('idno') ;?>" id="idno" class="form-control" required placeholder="Enter your ID Number e.g 10287049">
       <span class="text-danger"><?php echo form_error('idno'); ?></span>
   </div>
 
  
  
  
  
  
  
    <label for="inputemail" class="col-sm-2 control-label">Email</label>
    <div class="col-sm-4">
				<input type="email"  name="email" value="<?php echo set_value('email') ;?>" id="email" class="form-control" required placeholder="Enter Your email">
    <span class="text-danger">Enter Your email<?php echo form_error('email'); ?></span>
	</div>
  </div>

<div class="form-group">

    <label for="inputcountry" class="col-sm-2 control-label">Country </label>
    <div class="col-sm-4">
<select class="chzn-select" name="country" style="width:90%" >
	

<option value="" >---Select country---</option>
	
	<?php 
					 
			$mycountry = $this->nationmodel->populatenation();
	


if(is_array($mycountry)) {

   foreach($mycountry as $mycountryrow){

   //echo $courserow->coursename;
    ?>
					<option value="<?=  $mycountryrow->id;   ?>"><?=  $mycountryrow->name; ?></option>



	<?php

}} else {
    echo 'No country available.';
}
	
?>
	
</select>   







   <span class="text-danger"><?php echo form_error('country'); ?></span>
  
 </div>

  
 

    <label for="inputtown" class="col-sm-2 control-label"> Nearest Town </label>
    <div class="col-sm-4">
				<input type="text" name="town" value="<?php echo set_value('town'); ?>"id="town" class="form-control" required placeholder="Create your nearest town">
    <span class="text-danger">Enter town name<?php echo form_error('town'); ?></span>
   </div>
  </div>
    
 
   <div class="form-group">
    <label for="inputlocation" class="col-sm-2 control-label"> Exact Location  </label>
    <div class="col-sm-4">
				<input type="text" name="location" value="<?php echo set_value('location'); ?>"id="town" class="form-control" required placeholder="Create your nearest town">
    <span class="text-danger">Enter Exact location<?php echo form_error('location'); ?></span>
   </div>
  
    <label for="inputbox" class="col-sm-2 control-label"> Box Adress  </label>
    <div class="col-sm-4">
				<input type="text" name="box" value="<?php echo set_value('box'); ?>"id="town" class="form-control" required placeholder="Create your postal Adress">
    <span class="text-danger"> Box Adress <?php echo form_error('box'); ?></span>
   </div>
  </div>
  

    <div class="form-group">
    <label for="inputlpostcode" class="col-sm-2 control-label"> Post code  </label>
    <div class="col-sm-4">
				<input type="text" name="postcode" value="<?php echo set_value('postcode'); ?>"id="town" class="form-control" required placeholder="Create address code">
    <span class="text-danger">Enter Post code if any<?php echo form_error('postcode'); ?></span>
   </div>

    <label for="inputsalestype" class="col-sm-2 control-label"> Mode of payment  </label>
    <div class="col-sm-4">
					
	<select name="salestype" id="salestype" class="form-control" >

<option value="Cash" selected="selected">Cash On Delivery</option>
<option value="Mpesa">Mpesa</option>



<option value="Card">Card(Master/Visa card)</option>

<option value="Bank">Bank Transfer</option>


</select>										

    <span class="text-danger">  Mode of payment<?php echo form_error('salestype'); ?></span>

   
   
   </div>
  </div>
 
  
  <div class="form-group">
    <label for="inputtransactioncode" class="col-sm-3 control-label"> </label>

    <div class="col-sm-3">
     
	<button type="submit" name="btn_addorder" class="btn btn-primary" value="ordernow">Confirm Order  Now</button>

    </div>
  <label for="inputtransactioncode" class="col-sm-2 control-label">Transaction Code </label>
    <div class="col-sm-4">
				<input type="text" name="transactioncode" value="<?php echo set_value('transactioncode'); ?>"id="town" class="form-control"  placeholder="Enter Transaction code if any">
    <span class="text-danger"> Transaction Code <?php echo form_error('transactioncode'); ?></span>
   </div>
  </div>
  
   
<div style="clear:both;"></div>

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


