<?php
$this->load->view("top");
$fieldType = $this->session->userdata('field_type');
$this->session->set_userdata('status', $this->input->get_post('order_status'));
?>
<style>
    .form-control {
    display: block;
    width: 35%;
    padding: 0.375rem 0.75rem;
    font-size: 1rem;
    font-weight: 400;
    line-height: 1.5;
    color: #212529;
    background-color: #fff;
    background-clip: padding-box;
    border: 1px solid #ced4da;
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
    border-radius: 0.25rem;
    transition: border-color .15s ease-in-out,box-shadow .15s ease-in-out;
}
</style>
<div class="clearfix"></div>
<section class="banner_category" style="background-image: url('<?php echo theme_url(); ?>images/listing-banner.jpg');">
  <div class="container">

    <h1 class="banner_title">My Orders</h1>
  </div>
</section>
<div class="page_breadcrumbs">
  <div class="container">
    <ul>
      <li><a href="<?= site_url(); ?>" title="Home">Home</a>
      </li>
      <li>My Orders</li>
    </ul>
  </div>
</div>
<section class="my_account_page">
  <div class="container">

    <div class="row">

      <div class="col-md-3">
        <?php $this->load->view('members/left'); ?>
      </div>
      <div class="col-md-9">
        <div class="right_side">
          <h1>My Orders</h1>
             <div style="text-align: -webkit-right;padding: 12px 0px;">
                     <form method="post" action="" id="orderForm">
                             <label>Search Order :</label>
                         <select name="order_status" onchange="$('#orderForm').submit();" class="form-control">
                            <option >-Select-</option>
                            <option value="0" <?php if($this->session->userdata('status')==0){ echo "selected";}?>>Placed Order</option>
                            <option value="1" <?php if($this->session->userdata('status')==1){ echo "selected";}?>>Confirmed Order</option>
                            <option value="2" <?php if($this->session->userdata('status')==2){ echo "selected";}?>>Dispatched Order</option>
                            <option value="8" <?php if($this->session->userdata('status')==8){ echo "selected";}?>>Delivered Order</option>
                            <option value="5" <?php if($this->session->userdata('status')==5){ echo "selected";}?>>Cancelled Order</option>
                            <option value="6" <?php if($this->session->userdata('status')==6){ echo "selected";}?>>Returned Order</option>
                        </select>
                    </form>
                    </div>
          <div class="wps_right">
           
            <?php echo error_message();
            echo validation_message();
            if (is_array($orders) && !empty($orders)) {
            ?>
              <div class="my_account" id="example">
                <div class="my_account my_order">
                  <!--- Start Here-->
                  <?php foreach ($orders as $key => $val) {
              $orderDets = $this->order_model->get_order_detail($val['order_id']);
              $orderDetail = $this->db->query("select * from wps_order where order_id=".$val['order_id']."")->row_array();
              ?>
                    <div class="order_item">
                      <div class="order_top">
                        <div class="row">
                          <div class="col-lg-6 col-12">
                            <a class="orderID" rel="noopener noreferrer" href="<?php echo site_url(); ?>members/order_details/<?php echo $val['order_id']; ?>"><?php echo $val['invoice_number']; ?></a>
                          </div>
                          <div class="col-lg-6 col-12 pull-right text-right text-xs-center">
                             <p class="order-sucess"><i class="fa fa-check-circle" aria-hidden="true"></i> Ordered On <?php echo date('d M Y H:i A', strtotime($val['order_received_date'])); ?></p>
                          </div>
                        </div>
                      </div>
                      <div class="order_body p-xs-2">
                        <?php if (is_array($orderDets) && !empty($orderDets)) {
                          foreach ($orderDets as $o => $pageVal) {
                            $furl = get_db_field_value("wps_products", "friendly_url", "WHERE products_id = '" . $pageVal['products_id'] . "'");
                            $img = get_db_field_value("wps_products_media", "media", "WHERE products_id = '" . $pageVal['products_id'] . "'");
                            $productName = get_db_field_value("wps_products", "product_name", "WHERE products_id = '" . $pageVal['products_id'] . "'");
                        ?>
                            <div class="row">
                              <div class="col-lg-6 col-md-6 col-sm-5">
                                <div class="row">
                                  <div class="col-lg-4 col-md-4 col-sm-5 col-4">
                                    <a href="<?=$furl;?>" title="<?php echo $productName; ?>" target="_blank">
                                      <img src="<?php echo get_image('products', $img, '99', '86', 'R'); ?>" alt="<?php echo $productName; ?>" title="<?php echo $productName; ?>">
                                    </a>
                                  </div>
                                  <div class="col-lg-8 col-md-8 col-sm-7 col-8">
                                    <div class="order_info">
                                      <a href="<?=$furl;?>" title="<?php echo $productName; ?>" target="_blank"><?php echo substr($productName,0,60); ?>...</a>
                                    </div>
                                  </div>

                                </div>
                              </div>
                              <div class="col-lg-6 col-md-6 col-sm-6 col-12">
                                <div class="row">
                                  <div class="col-lg-4 col-md-4 col-sm-3">
                                    <div class="order_price text-center text-sucess"><?php echo display_price($pageVal['product_price'] * $pageVal['quantity']); ?></div>
                                  </div>
                                    <!--  <div class="col-lg-5 col-md-5 col-sm-5">
                                           <div class="delivered">
                                           <p>Delivered on Mon, May 20' 2020</p>
                                            <small>Your item has been delivered</small>
                                         </div>
                                       </div>-->



                                  <div class="col-lg-8 col-md-8 col-sm-9 col-12">
                                    <div class="deliver_process mt-0">
                                      <span class="delHead"><b>Your Order Status:</b></span>
                                      <!-- <span class="success-detail"><i class="fa fa-circle" aria-hidden="true"></i> <b>Order Placed</b></span> -->
                                          <?php if($orderDetail['order_status']=='0'){ ?>                 
<span class="success-detail"><i class="fa fa-circle" aria-hidden="true"></i> Order Placed on date <?php echo getDateFormat($orderDetail['order_received_date'], 6);?></span>
                                        <?php } ?>
                                               <?php if($orderDetail['order_status']=='8'){ ?>                 
<span class="success-detail"><i class="fa fa-circle" aria-hidden="true"></i> Order Delivered on date <?php echo getDateFormat($orderDetail['order_delivery_date'], 6);?></span>
<!--<a href="<?=SITE_URL();?>return-and-refund-policy" title="Return And Refund Policy" class="review_product external cancel-order">Return And Refund Policy</a>-->
                                        <?php } ?>
                        <?php if($orderDetail['order_status']=='1'){ ?>                 
<span class="success-detail"><i class="fa fa-circle" aria-hidden="true"></i> Confirmed Order on date <?php echo getDateFormat($orderDetail['order_confirmed_date'], 6);?></span>
                                        <?php } ?>
                                         <?php if($orderDetail['order_status']=='2'){ ?>                 
<span class="success-detail"><i class="fa fa-circle" aria-hidden="true"></i> Dispatched Order on date <?php echo getDateFormat($orderDetail['order_dispatched_date'], 6);?></span>
                                        <?php } ?>
                                        <?php if($orderDetail['order_status']=='5'){ ?>                 
<span class="cancel-detail"><i class="fa fa-circle" aria-hidden="true"></i> Cancelled on date <?php echo getDateFormat($orderDetail['order_cancelled_date'], 6);?></span>
                                        <?php } ?>
                                        <?php if($orderDetail['order_status']=='6'){ ?> 
                                      <span class="cancel-detail"><i class="fa fa-circle" aria-hidden="true"></i> Return Request on dated <?php echo getDateFormat($orderDetail['order_returned_date'], 6);?></span>
                                        <?php } ?>
<!--<?php echo trace($pageVal['products_id']);?>-->
                                        
                                <div class="deliver_righ_info">
                                        <div class="form-cancel-order">
                                    <?php if($orderDetail['order_status']!='5' && $orderDetail['order_status']!='2'  && $orderDetail['order_status']!='8' && $orderDetail['order_status']!='6'){ ?> 
                                         <a class="review_product external" href="javascript:void(0);" title="Cancer Request" id="cancel_order"  data-orderid="<?=$orderDetail['order_id'];?>" data-customers_id="<?=$orderDetail['customers_id'];?>" ><i class="fa fa-close cancelcon"></i> Cancel Order</a>
                                    <?php } ?>
                                  
                                    </div>
<?php 
$forReturn = getDateFormat($val['order_delivery_date'], 12) + 7;
$currentDate = $this->config->item('config.date.time'); 
$currentDate = getDateFormat($currentDate, 12); 
                              if($currentDate <= $forReturn){
?>
                              
                                   <?php if($orderDetail['order_status']!='6' && $orderDetail['order_status']=='8' ){ ?> 
                                        <a class="review_product external" href="javascript:void(0);" title="Return Request" id="return_order"  value="<?php echo $orderDetail['order_id']; ?>" ><i class="fa fa-repeat"></i> Return Order</a>
     <script type="text/javascript">
 function confirmReturn() {
     var reasontext = $('.reason_for_return').val();
     var orderid  = $('#oid').val();
         $.ajax({
          url: "<?php echo site_url('members/return_order')?>",
          data:$('#returnForm').serialize(),
         type : "POST",
         success : function(data){
             if(data == 'success')
                 {
                    $('#return_status').html('<div class="alert alert-success">Your Request for Return has been Taken. One of our Team will contact you soon</div>');
                    $('#returnForm').trigger('reset');

                    setTimeout(function() {
                      location.reload();
}, 5000);

                    
                  
                 }
             else{
                 $('#return_status').html(data);
             }
         }
        
     })
 }
  
</script>   
 <?php } }?>
                                    
                                        <a href="<?=SITE_URL();?>need-help" title="Need Help" class="local"> Need Help<i class="fa fa-question"></i></a>

                                      </div>
                                    </div>
                                  </div>


                                </div>
                              </div>
                                <?php if($orderDetail['order_status']=='8') { ?>
                              <div class="col-lg-12 col-md-4 col-sm-12">
                        <a class="review_product review-btn bg-warning" href="<?php echo site_url(); ?>members/post_review/<?php echo $pageVal['products_id']; ?>" title="Rate & Review Product"><i class="fa fa-star"></i> Rate & Review</a>
                      </div>
                                <?php } ?>
                            </div>
                        <?php }
                        } ?>
                      </div>
                      <div class="order_foot">
                        <div class="row">
                          <div class="col-lg-6 col-md-6 col-sm-6 col-6">
                              <a class="need_btn" href="<?php echo site_url(); ?>members/order_details/<?php echo $val['order_id']; ?>" title="Track"><i class="fa fa-angle-double-right"></i> View More</a>
                            
                          </div>
                          <div class="col-lg-6 col-md-6 col-sm-6 col-6">
                            <p class="text-right">Order Total <strong><?php echo display_price($val['total_amount']); ?></strong>
                            </p>
                          </div>
                        </div>
                      </div>
                    </div>
                  <?php } 
                  ?>

                </div>
              </div>
            
            <?php } else { ?>
              <div class="text-center mt10 thank-you">
                <h4 class="mb-70 thankyou-text">Sorry, No Orders Found!</h4>
                <a href="<?= site_url('products'); ?>" class="checkout_btn" title="Continue Shopping">Continue Shopping</a>
              </div>
            <?php } ?>
          </div>
        </div>
      </div>

    </div>
  </div>
