<!DOCTYPE html>
<html lang="en">

<?php 

include "head.php";
require_once('pdf_demo/fpdf.php');
include "dbconfig.php";
include 'phpmailer/PHPMailerAutoload.php';
?>

   <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

<!-- Select 2 Ref Files

-->
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>

<?php
$today=date("Y_m-d");
$yesterday=date('Y-m-d',strtotime("-48 hours"));
date_default_timezone_set ("Europe/London"); 
$today=date("Y-m-d");
$sql='';
$sql="SELECT bookings.pickup_date,bookings.id AS booking_id,bookings.comments, bookings.parking,bookings.driver_id, bookings.drop_address,bookings.drop_post_code,bookings.pickup_address,bookings.pickup_post_code, bookings.waiting_charges,bookings.fare,bookings.total_fare,bookings.waiting_time, bookings.hours,bookings.mins,bookings.other,bookings.job_status,bookings.passengers,job_status.name,
customers.id AS customer_id, customers.name AS customer_name,customers.address1,customers.post_code,customers.city, customers.phone as customer_phone, 
employees.first_name AS driver,vehicles.color as color,vehicles.reg as reg, 
company_contact.name as company_contact,company_contact.email,company_contact.mobile as passenger_phone,
vehicle_make.make as make
FROM bookings 
LEFT JOIN customers ON bookings.customer_id = customers.id 
LEFT JOIN payment_type ON bookings.payment_type = payment_type.id 
LEFT JOIN employees ON bookings.driver_id = employees.id 
LEFT JOIN company_contact ON bookings.passenger_id = company_contact.id 
LEFT JOIN vehicle_make ON bookings.vehicle_id = vehicle_make.id 
LEFT JOIN vehicles on vehicles.driver_id=employees.id
LEFT JOIN job_status ON bookings.job_status = job_status.id 
";


if(isset($_POST)){
		if (!empty($_POST['from']) && !empty($_POST['to']) && !empty($_POST['company_name']))
			{
			  $from=$con->real_escape_string($_POST['from']);
			  $from = str_replace("/","-",$from);
			  $from = date("Y-m-d", strtotime($from));
			
			  $to=$con->real_escape_string($_POST['to']);
			  $to = str_replace("/","-",$to);
			  $to = date("Y-m-d", strtotime($to)); 
			  $company_name=$con->real_escape_string($_POST['company_name']);
			$sql.=" WHERE customers.id='$company_name' AND 
			bookings.pickup_date BETWEEN '$from' AND '$to' GROUP BY  bookings.id ORDER BY bookings.pickup_date ASC, bookings.hours ASC, bookings.mins ASC"; 
			}
			
		else{$sql.=" WHERE 
		bookings.pickup_date > '$yesterday' GROUP BY  bookings.id ORDER BY bookings.pickup_date ASC, bookings.hours ASC, bookings.mins ASC";
			
		}	
}
//echo $sql;

$res=mysqli_query($con,$sql);
if(!$res){echo 'Error is '. mysqli_error($con);}
$rowcount=mysqli_num_rows($res);
//
 
$count_sql="SELECT * from bookings";
$count_res=mysqli_query($con,$count_sql);
$total_jobs=mysqli_num_rows($count_res);


?>  

<style>
tr:hover td { background:#EBE312; cursor:pointer;}
</style> 
<body>

      

<div class="container-fluid">

    <form action="" method="post" class="continer-fluid">
    <h2>Filter for Jobs</h2>
   
     <div class="col-md-12 pull-right">

        <center>
        <div class="col-md-2">
         <div class="form-group">
                      
 <input type="text" name="from" class="from form-control" placeholder="Date From" id="from">
             
         </div>
        </div>
       <div class="col-md-2">
         <div class="form-group">
                      
 <input type="text"  name="to" class="to form-control" placeholder="Date To" id="to">
             
         </div>
        </div>
 
      <div class="col-md-3">
          <div class="form-group">
          <select name="company_name" id="company_name" class="form-control">
			       	   	 <option style="padding:10px; background-color:#E7D91E; color:#F9F0F1; cursor:pointer;" value="">Select from List </option>
                               <?php
							   
							        $customers_sql="SELECT * from customers order by name ASC";
									$t_result=mysqli_query($con,$customers_sql);
									while($customer=mysqli_fetch_assoc($t_result))
									{
									
									?>
	<option style="padding:20px; background-color:#2977C9; color:#F9F0F1; cursor:pointer;" value="<?php echo $customer['id']; ?>">
	   <?php echo $customer['name']; ?>
    </option>
						<?php
						
						}
						?>
			       	   </select>
                       <!--
            <input type="text" name="company_name" id="company_name" class="name form-control" placeholder="Company Name">
            -->
          </div>  
       </div>
          
         
           <div class="col-md-1">
         
          <input type="submit" name="filter_search" id="filter_search" class="btn btn-primary btn-sm" value="Search">
       
      </div>
      <div class="col-md-1">
            <a id="pending" class="btn btn-sm btn-danger">Pending </a>
           </div> 
            <div class="col-md-1">
            <a id="confirmed" class="btn btn-sm btn-success">Confirmed </a>
          </div> 
          
        
        
</center>
</div>
      </form>
     <hr>
    
        <?php include "ajax/list_bookings_data.php";?>
    
      
      </div>
    <script type="text/javascript" src="js_files/list_bookings.js"></script>
     <script type="text/jscript">
$(document).on('click', '.edit', function(e) {
	e.preventDefault();
    var $self = $(this);
    var $input = $self.closest('tr').find('.input');
    $input.attr('contenteditable', 'true').addClass('editable form-control');
    $self.closest('td').html('<span class="button btn btn-success btn-round">Save</span>');
});

$(document).on('click', '.button', function(e) {
    e.preventDefault();
    var booking_id = $('#booking_id').val();
    var $self = $(this);
    var field = $self.closest('tr').data('field');
    var value = $self.closest('tr').find('.input.editable').text();
	
    var $input = $self.closest('tr').find('.input');
    var dataString = 'booking_id=' + booking_id + '&field=' + field +'&value=' + value;
	//alert(dataString);
    if (value.length > 0) {
        var data = {
            booking_id: booking_id,
            field: field,
            value: value
        }
	//alert(data);
        //console.log(data);
var url11 = 'ajax/update_comment.php?'+new Date().getTime();
  	$.ajax({
  		type: "POST",
      url: url11,
      data: dataString,
      success: function(data){
		  console.log(data);
     	if (data == 'success') {
  $input.attr('contenteditable', 'false').removeClass('editable form-control');
  $self.closest('td').html('<span class="edit glyphicon glyphicon-pencil"></span>');
  			}
      }
    });
    };
});

$('#pending').click(function(){
	$('.confirmed').hide();
	$('.pending').show();
});
$('#confirmed').click(function(){
	$('.pending').hide();
	$('.confirmed').show();
});
$('#company_name').select2({ width: '100%' });
</script>  

   </body>
</html>
