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

// Check if the customer is logged in or not
if(!isset($_SESSION['customer'])) {
    header('location: '.BASE_URL.'logout.php');
    exit;
} else {
    // If customer is logged in, but admin make him inactive, then force logout this user.
    $statement = $pdo->prepare("SELECT * FROM tbl_customer WHERE cust_id=? AND cust_status=?");
    $statement->execute(array($_SESSION['customer']['cust_id'],0));
    $total = $statement->rowCount();
    if($total) {
        header('location: '.BASE_URL.'logout.php');
        exit;
    }
}

// check order id posted or not if not return order list
if(isset($_REQUEST['order_id'])){
    $payment_id = $_REQUEST['order_id'];
}
else
{
    header('location: '.BASE_URL.'orderlist.php');
}

                                            
?>
        <section class="pt-3 pb-3 page-info section-padding border-bottom bg-white">
			<div class="container">
				<div class="row">
					<div class="col-md-12">
						<a href="<?php echo BASE_URL;?>"><strong><span class="mdi mdi-home"></span> Home</strong></a>
						<span class="mdi mdi-chevron-right"></span> <a href="orderlist.php">Order List</a>
						<span class="mdi mdi-chevron-right"></span> <a href="#">Order View</a>
					</div>
				</div>
			</div>
		</section>
		
        <section class="account-page section-padding">
         <div class="container">
            <div class="row">
                   
                   <?php                    
                   
                // echo $payment_id;
                $statement = $pdo->prepare("SELECT * FROM tbl_payment WHERE payment_id=?");
                $statement->execute(array($payment_id));
                $result = $statement->fetchAll(PDO::FETCH_ASSOC);
                foreach ($result as $row) {
                                                
                                                // getting ordered product data
                                                $statement11 = $pdo->prepare("SELECT * FROM tbl_order WHERE payment_id=?");
                                                $statement11->execute(array($row['payment_id']));
                                                $total_product = $statement11->rowCount();
                                                if($total_product){
                                                    $result11 = $statement11->fetchAll(PDO::FETCH_ASSOC);
                                                    foreach ($result11 as $row11) {
                                                        $total_price += $row11['quantity']*$row11['unit_price'];
                                                    }
                                                }
                                                
                                                // getting shipping cost
                                                
                                                $statement5 = $pdo->prepare("SELECT * FROM applied_shipping_cost WHERE payment_id=? AND user_id=?");
                                                $statement5->execute(array($row['payment_id'],$_SESSION['customer']['cust_id']));
                                                $result5 = $statement5->fetchAll(PDO::FETCH_ASSOC);
                                                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 = $pdo->prepare("SELECT * FROM applied_coupon WHERE payment_id=? AND user_id=?");
                                                $statement5->execute(array($row['payment_id'],$_SESSION['customer']['cust_id']));
                                                $result5 = $statement5->fetchAll(PDO::FETCH_ASSOC);
                                                foreach ($result5 as $row5) 
                                                {
                                                    // this is coupoun id
                                                    $coupon_id = $row5['coupon_id'];
                                                }
                                                
                                                // getting copoun data from copoun id
                                                $statement5 = $pdo->prepare("SELECT * FROM tbl_coupon WHERE coupon_id=?");
                                                $statement5->execute(array($coupon_id));
                                                $result5 = $statement5->fetchAll(PDO::FETCH_ASSOC);
                                                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;
                                                
                                                
                                                // getting applied delivery address id 
                                                $statement6 = $pdo->prepare("SELECT * FROM `applied_delivery_address` WHERE `payment_id` = ?");
                                                $statement6->execute(array($row['payment_id']));
                                                $statement6_rowcount = $statement6->rowCount();
                                                if($statement6_rowcount){
                                                    $statement6_result = $statement6->fetchAll(PDO::FETCH_ASSOC);
                                                    foreach ($statement6_result as $statement6_row) {
                                                        $delivery_address_id = $statement6_row['delivery_address_id'];
                                                    }
                                                }
                                                
                                                // getting  delivery address data
                                                $statement7 = $pdo->prepare("SELECT * FROM `tbl_delivery_address` WHERE `delivery_address_id` = ?");
                                                $statement7->execute(array($delivery_address_id));
                                                $statement7_rowcount = $statement7->rowCount();
                                                if($statement7_rowcount){
                                                    $statement7_result = $statement7->fetchAll(PDO::FETCH_ASSOC);
                                                    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 = $pdo->prepare("SELECT * FROM `state` WHERE `id` = ?");
                                                $state6->execute(array($state));
                                                $state6_rowcount = $state6->rowCount();
                                                if($state6_rowcount){
                                                    $state6_result = $state6->fetchAll(PDO::FETCH_ASSOC);
                                                    foreach ($state6_result as $state6_row) {
                                                        $state = $state6_row['state'];
                                                    }
                                                }
                                                
                                                // getting district name
                                                $district6 = $pdo->prepare("SELECT * FROM `tbl_country` WHERE `country_id` = ?");
                                                $district6->execute(array($district));
                                                $district6_rowcount = $district6->rowCount();
                                                if($district6_rowcount){
                                                    $district6_result = $district6->fetchAll(PDO::FETCH_ASSOC);
                                                    foreach ($district6_result as $district6_row) {
                                                        $district = $district6_row['country_name'];
                                                    }
                                                }
                                        ?>
                                        
                                        
                <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="section-header">
                                 <h5 class="heading-design-h5">
                                    Track Order
                                 </h5>
                              </div>
                              <div class="order-list-tabel-main">
                                  
                                <div class="row">
                                    <div class="col-md-12 col-sm-12 col-lg-12 col-12">
                                        
                                            <div class="order-track-step">
                                              <div class="order-track-status">
                                                <span class="order-track-status-dot bg-success "><span class="mdi mdi-check"></span></span>
                                                <span class="order-track-status-line bg-success"></span>
                                              </div>
                                              <div class="order-track-text">
                                                <p class="order-track-text-stat ">Ordered</p>
                                                <span class="order-track-text-sub"><?php echo date('d F Y h:i:s A',strtotime($row['payment_date']));?></span>
                                              </div>
                                            </div>
                                            <?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;
                                            }
                                            ?>
                                            <div class="order-track-step">
                                              <div class="order-track-status">
                                                <span class="order-track-status-dot <?php if(!empty($row['shipping_date']) && $Delivered2==0) echo 'bg-success'; ?>">
                                                    <?php if(!empty($row['shipping_date']) && $Delivered2==0){ echo  "<span class='mdi mdi-check'></span>"; }else{ echo "<span class='mdi mdi-alarm'></span>"; } ?>
                                                </span>
                                                <span class="order-track-status-line <?php if(!empty($row['shipping_date']) && $Delivered2==0){ echo 'bg-success'; } ?>"></span>
                                              </div>
                                              <div class="order-track-text">
                                                <p class="order-track-text-stat"> Shipped</p>
                                                <span class="order-track-text-sub"><?php if(!empty($row['shipping_date']) && $Delivered2==0){ echo date('d F Y h:i:s A',strtotime($row['shipping_date']));}?></span>
                                              </div>
                                            </div>
                                            
                                            <div class="order-track-step">
                                              <div class="order-track-status">
                                                <span class="order-track-status-dot <?php if(!empty($row['delivery_date']) && $Delivered==1){ echo 'bg-success';} ?>">
                                                    <?php 
                                                    if(!empty($row['delivery_date']) && $Delivered==1)
                                                    {
                                                        echo  "<i class='mdi mdi-check'></i>"; 
                                                    }
                                                    else
                                                    {
                                                        echo "<i class='mdi mdi-alarm'></i>"; 
                                                    }
                                                    ?>
                                                </span>
                                                <span class="order-track-status-line <?php if(!empty($row['delivery_date']) && $Delivered==1){ echo 'bg-success'; } ?>"></span>
                                              </div>
                                              <div class="order-track-text">
                                                <p class="order-track-text-stat"> Delivered</p>
                                                <span class="order-track-text-sub"><?php if(!empty($row['delivery_date']) && $Delivered==1){ echo date('d F Y h:i:s A',strtotime($row['delivery_date']));}?></span>
                                              </div>
                                            </div>
                                            <?php 
                                            
                                            }
                                            else{
                                                ?>
                                                <div class="order-track-step">
                                              <div class="order-track-status">
                                                <span class="order-track-status-dot bg-danger">
                                                    <i class="mdi mdi-close"></i>
                                                </span>
                                                <span class="order-track-status-line bg-danger"></span>
                                              </div>
                                              <div class="order-track-text">
                                                <p class="order-track-text-stat"> Cancelled</p>
                                                <span class="order-track-text-sub"><?php if(!empty($row['cancelled_date'])){ echo date('d F Y h:i:s A',strtotime($row['cancelled_date']));}?></span>
                                              </div>
                                            </div>
                                                <?php
                                            }
                                            ?>
                                          
                                    </div>
                                </div>
                                    <!--row end-->
                              </div>
                           </div>
                        </div>
                     </div>
                  </div>
               </div>
                  <div class="col-12"><br></div>
                   
               <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="section-header">
                                 <h5 class="heading-design-h5">
                                    Order Details
                                 </h5>
                              </div>
                              <div class="order-list-tabel-main">
                                  
                                 <div class="row">
                                    <div class="col-md-12 col-sm-12 col-lg-12 col-12">
                                        <?php
                                            //  order data
                                            
                                                // order person name
                                                    echo '<div  class="border-bottom p-10-0">';
                                                    echo '<div  class="row">';
                                                    echo '<div  class="col-4">Name</div>';
                                                    echo '<div  class="col-8"><strong class="text-dark float-right">'.$name.'</span></strong></div>';
                                                    echo '</div>';
                                                    echo '</div>';
                                            
                                                // order id
                                                    echo '<div  class="border-bottom p-10-0">';
                                                    echo '<div  class="row">';
                                                    echo '<div  class="col-4">Order Id</div>';
                                                    echo '<div  class="col-8"><strong class="text-dark float-right">'.$payment_id.'</span></strong></div>';
                                                    echo '</div>';
                                                    echo '</div>';
                                                    
                                                // order date
                                                    echo '<div  class="border-bottom p-10-0">';
                                                    echo '<div  class="row">';
                                                    echo '<div  class="col-4">Order Date</div>';
                                                    echo '<div  class="col-8"><strong class="text-dark float-right">'.date('d M Y',strtotime($row['payment_date'])).'</span></strong></div>';
                                                    echo '</div>';
                                                    echo '</div>';
                                                    
                                                // order time
                                                    echo '<div  class="border-bottom p-10-0">';
                                                    echo '<div  class="row">';
                                                    echo '<div  class="col-6">Order Time</div>';
                                                    echo '<div  class="col-6"><strong class="text-dark float-right">'.date('h:i:s A',strtotime($row['payment_date'])).'</span></strong></div>';
                                                    echo '</div>';
                                                    echo '</div>';
                                                    
                                                // order amount
                                                    echo '<div  class="border-bottom p-10-0">';
                                                    echo '<div  class="row">';
                                                    echo '<div  class="col-6">Order Amount</div>';
                                                    echo '<div  class="col-6"><strong class="text-dark float-right">'.LANG_VALUE_1.$total_price_wsc.'</span></strong></div>';
                                                    echo '</div>';
                                                    echo '</div>';
                                                    
                                                // order payment mod
                                                    echo '<div  class="border-bottom p-10-0">';
                                                    echo '<div  class="row">';
                                                    echo '<div  class="col-6">Payment Mode</div>';
                                                    echo '<div  class="col-6"><strong class="text-dark float-right">'.$row['payment_method'].'</span></strong></div>';
                                                    echo '</div>';
                                                    echo '</div>';
                                            ?>
                                    </div>
                                    </div>
                                    <!--row end-->
                              </div>
                           </div>
                        </div>
                     </div>
                  </div>
               </div>
                  <div class="col-12"><br></div>
                   
               <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="section-header">
                                 <h5 class="heading-design-h5">
                                    Order Items (<?php echo $total_product;?>)
                                 </h5>
                              </div>
                              <div class="order-list-tabel-main">
                                  
                                 <div class="row">
                                    <div class="col-md-12 col-sm-12 col-lg-12 col-12">
                                        <?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 = $pdo->prepare("SELECT * FROM `tbl_product` WHERE `p_id` = ?");
                                                $statement_product->execute(array($product_id));
                                                $result_product = $statement_product->fetchAll(PDO::FETCH_ASSOC);
                                                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="assets/uploads/'.$product_featured_photo.'" class="p-img">';
                                                echo '</div>';
                                                echo '<div  class="col-9">';
                                                echo '<strong>';
                                                echo $row1['product_name'].'<br>';
                                                
                                                if($row1['size']!=null){
                                                    echo '<span class="text-dark">Size : '.$row1['size'].', ';
												}
												
												if($row1['color']!=null){
												    echo 'Color: '.$row1['color'].', ';
												}
												
                                                echo 'Qty : '.$row1['quantity'].',<br>';
                                                echo 'Price : ' .LANG_VALUE_1.'&nbsp;'.number_format($row1['unit_price'],2).'<br>';
                                               echo 'Total : ' .LANG_VALUE_1.'&nbsp;'.number_format($row1['quantity']*$row1['unit_price'],2);
                                                echo '</span></strong>';
                                                echo '</div>';
                                                echo '</div>';
                                                echo '</div>';
                                            }
                                            ?>
                                    </div>
                                    </div>
                                    <!--row end-->
                              </div>
                           </div>
                        </div>
                     </div>
                  </div>
               </div>
          <div class="col-12"><br></div>
          <?php
                }
            ?>
                    
                    
               <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="section-header">
                                 <h5 class="heading-design-h5">
                                    Delivery Address
                                 </h5>
                              </div>
                              <div class="order-list-tabel-main">
                                  
                                <div class="row">
                                    <div class="col-md-12 col-sm-12 col-lg-12 col-12">
                                        <?php 
                                        if($address){
                                            echo $address.', <br>'.$district.', '.$state.' '.$pin_code;
                                        }
                                        ?>
                                    </div>
                                </div>
                                    <!--row end-->
                              </div>
                           </div>
                        </div>
                     </div>
                  </div>
               </div>
                  <div class="col-12"><br></div>
                  
                  
                    
               <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">
                                            <?php 
                                            if($row['shipping_status']!='Cancelled' && $row['shipping_status']!='Returned' && $row['shipping_status']!='Delivered'){
                                            ?>
                                            <form action="order_cancle.php" method="post">
                                                <input type="hidden" name="order_id" value="<?php echo $payment_id; ?>" />
                                                <button type="submit" name="cancle_order" class="btn btn-md btn-secondary">Cancle Order</button>
                                            </form>
                                            <?php
                                            }
                                            elseif($row['shipping_status']==='Delivered')
                                            {
                                            ?>
                                            <button type="submit" class="btn btn-md bg-success text-white"><i class="mdi mdi-checkbox-marked-circle-outline"></i> Delivered </button>
                                            <?php 
                                            }
                                            else
                                            {
                                            ?>
                                            <button type="submit" class="btn btn-md bg-danger text-white"><i class="mdi mdi-close"></i> Cancelled </button>
                                            <?php 
                                            }
                                            ?>
                                        </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-primary">Need Help</a>
                                        </div>
                                    </div>
                                    <!--row end-->
                              </div>
                           </div>
                        </div>
                     </div>
                  </div>
               </div>
                  <div class="col-12"><br></div>
                  
                  
            </div>
         </div>
      </section>
      
      
	  
     <?php require('footer.php'); ?>