</section>
 <!-- Modal Cancellation Process -->
<div class="modal" id="myModal_cancel">
  <div class="modal-dialog">
    <div class="modal-content">

      <!-- Modal Header -->
      <div class="modal-header">
        <h4 class="modal-title">Cancellation Process</h4>
        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
      </div>

      <!-- Modal body -->
      <form id="cancelForm" method="post" action="<?php echo site_url('members/cancel_order'); ?>">
        <div class="modal-body">
            <input type="hidden" class="order_id" value="" name="order_id">
            <input type="hidden" class="customer_id" value="" name="customers_id">
            <select name="reason_for_cancelOrder" required class="form-control">
                <option>Select Reason</option>
                <option value="Order by Mistakenly">Order by mistakenly</option>
                <option value="Change my mood">Change my mood</option>
                <option value="Out of town">Out of town</option>
                <option value="No more need">No more need</option>
                <option value="Over price">Over price</option>
                <option value="Long shipping time">Long shipping time</option>
                <option value="Bought from another website">Bought from another website</option>
                <option value="Others">Others</option>
            </select>
          <br />

        </div>

        <!-- Modal footer -->
        <div class="modal-footer">
          <button type="submit" class="btn btn-primary" name="cancel_req">Cancel Order</button>
        </div>
      </form>

    </div>
  </div>
