<?php
include('header.php');
?>

<!-- start main -->
<section class="inner-section single-banner">
	<div class="container">
		<h2>Order History</h2>
		<ol class="breadcrumb">
			<li class="breadcrumb-item"><a href="index.php">Home</a></li>
			<li class="breadcrumb-item active" aria-current="page">Orderlist</li>
		</ol>
	</div>
</section>
<section class="inner-section orderlist-part">
	<div class="container">
	    <?php 
	        $statement = $this->db->query("SELECT * FROM tbl_payment WHERE customer_email=? ORDER BY id DESC",array($_SESSION['customer']['cust_email']));
            $result = $statement->result_array();
            if(count($result)){
        ?>
		<div class="row">
			<div class="col-lg-12">
				<div class="orderlist-filter">
					<h5>total order <span>- (<?=count($result);?>)</span></h5>
					<div class="filter-short"><label class="form-label">short by:</label><select class="form-select">
							<option value="all" selected>all order</option>
							<!--<option value="recieved">recieved order</option>-->
							<option value="processed">processed order</option>
							<option value="shipped">shipped order</option>
							<option value="delivered">delivered order</option>
						</select></div>
				</div>
			</div>
		</div>
		<?php 
            }
        ?>
		
		<div class="row">
			<div class="col-lg-12">
			    <?php
                    $discount_percentage=0;
                    $discount_type=0;
                    $coupon_id=0;
                    $statement = $this->db->query("SELECT * FROM tbl_payment WHERE customer_email=? ORDER BY id DESC",array($_SESSION['customer']['cust_email']));
                    $result = $statement->result_array();
                    if(count($result)){
                        $num = 0;
                        foreach ($result as $row) {
                            $db_id=$row['db_id'];
                            $payment_method=$row['payment_method'];
                            $payment_status=$row['payment_status'];
                            $num++;
                            $total_price_wsc=0;
                            $total_price_ws=0;
                            $total_price=0;
                            $total_pv=0;
                            $shipping_cost=0;
                            $discount=0;
                            // getting ordered product data
                            $statement11 = $this->db->query("SELECT * FROM tbl_order WHERE payment_id=?",array($row['payment_id']));
                            $total_product = $statement11->num_rows();
                            if($total_product){
                                $result11 = $statement11->result_array();
                                foreach ($result11 as $row11) {
                                    $total_price += $row11['quantity']*$row11['unit_price'];
                                    $total_pv += $row11['quantity']*$row11['pv'];
                                }
                            }
                            
                            // getting shipping cost
                            
                            $statement5 = $this->db->query("SELECT * FROM applied_shipping_cost WHERE payment_id=? AND user_id=?",array($row['payment_id'],$_SESSION['customer']['cust_id']));
                            $result5 = $statement5->result_array();
                            foreach ($result5 as $row5) 
                            {
                                $shipping_cost = $row5['shipping_cost'];
                            }
                            if($shipping_cost){
                                $shipping_cost2 = LANG_VALUE_1.' '.$shipping_cost;
                            }
                            else{
                                $shipping_cost2='FREE';
                            }
                             $coupon_id=0; 
                            // getting copoun when applied
                            $statement5 = $this->db->query("SELECT * FROM applied_coupon WHERE payment_id=? AND user_id=?",array($row['payment_id'],$_SESSION['customer']['cust_id']));
                            $result5 = $statement5->result_array();
                            foreach ($result5 as $row5) 
                            {

                                // this is coupoun id
                                $coupon_id = $row5['coupon_id'];
                            }
                           // echo $coupon_id.'<br>';
                            
                            $discount_percentage =0;
                                $discount_type = '';
                            // getting copoun data from copoun id
                            $statement5 = $this->db->query("SELECT * FROM tbl_coupon WHERE coupon_id=?",array($coupon_id));
                            $result5 = $statement5->result_array();
                            foreach ($result5 as $row5)
                            {
                                // copoun discount in percentage
                                $discount_percentage = $row5['percentage'];
                                $discount_type = $row5['type'];
                            }
                            
                            // aplicable discount for this order
                            if($discount_type==1){
                                $discount =  $discount_percentage;
                            }
                            if($discount_type==0){
                                $discount =  ($total_price/100)*$discount_percentage;
                            }
                            
                            // total price with shipping cost
                            $total_price_ws = $total_price+$shipping_cost;
                            
                            // total price with copoun discount & shipping cost
                            $total_price_wsc = $total_price_ws-$discount;
                            
                            
                            // getting applied delivery address id 
                            $statement6 = $this->db->query("SELECT * FROM `applied_delivery_address` WHERE `payment_id` = ?",array($row['payment_id']));
                            $statement6_num_rows = $statement6->num_rows();
                            if($statement6_num_rows){
                                $statement6_result = $statement6->result_array();
                                foreach ($statement6_result as $statement6_row) {
                                    $delivery_address_id = $statement6_row['delivery_address_id'];
                                }
                            }
                            
                            // getting  delivery address data
                            $statement7 = $this->db->query("SELECT * FROM `tbl_delivery_address` WHERE `delivery_address_id` = ?",array($delivery_address_id));
                            $statement7_num_rows = $statement7->num_rows();
                            if($statement7_num_rows){
                                $statement7_result = $statement7->result_array();
                                foreach ($statement7_result as $statement7_row) {
                                    $name = $statement7_row['name'];
                                    $phone = $statement7_row['phone'];
                                    $state = $statement7_row['state'];
                                    $district = $statement7_row['district'];
                                    $pin_code = $statement7_row['pin_code'];
                                    $address = $statement7_row['address'];
                                    $addon = date('d M Y h:i:s A', strtotime($statement7_row['addon']));
                                }
                            }
                            
                            
                            // getting state name
                            $state6 = $this->db->query("SELECT * FROM `tbl_state` WHERE `state` = ?",array($state));
                            $state6_num_rows = $state6->num_rows();
                            if($state6_num_rows){
                                $state6_result = $state6->result_array();
                                foreach ($state6_result as $state6_row) {
                                    $state = $state6_row['state'];
                                }
                            }
                            
                            // getting district name
                            $district6 = $this->db->query("SELECT * FROM `tbl_district` WHERE `district` = ?",array($district));
                            $district6_num_rows = $district6->num_rows();
                            if($district6_num_rows){
                                $district6_result = $district6->result_array();
                                foreach ($district6_result as $district6_row) {
                                    $district = $district6_row['district'];
                                }
                            }
                    ?>
                    
				<div class="orderlist">
					<div class="orderlist-head">
						<h5>order #<?=$num;?></h5>
						<h5>order recieved</h5>
					</div>
					<div class="orderlist-body">
						<div class="row">
							<div class="col-lg-12">
								<div class="order-track">
									<ul class="order-track-list">
										<!--<li class="order-track-item active"><i class="icofont-check"></i><span>order recieved</span></li>-->
										<li class="order-track-item active"><i class="icofont-check"></i><span>order processed</span> <span><?=date('d F Y h:i:s A',strtotime($row['payment_date']));?></span></li>
									    <?php 
                                            if($row['shipping_status']!='Cancelled' && $row['shipping_status']!='Returned'){
                                                $Delivered2 = 1;
                                            if($row['shipping_status'] == 'Delivered'){
                                                $Delivered = 1;
                                                $Delivered2 = 0;
                                            }
                                            else
                                            { 
                                                $Delivered = 0;
                                            }
                                            if($row['shipping_status']!='Cancelled' && $row['shipping_status']!='Returned' && $row['shipping_status']!='Delivered'){
                                                $Delivered2 = 0;
                                            }
                                            ?>
                                            <li class="order-track-item <?php if(!empty($row['shipping_date']) && $Delivered2==0) echo 'active'; ?>">
                                                <?php if(!empty($row['shipping_date']) && $Delivered2==0){echo  '<i class="icofont-check"></i>'; } else {echo '<i class="icofont-close"></i>'; }?>
                                                <span>order shipped</span>
                                                <span><?php if(!empty($row['shipping_date']) && $Delivered2==0){ echo date('d F Y h:i:s A',strtotime($row['shipping_date']));} else { echo 'N/A';}?></span>
                                            </li>
                                            
                                            <li class="order-track-item <?php if(!empty($row['delivery_date']) && $Delivered==1) echo 'active'; ?>">
                                                <?php if(!empty($row['delivery_date']) && $Delivered==1){echo  '<i class="icofont-check"></i>'; } else {echo '<i class="icofont-close"></i>'; }?>
                                                <span>order Delivered</span>
                                                <span><?php if(!empty($row['delivery_date']) && $Delivered==1){ echo date('d F Y h:i:s A',strtotime($row['delivery_date']));} else { echo 'N/A';}?></span>
                                            </li>
                                            
                                            <?php 
                                            
                                            }
                                            elseif(!empty($row['cancelled_date'])){
                                                ?>
                                                <li class="order-track-item active">
                                                    <i class="icofont-close"></i><span>order Cancelled</span> 
                                                    <span><?php if(!empty($row['cancelled_date'])){ echo date('d F Y h:i:s A',strtotime($row['cancelled_date']));}?></span>
                                                </li>
                                                <?php
                                            }
                                            elseif(!empty($row['returned_date'])){
                                                ?>
                                                
                                                <li class="order-track-item active">
                                                    <i class="icofont-close"></i><span>order Returned</span> 
                                                    <span><?php if(!empty($row['returned_date'])){ echo date('d F Y h:i:s A',strtotime($row['returned_date']));}?></span>
                                                </li>
                                                <?php
                                            }
                                            ?>
									</ul>
								</div>
							</div>
							<div class="col-lg-5">
								<ul class="orderlist-details">
									<li>
										<h6>order id</h6>
										<p>#<?=$row['payment_id'];?></p>
									</li>
									<li>
										<h6>Total Item</h6>
										<p><?=count($result11);?> Items</p>
									</li>
									<li>
										<h6>Order Time</h6>
										<p><?=date('d F Y h:i:s A',strtotime($row['payment_date']));?></p>
									</li>
									<li>
										<h6>Delivery Code</h6>
										<p><?=$row['dcode'];?></p>
									</li>
									<li>
										<h6>Delivery Time</h6>
										<p><?php if(!empty($row['delivery_date']) && $Delivered==1){ echo date('d F Y h:i:s A',strtotime($row['delivery_date']));} else { echo 'N/A';}?></p>
									</li>
									
										<li>
										<h6>Payment Method</h6>
										<p><?php echo $payment_method; ?>&nbsp;(<?php echo $payment_status; ?>)</p>
										
									</li>
								</ul>
							</div>
							
						<?php 
						if(!empty($db_id))
						{
						    
					  $queryj = $this->db->query("SELECT * FROM details where uid='$db_id' ");
                      $rowj = $queryj->row();  
						    
						?>
								<div class="col-lg-5">
								<ul class="orderlist-details">
								    
								    	<li><center><b>Delevery Boy Details</b></center></li>
									<li>
										<h6>Name</h6>
										<p><?=$rowj->name;?></p>
									</li>
									<li>
										<h6>Contact</h6>
                                <p><?=$rowj->mob;?></p>
									</li>
								
								</ul>
							</div>
						<?php } ?>	
							
							<div class="col-lg-4">
								<ul class="orderlist-details">
									<li>
										<h6>Sub Total</h6>
										<p>&#8377;<?=$total_price;?></p>
									</li>
									<li>
										<h6>discount</h6>
										<p>&#8377;<?=$discount;?></p>
									</li>
									<li>
										<h6>delivery fee</h6>
										<p><?=$shipping_cost2;?></p>
									</li>
									<li>
										<h6>self benefit</h6>
										<p><?=$total_pv;?></p>
									</li>
									<li>
										<h6>Total<!--<small>(Incl. VAT)</small>--></h6>
										<p>&#8377;<?=$total_price_wsc-$total_pv;?></p>
									</li>
									<li>
										<p class="text-right"><b class="text-success">You Save &#8377;<?=$discount+$total_pv;?></b></p>
									</li>
								</ul>
							</div>
							<div class="col-lg-3">
								<div class="orderlist-deliver">
									<h6>Delivery location</h6>
									<p>
									<?php
									    if($pin_code){
                                            echo $address.', '.$district.', '.$state.' '.$pin_code;
                                        }
                                    ?>
                                    </p>
								</div>
							</div>
							<div class="col-lg-12">
								<div class="table-scroll">
									<table class="table-list">
										<thead>
											<tr>
												<th scope="col">S.No.</th>
												<th scope="col">Product</th>
												<th scope="col">Name</th>
												<th scope="col">Price</th>
												<th scope="col">Size / Color</th>
												<th scope="col">quantity</th>
												<th scope="col">PV</th>
												<th scope="col">Total</th>
											</tr>
										</thead>
										<tbody>
										    <?php
                                                // loop getting ordered product data
                                                $q=0;
                                                foreach ($result11 as $row1) {
                                                    
                                                    // this is product id
                                                    $product_id = $row1['product_id'];
                                                    
                                                    // getting product photo
                                                    $statement_product = $this->db->query("SELECT * FROM `tbl_product` WHERE `p_id` = ?",array($product_id));
                                                    $result_product = $statement_product->result_array();
                                                    foreach ($result_product as $row_product) {
                                                        
                                                        // this is product photo
                                                        $product_featured_photo = $row_product['p_featured_photo'];
                                                    }
                                                    
                                                    
                                                    
                                                $q++;
                                                    echo '<tr>';
                                                    echo '<td class="table-serial">';
                                                    echo $q . '.';
                                                    echo '</td>';
                                                    echo '<td class="table-image">';
                                                    echo '<img src="'.base_url('uploads/shop/product/'.$product_featured_photo).'" class="img-fluid p-img">';
                                                    echo '</td>';
                                                    echo '<td class="table-name">';
                                                    echo '<h6>'.$row1['product_name'].'</h6>';
                                                    echo '</td>';
                                                    echo '<td class="table-price">';
                                                    echo '<h6>' .LANG_VALUE_1.'&nbsp;'.number_format($row1['unit_price'],2).'</h6>';
                                                    echo '</td>';
                                                    echo '<td class="table-description">';
                                                    if($row1['size']!=null){
                                                    echo '<span class="text-primary">'.$row1['size'].'</span>';
                                                    }
                                                    
                                                    if($row1['color']!=null){
                                                    echo ' &nbsp; &nbsp; <span class="badge" style="background: '.$row1['color'].';">'.$row1['color'].'</span>';
                                                    }
                                                    echo '</td>';
                                                    echo '<td class="table-quantity">';
                                                    echo '<h6>'.$row1['quantity'].'</h6>';
                                                    echo '</td>';
                                                    echo '<td class="table-PV">';
                                                    echo '<h6>'.$row1['pv'].'</h6>';
                                                    echo '</td>';
                                                    echo '<td class="table-price">';
                                                    echo '<h6>' .LANG_VALUE_1.'&nbsp;'.number_format($row1['quantity']*$row1['unit_price'],2).'</h6>';
                                                    echo '</td>';
                                                    echo '</tr>';
                                                }
                                                ?>
										</tbody>
									</table>
								</div>
							</div>
						</div>
					</div>
				</div>
				
				<?php
                        }
                    }
                    else
                    {
                        ?>
                        <div class="col-md-12 text-center">
                            <img src="<?=base_url('uploads/no-order.svg');?>" class="img-fluid" />
                            <h3>
                                <b>NO Order's Found</b>
                            </h3>
                            <div class="col-12"><br></div>
                            <div class="col-12 col-md-6 d-block mt-3 mx-auto">
                                <div class="row">
                                    <div class="col">
                                        <a href="<?=base_url();?>" class="bg-success rounded px-3 py-2 text-white">Go to Super Store</a>
                                    </div>
                                    <div class="col">
                                        <a href="<?=base_url('profile');?>" class="bg-info rounded px-3 py-2 text-white">Go to Profile</a>
                                    </div>
                                </div>
                            </div>
                        </div>
                        
                        <?php
}
                    ?>
			</div>
		</div>
		<!--<div class="row">
			<div class="col-lg-12">
				<ul class="pagination">
					<li class="page-item"><a class="page-link" href="#"><i class="icofont-arrow-left"></i></a>
					</li>
					<li class="page-item"><a class="page-link active" href="#">1</a></li>
					<li class="page-item"><a class="page-link" href="#">2</a></li>
					<li class="page-item"><a class="page-link" href="#">3</a></li>
					<li class="page-item">...</li>
					<li class="page-item"><a class="page-link" href="#">65</a></li>
					<li class="page-item"><a class="page-link" href="#"><i class="icofont-arrow-right"></i></a></li>
				</ul>
			</div>
		</div>-->
	</div>
</section>
<!-- end main -->
<style>
    .p-img{
        max-width:100px;
        max-height: 100px;
    }
</style>
<?php
include('footer.php');
?>