<title>List Bookings</title>
<?php 
include "head.php";
require_once('pdf_demo/fpdf.php');
include "dbconfig.php";
include 'phpmailer/PHPMailerAutoload.php';
error_reporting(E_ERROR);

?>

   <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
   <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");
date_default_timezone_set ("Europe/London"); 
$yesterday=date('Y-m-d',strtotime("-24 hours"));

$today=date("Y-m-d");
$sql='';
$sql1='';
$sql="SELECT bookings.start_date,bookings.id AS booking_id,bookings.comments,bookings.job_type,bookings.flight_no,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.invoice_status,bookings.passengers,bookings.bags,job_status.name,bookings.comments,
bookings.start_date,bookings.end_date,bookings.days,bookings.rate AS rate,bookings.supp_cost AS supp_cost,bookings.substitute,
customers.id AS customer_id,bookings.client_reference, 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, vehicle_make.make as make,
company_contact.name as company_contact,company_contact.email,company_contact.mobile as passenger_phone

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 
WHERE 1=1
";
$sql1=" AND
		bookings.start_date >'$yesterday' GROUP BY  bookings.id ORDER BY bookings.start_date ASC, bookings.hours ASC, bookings.mins ASC";
			

if(isset($_POST['from']) OR isset($_POST['to']) OR isset($_POST['client_reference1']) OR isset($_POST['company_name1']) OR isset($_POST['passenger_name1']) OR isset($_POST['driver_name1']) && isset($_POST['driver_id1']) OR isset($_POST['booking_id1'])){


//	echo '<pre>';print_r($_POST);echo '</pre>';
	$sql1='';
	
	         
			  
			  $company_name=$con->real_escape_string($_POST['company_name']);
			  $driver_name=$con->real_escape_string($_POST['driver_name']);
			  $booking_id=$con->real_escape_string($_POST['booking_id']);
			  $passenger_name=$con->real_escape_string($_POST['passenger_name']);
			   $client_reference=$con->real_escape_string($_POST['client_reference']);
			 
 			  $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));
  if($from=='1970-01-01' && $to=='1970-01-01' )
			 {
				 $from='';$to='';
				 }
				// echo $from;
		if (!empty($from) && !empty($to) && !empty($company_name))
			{
			
			$sql1=" AND bookings.customer_id=$company_name AND 
			bookings.start_date BETWEEN '$from' AND '$to' GROUP BY  bookings.id ORDER BY bookings.start_date ASC, bookings.hours ASC, bookings.mins ASC"; 
			}elseif (empty($from) && empty($to) && !empty($company_name)){
				$sql1=" AND bookings.customer_id=$company_name GROUP BY  bookings.id ORDER BY bookings.start_date ASC, bookings.hours ASC, bookings.mins ASC"; 
			}
				
		if (!empty($from) && !empty($to) && !empty($driver_name))
			{
			
			$sql1=" AND bookings.driver_id=$driver_name AND 
			bookings.start_date BETWEEN '$from' AND '$to' GROUP BY  bookings.id ORDER BY bookings.start_date ASC, bookings.hours ASC, bookings.mins ASC"; 
			}elseif (empty($from) && empty($to) && !empty($driver_name)){
				$sql1=" AND bookings.driver_id=$driver_name GROUP BY  bookings.id ORDER BY bookings.start_date ASC, bookings.hours ASC, bookings.mins ASC"; 
				
				}
	if (!empty($from) && !empty($to) && empty($company_name) && !empty($driver_name))
			{
			
			$sql1=" AND  bookings.driver_id=$driver_name AND bookings.start_date BETWEEN '$from' AND '$to' GROUP BY  bookings.id ORDER BY bookings.start_date ASC, bookings.hours ASC, bookings.mins ASC"; 
			}
			if (!empty($from) && !empty($to) && empty($company_name) && empty($driver_name))
			{
				$sql1=" AND  bookings.start_date BETWEEN '$from' AND '$to' GROUP BY  bookings.id ORDER BY bookings.start_date ASC, bookings.hours ASC, bookings.mins ASC"; 
			}
			
		// If user just filled the booking id
		if (!empty($booking_id))
			{
			
			$sql1=" AND bookings.id=".$booking_id; 
			}
		if(!empty($client_reference))
		{
		
		$sql1=" AND bookings.id=".$client_reference; 
		
		}
		if(!empty($passenger_name))
		{
		
		$sql1=" AND bookings.passenger_id=".$passenger_name; 
		
		}
