<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-confirm/3.3.2/jquery-confirm.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-confirm/3.3.2/jquery-confirm.min.js"></script>
<!-- <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-confirm/3.3.0/jquery-confirm.min.css">

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-confirm/3.3.0/jquery-confirm.min.js"></script> -->
	<div class="offcanvas-overlay"></div>

    <!-- Page Title/Header Start -->
    <div class="page-title-section section" data-bg-image="assets/images/bg/page-title-1.webp">
        <div class="container">
            <div class="row">
                <div class="col">

                    <div class="page-title">
                        <h1 class="title">Order History</h1>
                        <ul class="breadcrumb">
                            <li class="breadcrumb-item"><a href="<?php echo base_url(); ?>">Home</a></li>
                            <li class="breadcrumb-item active">My order</li>
                        </ul>
                    </div>

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


	<!-- top Products -->
	<div class="section section-padding">
		<div class="container">
			<!-- product right -->
			<div class="agileinfo-ads-display col-md-12">
				<div class="wrapper">
					<!-- first section (nuts) -->
					<div class="product-sec1 cartDetailsD">
						
						<?php
				$number_of_order=0; 
				$fetch_shipping = $this->db->select('*')->where('c_id', $this->session->userdata('customer'))->order_by('id','desc')->get('shipping')->result_array();
				foreach($fetch_shipping as $row_shipping){
				 
				$fetch_order = $this->db->select('*')->where('shipping_id', $row_shipping['id'])->where('booking_status', '1')->get('order_history')->result_array();
				foreach($fetch_order as $row_order){

					$number_of_order++;

				

				$all_prd_id=explode(",", $row_order['prd_id']);

				$row_product = $this->db->select('*')->where('prd_id', $row_order['prd_id'])->get('product')->result_array();
				$delevery_boy_det = $this->db->select('*')->where('tech_id', $row_order['seller_id'])->get('technician_info')->result_array();
				
				//$id=base64_encode($row_product['prd_id']);

				if ($row_order) {

				?>

							<div class="panel panel-default">

								<div class="panel-heading">
									
									<button type="button" name="order" class="btn btn_grey "><?php echo $row_order['invoice_no']; ?></button>
									
									<?php if (!empty($delevery_boy_det)) { ?>
									<button class="pull-right btn btn-success" id="deleveyBdet<?php echo $row_order['order_id']; ?>">Delevery Boy: <?php echo $delevery_boy_det['0']['name']; ?></button> 

									<script type="text/javascript">
										$("#deleveyBdet<?php echo $row_order['order_id']; ?>").click(function(){ 
												$.confirm({
												    title: "<?php echo $delevery_boy_det['0']['name']; ?>",
												    content: "<?php echo $delevery_boy_det['0']['mobile']; ?>",
												    type: 'Green',
												    typeAnimated: true,
												    buttons: {
												        OK: function () {
												        }
												    }
												});
											});									
									</script>
								<?php } ?>
									
								</div>

								<div class="panel-body">

									<div class="table-responsive">

									<table class="cart-wishlist-table table mb0">

										<thead>

										<tr>

											<th>Product Image</th>

											<th>Product Title</th>

											<th>Payment Method</th>

											<th>Status</th>

											<th>Price</th>
											<th>Quantity</th>

										</tr>

									</thead>

										<tbody>

											<?php  

														$prd_img=$this->db->select('*')->where('prd_id', $row_order['prd_id'])->get('product')->result_array();

														$mainid=base64_encode($prd_img[0]['prd_id']);


													?>

											<tr>

												<td>

													<a href="<?php echo base_url().'Product/product_details/'.md5($prd_img[0]['prd_id']).'/?mode='.md5($prd_img[0]['prd_id']); ?>"><img src="<?php echo base_url();?>uploads/product/thumbnail/<?php echo $prd_img[0]['primary_image']; ?>" class="img-responsive" width="100px"></a><br>

												</td>

												<td class="name"><a href="<?php echo base_url().'Product/product_details/'.md5($prd_img[0]['prd_id']); ?>"><?php echo $prd_img[0]['p_title']; ?></a></td>

												<td>

													<?php echo $row_shipping['mode']; ?>

												</td>

												<td>

													<?php echo $row_order['status']; ?>

												</td>

												<td class="price"><span>₹<?php echo $prd_img[0]['p_sell_price']; ?></span></td>
												<td><?php echo $row_order['pqun']; ?></td>

												<!-- <td style="text-align:center;">Delivery on Mon, 4 Dec 2017<br>

												<span>Your item has been delivered</span>

												</td> -->

												<!-- <td style="text-align:center;">

												<a href="#" class="btn btn-default"><i class="fa fa-undo" aria-hidden="true"></i> Return</a>

												</td> -->

											</tr>

										</tbody>

									</table>

									</div>

								</div>

								<div class="panel-footer">

									<span class="pull-left">Order on <?php echo $row_order['date']; ?></span>

									<span class="pull-right">Order Total : ₹<?php echo $row_order['total']; ?> 
									<?php $return_date= date('Y-m-d',strtotime(' + 2 day', strtotime($row_order['delevery_date']))); ?>
								</span>

									<div class="clearfix"></div>

								</div>


									<span class="pull-right mt10"><?php

													if($row_order['status']=='Pending'){

													?>

													<button type="button" class="btn btn-primary" id="sendMail<?php echo $row_order['order_id']; ?>"> Cancel Order</button>

													<?php  

													}

													else if($row_order['status']!='Canceled' && $row_order['status']!='Return' && $row_order['status']!='Admin Cancel Order'){
														if ($return_date>date('Y-m-d')) {
														
													?>

													<button type="button" class="btn btn-danger" onclick="return_product('<?php echo $row_order['order_id']; ?>','<?php echo $row_shipping['email']; ?>','<?php echo $row_order['status']; ?>')"> Return</button>

													<?php  
													}
													}
													else if($row_order['status']=='Admin Cancel Order'){

													?>

													<button type="button" class="btn btn-danger" > Order Canceled</button>

													<?php  

													}
													else if($row_order['status']=='Canceled'){

													?>

													<button type="button" class="btn btn-danger" > Cancelled</button>

													<?php  

													}
													else if($row_order['status']=='Return'){

													?>

													<button type="button" class="btn btn-danger" > Return Requested</button>

													<?php  

													}

													else{
														$tracking_details=$this->db->where('order_id',$row_order['order_id'])->get('trackings')->result_array();
														if ($tracking_details) {
														
													?>

													<!-- <a href="<?php echo base_url().'Product/product_details/'.md5($prd_img[0]['prd_id']); ?>" class="btn btn-primary"> View Product</a> -->
													<a href="<?php echo $tracking_details[0]['link']; ?>" target="_blank" class="btn btn-success"><i class="fa fa-undo" aria-hidden="true"></i> Track Shipment(<?php echo $tracking_details[0]['courier_tracking_number']; ?>)</a>

													<?php  
													}

													}

													?></span>
									<div class="clearfix"></div>

							</div>

						

						

				

<script type="text/javascript">

	$(document).ready(function(){

$("#sendMail<?php echo $row_order['order_id']; ?>").click(function (){

	$.confirm({

    title: 'Order Cancel',

    content: '' +

    '<form action="" class="formName">' +

    '<div class="form-group">' +

    '<label>Reason</label>' +

    '<textarea class="message form-control" placeholder="Cancel Reasons" required></textarea>' +

    '</div>' +

    '</form>',

    buttons: {

        formSubmit: {

            text: 'Submit',

            btnClass: 'btn-blue',

            action: function () {

                var message = this.$content.find('.message').val();

                var order_id ="<?php echo $row_order['order_id']; ?>";

                var email ="<?php echo $row_shipping['email']; ?>";

                var status ="<?php echo $row_order['status']; ?>";

                if(!message){

                	$.alert('Enter Your Reasons');

                    return false;



                }

                	$.ajax({

					type: "POST",

					url: "<?php echo base_url();?>order/order_cancel/",

					data: {email : email ,status : status ,order_id : order_id ,message : message }, 

					success: function(html) {

						$.alert("Your order has been successfully cancelled");

						setInterval(function(){

							location.reload();

						}, 3000);

						}

					});

            }

        },

        cancel: function () {

        },

    },

    onContentReady: function () {

        var jc = this;('form').on('submit', function (e) {

            // if the user submits the form by pressing enter in the field.

            e.preventDefault();

            jc.$$formSubmit.trigger('click'); // reference the button and click it

        });

    }

});

	});

});

</script>

				<?php } } } ?>

				<?php  if($number_of_order<1) { ?>

				<div class="col-md-12 col-sm-12 col-xs-12" align="center">

					<div><img src='<?php echo base_url();?>assets/images/no_order.avif' class='img-responsive' ></div>

				</div>

				<?php } ?>

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


