<?php include('header.php');?>
<?php
if(isset($_SESSION['order_cod'])){
unset($_SESSION['order_cod']);
}
?>
<!-- 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">Order list</li>
		</ol>
	</div>
</section>

<section class="inner-section orderlist-part">
         <div class="container">
            <div class="row">
                   
                   <?php
                                            $discount_percentage=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)){
                                            foreach ($result as $row) {
                                                $total_price_wsc=0;
                                                $total_price_ws=0;
                                                $total_price=0;
                                                $shipping_cost=0;
                                                $discount=0;
                                                // getting ordered product data
                                                $statement1 = $this->db->query("SELECT * FROM tbl_order WHERE payment_id=?",array($row['payment_id']));
                                                $total_product = count($statement1->result_array());
                                                if($total_product){
                                                    $result1 = $statement1->result_array();
                                                    foreach ($result1 as $row1) {
                                                        $total_price += $row1['quantity']*$row1['unit_price'];
                                                    }
                                                }
                                                
                                                // 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';
                                                }
                                                
                                                // 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'];
                                                }
                                                
                                                // 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'];
                                                }
                                                // aplicable discount for this order
                                                $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;
                                                
                                                
                                        ?>
                   
               <div class="col-lg-10 mx-auto">
                  <div class="row no-gutters">
                     <div class="col-md-12">
                        <div class="card card-body">
                           <div class="widget">
                              <div class="order-list-tabel-main">
                                  
                                 <div class="row">
                                    <div class="col-md-6 col-sm-6 col-lg-6 col-6">
                                        <strong>
                                            Order status : 
                                        </strong>
                                        
                                        <h6>
                                            <?php
                                                if($row['shipping_status']=='Pending')
                                                {
                                                    echo "<strong class='text-warning'>".$row['shipping_status']."</strong>";
                                                }
                                                elseif($row['shipping_status']=='Cancelled' || $row['shipping_status']=='Returned' )
                                                {
                                                    echo "<strong class='text-danger'>".$row['shipping_status']."</strong>";
                                                }
                                                elseif($row['shipping_status']=='Arrive')
                                                {
                                                    echo "<strong class='text-success'>Shipped</strong>";
                                                }
                                                elseif($row['shipping_status']=='Delivered')
                                                {
                                                    echo "<strong class='text-success'>".$row['shipping_status']."</strong>";
                                                }
                                            ?>
                                        </h6>
                                        
                                    </div>
                                    
                                    <div class="col-md-6 col-sm-6 col-lg-6 col-6">
                                        <strong>
                                            Order Total (<?php echo $total_product;?>)
                                        </strong>
                                        
                                        <h6>
                                            <?php
                                                echo LANG_VALUE_1;
                                                
                                                // total price without copoun discount & shipping cost
                                                // echo $total_price;
                                                
                                                // total price with copoun discount & shipping cost
                                                echo $total_price_wsc;
                                            ?>
                                        </h6>
                                        
                                    </div>
                                    <div class="col-md-12 col-sm-12 col-lg-12 col-12">
                                        <div class="text-info border-bottom border-top">
                                            ORDER ID : <strong class="text-success">#<?php echo $row['payment_id'];?></strong>
                                        </div>
                                    </div>
                                    <div class="col-md-12 col-sm-12 col-lg-12 col-12">
                                        <?php
                                            // loop getting ordered product data
                                            // $q=0;
                                            foreach ($result1 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 '<div  class="border-bottom">';
                                                echo '<div  class="row">';
                                                // echo '<div  class="col-1">';
                                                // echo $q . '.';
                                                // echo '</div>';
                                                echo '<div  class="col-3">';
                                                echo '<img src="'.base_url('uploads/shop/product/'.$product_featured_photo).'" class="p-img">';
                                                echo '</div>';
                                                echo '<div  class="col-8">';
                                                echo '<strong>'.$row1['product_name'].'</strong>';
                                                echo '</div>';
                                                echo '</div>';
                                                echo '</div>';
                                            }
                                            ?>
                                    </div>
                                    <div class="col-md-12 col-sm-12 col-lg-12 col-12"><br></div>
                                        <div class="col-md-6 col-sm-6 col-lg-6 col-6">
                                            <form action="<?=base_url('orderlist/order_view');?>" method="post">
                                                <input type="hidden" name="order_id" value="<?php echo $row['payment_id'];?>" />
                                                <button type="submit" class="btn btn-md btn-primary">View Details</button>
                                            </form>
                                        </div>
                                        <div class="col-md-6 col-sm-6 col-lg-6 col-6">
                                            <a href="<?=base_url('contact');?>" class="btn btn-md float-right btn-secondary">Need Help</a>
                                        </div>
                                    </div>
                                    <!--row end-->
                              </div>
                           </div>
                        </div>
                     </div>
                  </div>
               </div>
                  <div class="col-12"><br></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>
      </section>
      
      
	  
     <?php require('footer.php');?>