if(isset($_POST['btn_pending']))
{
	if (!empty($from) && !empty($to) )
			{
				
	$sql1=" AND bookings.job_status='1' AND 
			bookings.start_date BETWEEN 
			'$from' AND '$to' GROUP BY  bookings.id ORDER BY bookings.start_date ASC, bookings.hours ASC, bookings.mins ASC";
			}
		else{
		$sql1=" AND bookings.job_status='1' GROUP BY  bookings.id ORDER BY bookings.start_date ASC, bookings.hours ASC, bookings.mins ASC";
			}
	}

if(isset($_POST['btn_cancelled']))
{
	if (!empty($from) && !empty($to) )
			{
				
	$sql1=" AND bookings.job_status='3' AND 
			bookings.start_date BETWEEN 
			'$from' AND '$to' GROUP BY  bookings.id ORDER BY bookings.start_date ASC, bookings.hours ASC, bookings.mins ASC";
			}
		else{
		$sql1=" AND bookings.job_status='3' GROUP BY  bookings.id ORDER BY bookings.start_date ASC, bookings.hours ASC, bookings.mins ASC";
			}
	}
	
if(isset($_POST['btn_invoiced']))
{
	if (!empty($from) && !empty($to) )
			{
				
	$sql1=" AND bookings.job_status='4' AND 
			bookings.start_date BETWEEN 
			'$from' AND '$to' GROUP BY  bookings.id ORDER BY bookings.start_date ASC, bookings.hours ASC, bookings.mins ASC";
			}
		else{
		$sql1=" AND bookings.job_status='4' GROUP BY  bookings.id ORDER BY bookings.start_date ASC, bookings.hours ASC, bookings.mins ASC";
			}
	}		
	
// If user clicked Confirmed Button then show only confirmed bookings
if(isset($_POST['btn_confirmed']))
{
	if (!empty($from) && !empty($to))
			{
				
	$sql1=" AND bookings.job_status='2' AND 
			bookings.start_date BETWEEN 
			'$from' AND '$to' GROUP BY  bookings.id ORDER BY bookings.start_date ASC, bookings.hours ASC, bookings.mins ASC";
			}
		else{
		$sql1=" AND bookings.job_status='2' GROUP BY  bookings.id ORDER BY bookings.start_date ASC, bookings.hours ASC, bookings.mins ASC";
			}
	
	}
}
$sql=$sql.$sql1;
//echo $sql;
	
$res=mysqli_query($con,$sql);
if(!$res){echo 'Error is <h2><b>'. mysqli_error($con).'</h2></b>';}
$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>
/* on mouse hover change color
tr:hover td { background:#EBE312; cursor:pointer;}
*/
</style>
<!DOCTYPE html>
<html lang="en"> 
<body>

<div class="container-fluid">

    <form action="" method="post" class="continer-fluid">
    <h2>Filter for Jobs - You Must Select From , To and Company Name for Filter</h2>
   
     <div class="col-md-12">

        <center>
        <div class="col-md-2" style="margin-left:-30px;">
         <div class="form-group">
                      
 <input type="text" name="from" class="from form-control" placeholder="Date From" style="max-width:100px;" autocomplete="off" id="from">
             
         </div>
        </div>
       <div class="col-md-2" style="margin-left:-100px;">
         <div class="form-group">
                      
 <input type="text"  name="to" class="to form-control" placeholder="Date To" style="max-width:100px;" autocomplete="off" id="to">
              </div>
        </div>
 
      <div class="col-md-2" style="margin-left:-70px;">
          <div class="form-group">
          <select name="company_name1" id="company_name1" class="form-control">
			       	   	 <option style="padding:10px; background-color:#E7D91E; color:#F9F0F1; cursor:pointer;" value="">Search By Company </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-2" style="margin-left:-20px;">
          <div class="form-group">
          <select name="client_reference1" id="client_reference1" class="form-control">
			       	   	 <option style="padding:10px; background-color:#E7D91E; color:#F9F0F1; cursor:pointer;" value="">Search By Client Reference </option>
                               <?php
							   
							        $customers_sql="SELECT * from bookings WHERE client_reference!='' order by id DESC";
									$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['client_reference']; ?>
    </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-2" style="margin-left:-20px;">
          <div class="form-group">
          <select name="passenger_name1" id="passenger_name1" class="form-control">
			       	   	 <option style="padding:10px; background-color:#E7D91E; color:#F9F0F1; cursor:pointer;" value="">Search By Passenger</option>
                               <?php
							   
							        $customers_sql="SELECT * from company_contact order by id DESC";
									$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']." ".$customer['mobile']; ?>
    </option>
						<?php
						
						}
						?>
			       	   </select>
                       <!--
            <input type="text" name="company_name" id="company_name" class="name form-control" placeholder="Company Name">
            -->
          </div>  
       </div>
	   
          <div class="form-group col-sm-2" style="margin-left:-20px;">
			       	   <select name="driver_name1" id="driver_name1" class="form-control">
			       	   	 <option style="padding:10px; background-color:#E7D91E; color:#F9F0F1; cursor:pointer;" value="">Search By Driver</option>
                               <?php
							   
							        $customers_sql="select employees.id,employees.phone,employees.first_name,vehicle_make.make,vehicles.color,vehicles.reg