<script type="text/javascript">
	function return_product(order_id,order_email,order_status)
	{


	$.confirm({

    title: 'Product Return',

    content: '' +

    '<form action="" class="formName">' +

    '<div class="form-group">' +

    '<label>Reason</label>' +

    '<textarea class="message form-control" placeholder="Return Reasons" required></textarea>' +

    '</div>' +

    '</form>',

    buttons: {

        formSubmit: {

            text: 'Submit',

            btnClass: 'btn-blue',

            action: function () {

                var message = this.$content.find('.message').val();

                //var order_id =order_id;

                var email =order_email;

                var status =order_status;

                if(!message){

                	$.alert('Enter Your Reasons');

                    return false;



                }

                	$.ajax({

					type: "POST",

					url: "<?php echo base_url();?>order/product_return/",

					data: {email : email ,status : status ,order_id : order_id ,message : message },  

					success: function(html) {

						$.alert("Your return request submited successfully");

						setInterval(function(){

							//location.reload();

						}, 3000);

						}

					});

            }

        },

        cancel: function () {

        },

    },

    onContentReady: function () {

        var jc = this;('form').on('submit', function (e) {

            // if the user submits the form by pressing enter in the field.

            e.preventDefault();

            jc.$$formSubmit.trigger('click'); // reference the button and click it

        });

    }

});

	


	}
</script>