</div>
 <!-- Modal Reason for Return -->
<div class="modal" id="myModalreturn">
  <div class="modal-dialog">
    <div class="modal-content">

      <!-- Modal Header -->
      <div class="modal-header">
        <h4 class="modal-title">Reason for Return</h4>
        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
      </div>

      <!-- Modal body -->
      <form id="returnForm">
        <div id="return_status"></div>
        <div class="modal-body">
            <input type="hidden" class="order_id" value="" id="oid" name="order_id">
          <input type="text" name="reason_for_return" class="form-control reason_for_return" placeholder="Please Enter the reason for Return" required>
          <br />

        </div>

        <!-- Modal footer -->
        <div class="modal-footer">
          <button type="button" class="btn btn-primary" id="return_req" onclick="return confirmReturn();">Submit</button>
        </div>
      </form>

    </div>
  </div>
</div>
<?php $this->load->view("bottom"); ?>

<script type="text/javascript">
    $(function() {
   <?php if ($this->session->flashdata('reviewstatus') != '') { ?>
       swal({
            title:"<?php echo $this->session->flashdata('reviewstatus'); ?>",
            confirmButtonColor: '#dc3545',
           });
   <?php } ?>
   });
</script>

<script type="text/javascript">
 $(document).on("click", "#return_order", function(e) {

        e.preventDefault();
     var orderid = $(this).attr("value");
     if(orderid != "")
         {
            $('#myModalreturn').modal('show');
            $(".order_id").val(orderid);
            
         }else{
             
         }
 });
  
</script>

<script type="text/javascript">
 $(document).on("click", "#cancel_order", function(e) {
        e.preventDefault();
     var orderid = $(this).data('orderid');
     var customers_id = $(this).data('customers_id');
     console.log(orderid);
     if(orderid != "")
         {
            $('#myModal_cancel').modal('show');
            $(".order_id").val(orderid);
            $(".customers_id").val(customers_id);
         }else{
             
         }
 });
  
</script>