from vehicles
left join employees on vehicles.driver_id=employees.id
left join vehicle_make on vehicles.make=vehicle_make.id WHERE employees.first_name IS NOT NULL ORDER BY first_name";
									$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['first_name']; ?>
    </option>
						<?php
						
						}
						?>
			       	   </select>
			       	   </div>
         
           <div class="col-md-1" style="margin-top:-30px; margin-left:-20px;">
           <label>Job ID</label>
           <input type="text" name="booking_id1" class="form-control" placeholder="8596" value=""/>
           </div>
           <div class="col-md-1" style="margin-left:-30px;">
         
          <input type="submit" name="filter_search" id="filter_search" class="btn btn-primary btn-sm" value="Search">
       
      </div>
	 <br>
				<div class="col-md-12" style="float:left;">	   
      <div class="col-md-1">
	   
            <button id="pendingg" name="btn_pending" class="btn btn-sm btn-danger" value="Pending">Pending</button>
           </div>
		   
            <div class="col-md-1">
            <button id="confirmedd" name="btn_confirmed" class="btn btn-sm btn-success" value="Confirmed">Confirmed</button>
           </div>
		   
		    <div class="col-md-1">
            <button id="cancelledd" name="btn_cancelled" class="btn btn-sm btn-danger" value="Cancelled">Cancelled</button>
           </div> 
		 
            <div class="col-md-1" >
            <button id="invoicedd" name="btn_invoiced" class="btn btn-sm btn-success" value="Invoiced">Invoiced</button>
           </div>
		   
		   
		   
       </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", ".open-AddBookDialog", function () {
	 var job_id = $(this).data('id');	  
    
      var dataString='job_id='+job_id;
	  $.ajax({
				
				url: "modal_details.php",
				type: "POST",
				data:  dataString,
				
				success: function(data)
				{ 
				//alert(data);
			    //   $('#planner_data').html(data);
				 // $('#drivers_modal').modal('show');
			   //$(".modal-body #bookId").val( data );
				 $('#bookId').html(data);
				  $('.myModal').modal('show');
				}
			  });
		
});
function yourFunction(intValue){
  // alert(intValue);
    $.ajax({
				
				url: "allocate_driver.php",
				type: "POST",
				data:  dataString,
				
				success: function(data)
				{ 
				//alert(data);
			       $('#planner_data').html(data);
				  $('#drivers_modal').modal('show');
			  
				
				}
			  });
    $('#myModal').modal('show');
	$(".modal-body #bookId").val(intValue);
}
$(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();
	$('.cancelled').hide();
	$('.invoiced').hide();
	$('.pending').show();
});
$('#confirmed').click(function(){
	$('.pending').hide();
	$('.cancelled').hide();
	$('.invoiced').hide();
	$('.confirmed').show();
});
$('#cancelled').click(function(){
     
	$('.invoiced').hide();
	$('.confirmed').hide();
	$('.pending').hide();
	$('.cancelled').show();
});
$('#invoiced').click(function(){
	$('.pending').hide();
	$('.confirmed').hide();
	$('.cancelled').hide();
	$('.invoiced').show();
});
$('#company_name').select2({ width: '100%' });
$('#driver_id').select2({width: '100%'});
$('#driver_name').select2({width: '100%'});
$('#client_reference').select2({width: '100%'});
$('#passenger_name').select2({ width: '100%' });
$('#invoice_id').select2({width: '100%'});
</script>  

   </body>
</html>
