<?php  
include "lib/header.php";
?>
<div class="headerImg">
	<img src="img/headerImg.jpg" class="img-responsive">
	<div class="headerCap" align="center">
		<h2 class="headingDotherPg">Order History<strong></strong></h2>
	</div>
</div>
<a href="#" class="btn giftBtn"><img src="img/gift.png"> Gift</a>
<div class="cartDetails" >
	<div class="container">
		<div class="row">
			<div class="col-md-12 col-sm-12 col-xs-12">  
				<ul class="breadcrumb">
				  <li><a href="index.php">Home</a></li>
				  <li>Order History</li>
				</ul>
				<?php
				$number_of_order=0;  
				$fetch_shipping=mysqli_query($con,"select * from `shipping` where `c_id`='".$_SESSION['customer']."'  order by `id` desc");
				while ($row_shipping=mysqli_fetch_array($fetch_shipping)) {
				$fetch_order=mysqli_query($con,"select * from `order_history` where `shipping_id`='".$row_shipping['id']."'");
				$row_order=mysqli_fetch_array($fetch_order);
				$all_prd_id=explode(",", $row_order['prd_id']);
				$number_of_order=mysqli_num_rows($fetch_order);
				$fetch_product=mysqli_query($con,"select * from `product` where `prd_id`='".$row_order['prd_id']."'");
				$row_product=mysqli_fetch_array($fetch_product);
				$id=base64_encode($row_product['prd_id']);
				if ($row_order) {
				?>
				<div class="repairD">
					<ul class="nav nav-pills">
						<li class="active"><a data-toggle="tab" href="#home">Order History</a></li>
						<li class=""><a data-toggle="tab" href="#menu1">Service History</a></li>
						<li class=""><a data-toggle="tab" href="#menu2">Gift Booking</a></li>
					</ul>
					<div class="tab-content">
						<div id="home" class="tab-pane fade in active">
						<div class="checkOutD">
							<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>
								</div>
								<div class="panel-body">
									<div class="table-responsive">
									<table class="table">
										<thead>
										<tr>
											<th>Product Image</th>
											<th>Product Title</th>
											<th>Payment Method</th>
											<th>Status</th>
											<th>Price</th>
										</tr>
									</thead>
										<tbody>
											<?php  
													foreach ($all_prd_id as $key_id) {
														$query_img=mysqli_query($con,"select * from `product` where `prd_id`='".$key_id."'");
														$prd_img=mysqli_fetch_array($query_img);
														$mainid=base64_encode($prd_img['prd_id']);
														$imag= explode(",", $prd_img['p_img']);
													?>
											<tr>
												<td>
													<a href="product_details.php?mode=<?php echo $mainid; ?>"><img src="upload/product/<?php echo $imag[0]; ?>" class="img-responsive" width="50px"></a><br>
												</td>
												<td><p><a href="product_details.php?mode=<?php echo $mainid; ?>"><?php echo $prd_img['p_title']; ?></a></p></td>
												<td>
													<?php echo $row_shipping['mode']; ?>
												</td>
												<td>
													<?php echo $row_order['status']; ?>
												</td>
												<td><i class="fa fa-inr" aria-hidden="true"></i> <?php echo $prd_img['p_sell_price']; ?></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>
											<?php } ?>
										</tbody>
									</table>
									</div>
								</div>
								<div class="panel-footer">
									<span class="pull-left">Order on <?php echo $row_order['date']; ?></span>
									<span style="padding-left: 300px;">Order Total : <i class="fa fa-inr" aria-hidden="true"></i> <?php echo $row_order['total']; ?></span>
									<span class="pull-right"><?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{
													?>
													<a href="product_details.php?mode=<?php echo $id; ?>" class="btn btn_green"> View Product</a>
													<?php  
													}
													?></span>
									<div class="clearfix"></div>
								</div>
							</div>
						</div>
					</div>
					
						<div id="menu1" class="tab-pane fade">
							<?php  
							$fetch_sevice=mysqli_query($con,"SELECT * FROM `service_booking` where `c_id`='".$_SESSION['customer']."' ORDER BY `book_id` DESC");
							while ($row_service=mysqli_fetch_array($fetch_sevice)) {
								$fetch_brand=mysqli_query($con,"select * from `brand` where `brand_id`='".$row_service['brand_id']."'");
								$row_brand=mysqli_fetch_array($fetch_brand);
								$fetch_service_type=mysqli_query($con,"select * from `service_type` where `serv_id`='".$row_service['serv_id']."'");
								$row_service_type=mysqli_fetch_array($fetch_service_type);
								$fetch_model=mysqli_query($con,"select * from `model_number` where `model_id`='".$row_service['model_id']."'");
								$row_model=mysqli_fetch_array($fetch_model);
								$problem_section_array=explode(',', $row_service['prob_sec_id']);
								$problem_sec_details='';
								foreach ($problem_section_array as $problem_section_key => $problem_section_value) {
								$fetch_problem_section=mysqli_query($con,"select * from `problem_section` where `prob_sec_id`='".$problem_section_value."'");
								$row_problem_section=mysqli_fetch_array($fetch_problem_section);
								if ($problem_key==0) {
									$problem_sec_details=$row_problem_section['problem_section'];
								}
								else
								{
									$problem_sec_details=$problem_sec_details.','.$row_problem_section['problem_section'];
								}
								}

								$problem_array=explode(',', $row_service['problem_id']);
								$problem_details='';
								$total_service_price=0;
								foreach ($problem_array as $problem_key => $problem_value) {
								$fetch_problem=mysqli_query($con,"select * from `problems` where `problem_id`='".$problem_value."'");
								$row_problem=mysqli_fetch_array($fetch_problem);
								$total_service_price=$total_service_price+$row_problem['e_cost'];
								if ($problem_key==0) {
									$problem_details=$row_problem['problem'];
								}
								else
								{
									$problem_details=$problem_details.','.$row_problem['problem'];
								}
								}
							?>
							<div class="checkOutD">
							<div class="panel panel-default">
								<div class="panel-body">
									<div class="table-responsive">
									<table class="table">
										<thead>
										<tr>
											<th>Brand</th>
											<th>Model No.</th>
											<th>Problem Section</th>
											<th>Problem</th>
											<th>Price</th>
										</tr>
									</thead>
										<tbody>
											<tr>
												<td><?php echo $row_brand['brand_name']; ?></td>
												<td><?php echo $row_model['model_number']; ?></td>
												<td><?php echo $problem_sec_details; ?></td>
												<td><?php echo $problem_details; ?></td>
												<td><i class="fa fa-inr" aria-hidden="true"></i> <?php echo $total_service_price; ?></td>
											</tr>
										</tbody>
									</table>
									</div>
								</div>
								<div class="panel-footer">
									<span class="pull-left">Booking on <?php echo $row_service['date']; ?></span>
									<span class="pull-right">
									Order Total : <i class="fa fa-inr" aria-hidden="true"></i> <?php echo $total_service_price; ?>
									</span>
									<div class="clearfix"></div>
								</div>
							</div>
						</div>
					<?php } ?>
						</div>
						<div id="menu2" class="tab-pane fade">
							<?php  
							$fetch_gift=mysqli_query($con,"SELECT * FROM `gift_booking` where `c_id`='".$_SESSION['customer']."' ORDER BY `book_id` DESC");
							while ($row_gift=mysqli_fetch_array($fetch_gift)) {
								$fetch_gifting=mysqli_query($con,"select * from `gifting` where `g_id`='".$row_gift['g_id']."'");
								$row_gifting=mysqli_fetch_array($fetch_gifting);
								$fetch_gift_item=mysqli_query($con,"select * from `gift_item` where `gi_id`='".$row_gift['gi_id']."'");
								$row_gift_item=mysqli_fetch_array($fetch_gift_item);
								$fetch_gift_type=mysqli_query($con,"select * from `gift_type` where `gt_id`='".$row_gift['gt_id']."'");
								$row_gift_type=mysqli_fetch_array($fetch_gift_type);
							?>
							<div class="checkOutD">
							<div class="panel panel-default">
								<div class="panel-body">
									<div class="table-responsive">
									<table class="table">
										<thead>
										<tr>
											<th>Gift Type</th>
											<th>Gift Item</th>
											<th>Size</th>
											<th>To</th>
											<th>Wish</th>
											<th>Message</th>
											<th>Price</th>
										</tr>
									</thead>
										<tbody>
											<tr>
												<td><?php echo $row_gift_type['gift_type_name']; ?></td>
												<td><?php echo $row_gift_item['gift_name']; ?></td>
												<td><?php echo $row_gifting['gift_size']; ?></td>
												<td><?php echo $row_gift['to_name']; ?></td>
												<td><?php echo $row_gift['wish']; ?></td>
												<td><?php echo $row_gift['message']; ?></td>
												<td><i class="fa fa-inr" aria-hidden="true"></i> <?php echo $row_gift['gb_price']; ?></td>
											</tr>
										</tbody>
									</table>
									</div>

								</div>
								<div class="panel-footer">
									<span class="pull-left">Booking on <?php echo $row_gift['date']; ?></span>
									
									<span class="pull-right">
									Order Total : <i class="fa fa-inr" aria-hidden="true"></i> <?php echo $row_gift['gb_price']; ?>
									</span>
									<div class="clearfix"></div>
								</div>

							</div>

						</div>
					<?php } ?>
						</div>
				</div>
			</div>
				<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>
<script type="text/javascript">
	$(document).ready(function(){
$("#sendMail<?php echo $row_order['order_id']; ?>").click(function (){
	$.confirm({
    title: 'Oder 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: "order_cancel.php",
					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='img/no_order.jpg' class='img-responsive' ></div>
				</div>
				<?php } ?>
			</div>
		</div>
	</div>
</div>
<script>
$(".ddd").on("click", function () {
    var $button = $(this);
    var oldValue = $button.closest('.sp-quantity').find("input.quntity-input").val();
    if ($button.text() == "+") {
        var newVal = parseFloat(oldValue) + 1;
    } else {
        if (oldValue > 0) {
            var newVal = parseFloat(oldValue) - 1;
        } else {
            newVal = 0;
        }
    }
    $button.closest('.sp-quantity').find("input.quntity-input").val(newVal);
});
</script>
<?php
include "lib/footer.php";
?>