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

ob_start();
include "head.php";
include "dbconfig.php";
?>

   <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>
  <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 
date_default_timezone_set ("Europe/London"); 
$yesterday=date('Y-m-d',strtotime("-24 hours"));
$sql='';
$sql_sub='';
/*$sql="SELECT DISTINCT bookings.id as booking_id,bookings.substitute,
driver_bookings.waiting_time,bookings.job_status as job_status,driver_bookings.total_fare as total_fare,driver_bookings.waiting_charges,driver_bookings.driver_id,driver_bookings.parking,driver_bookings.transfer,driver_bookings.supp_cost,driver_bookings.other,bookings.pickup_date,bookings.start_date,bookings.end_date,bookings.pickup_address,bookings.supp_cost,bookings.days,bookings.rate,bookings.pickup_post_code,bookings.drop_address,bookings.drop_post_code,bookings.hours,bookings.mins,customers.name as customer_name, company_contact.name as company_contact,employees.first_name as driver
from driver_bookings
INNER JOIN bookings on bookings.id=driver_bookings.booking_id
INNER JOIN customers on bookings.customer_id=customers.id
INNER JOIN company_contact on bookings.passenger_id=company_contact.id
INNER JOIN employees on driver_bookings.driver_id=employees.id
WHERE bookings.start_date >'$yesterday' AND bookings.job_status=2"; */

$sql="SELECT DISTINCT bookings.id as booking_id,bookings.substitute,
driver_bookings.waiting_time,bookings.job_type,bookings.job_status as job_status,driver_bookings.id,bookings.customer_id,bookings.status,bookings.paid_status,driver_bookings.total_fare as total_fare,driver_bookings.waiting_charges,driver_bookings.driver_id,driver_bookings.parking,driver_bookings.transfer,driver_bookings.supp_cost,driver_bookings.other,bookings.pickup_date,driver_bookings.start_date,driver_bookings.end_date,bookings.pickup_address,driver_bookings.days,bookings.rate,bookings.pickup_post_code,bookings.drop_address,bookings.drop_post_code,bookings.hours,bookings.mins,customers.name as customer_name, company_contact.name as company_contact,employees.first_name as driver
from driver_bookings
INNER JOIN bookings on bookings.id=driver_bookings.booking_id
INNER JOIN customers on bookings.customer_id=customers.id
INNER JOIN company_contact on bookings.passenger_id=company_contact.id
INNER JOIN employees on driver_bookings.driver_id=employees.id
WHERE bookings.start_date >'$yesterday' AND bookings.job_status=2 AND bookings.paid_status!=1"; 


//$sql_sub="SELECT * FROM driver_substitute WHERE start_date >'$yesterday'";
//echo $sql;
//exit;
//echo $sql_sub;
//exit;

if(isset($_POST['filter_search'])){
    
	$name='';
	$from="";
	$to="";
/*	$sql="SELECT DISTINCT bookings.id as booking_id,bookings.substitute,
driver_bookings.waiting_time,bookings.job_status as job_status,driver_bookings.total_fare as total_fare,driver_bookings.waiting_charges,driver_bookings.driver_id,driver_bookings.parking,driver_bookings.transfer,driver_bookings.fare,driver_bookings.other,bookings.pickup_date,bookings.start_date,bookings.end_date,bookings.pickup_address,bookings.days,bookings.rate,bookings.supp_cost,bookings.pickup_post_code,bookings.drop_address,bookings.drop_post_code,bookings.hours,bookings.mins,customers.name as customer_name, company_contact.name as company_contact,employees.first_name as driver
from driver_bookings
INNER JOIN bookings on bookings.id=driver_bookings.booking_id
INNER JOIN customers on bookings.customer_id=customers.id
INNER JOIN company_contact on bookings.passenger_id=company_contact.id
INNER JOIN employees on driver_bookings.driver_id=employees.id


";*/
$sql="SELECT DISTINCT bookings.id as booking_id,bookings.substitute,
driver_bookings.waiting_time,bookings.job_type,bookings.job_status as job_status,driver_bookings.id,bookings.customer_id,bookings.status,bookings.paid_status,driver_bookings.total_fare as total_fare,driver_bookings.waiting_charges,driver_bookings.driver_id,driver_bookings.parking,driver_bookings.transfer,driver_bookings.supp_cost,driver_bookings.other,bookings.pickup_date,driver_bookings.start_date,driver_bookings.end_date,bookings.pickup_address,driver_bookings.days,bookings.rate,bookings.pickup_post_code,bookings.drop_address,bookings.drop_post_code,bookings.hours,bookings.mins,customers.name as customer_name, company_contact.name as company_contact,employees.first_name as driver
from driver_bookings
INNER JOIN bookings on bookings.id=driver_bookings.booking_id
INNER JOIN customers on bookings.customer_id=customers.id
INNER JOIN company_contact on bookings.passenger_id=company_contact.id
INNER JOIN employees on driver_bookings.driver_id=employees.id
WHERE 1=1 AND bookings.paid_status!=1"; 
//$sql_sub="SELECT * FROM driver_bookings WHERE 1=1";
	
	if (!empty($_POST['from']) && !empty($_POST['to']))
	{
	  $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));
	$sql.=" AND 
    driver_bookings.start_date BETWEEN '$from' AND '$to'"; 
	//$sql_sub.=" AND start_date BETWEEN '$from' AND '$to'"; 
	}
	
	if (!empty($_POST['driver_id']))
	{
	  $driver_id=$con->real_escape_string($_POST['driver_id']);
	$sql.=" AND driver_bookings.driver_id='$driver_id'  AND bookings.job_status=2";
	
	}
	
$sql.="  

GROUP BY bookings.id ORDER BY bookings.start_date ASC, bookings.hours ASC, bookings.mins ASC";


}
//echo $sql;
if(!mysqli_query($con,$sql)){echo 'error'.mysqli_error($con);}
$res=mysqli_query($con,$sql);
  $grand_total=0;
				$res1=mysqli_query($con,$sql);
                while($job1=mysqli_fetch_assoc($res1))

                  {
				      $gtotal=$job1['days']*$job1['supp_cost']+$job1['parking']+$job1['transfer'];
					  $grand_total+=$gtotal;
				  }

/*if(!mysqli_query($con,$sql_sub)){echo 'error'.mysqli_error($con);}
$res_sub=mysqli_query($con,$sql_sub);
 // $grand_total=0;
				$res2=mysqli_query($con,$sql_sub);
                while($job2=mysqli_fetch_assoc($res2))

                  {
				      $gtotal=$job2['days']*$job2['supp_cost']+$job2['parking']+$job2['transfer'];
					  $grand_total+=$gtotal;
				  }				  
*/
?>
   <body>

      

      <div class="container-fluid">
<div class="col-md-12" style="text-align:center;color:#0000; padding:5px; font-size:18px;">
   <h2> List of Allocated Bookings</h2>
     
</div>

    <form action="" method="post">
    
    <input type="hidden" name="filter_search" value="yes">
  <div class="col-md-12 well" style="text-align:left;">
	<label style="font-size:16px;">Appy Jobs Filter to Search Jobs</label>
        <center>
       <div class="col-md-3" id="jobs_data">
	</div>
          
        <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-1">
         <div class="form-group">
          <input type="submit" name="filter_search" id="filter_search" class="btn btn-warning btn-sm" placeholder="Type N or P">
         </div>
      </div>
      </center>
        </div>
       
    </form>
      
   
<hr>
  
  <div class="row">
      <div class="col-md-12" id="filter_results">
        
        <?php include "ajax/list_driver_jobs_data.php";?>
        
		    </div>
	    </div>
<!-- Delete Modal -->

      
      </div>
 <!-- Java Script Section -->
<script type="text/javascript">
$(document).ready(function(){
    $('#select_all').on('click',function(){
        if(this.checked){
            $('.checkbox').each(function(){
                this.checked = true;
            });
        }else{
             $('.checkbox').each(function(){
                this.checked = false;
            });
        }
    });
    
    $('.checkbox').on('click',function(){
        if($('.checkbox:checked').length == $('.checkbox').length){
            $('#select_all').prop('checked',true);
        }else{
            $('#select_all').prop('checked',false);
        }
    });
});


$('body').delegate('.waiting', 'click',function()  
{
   var tr=$(this).parent().parent();  
   var index=tr.find('.waiting').attr('data-id');
 //  $('#waiting'+index).val(''); 
});
$('body').delegate('.parking', 'click',function()  
{
   var tr=$(this).parent().parent();  
   var index=tr.find('.parking').attr('data-id');
  // $('#parking'+index).val(''); 
    
});
$('body').delegate('.transfer', 'click',function()  
{
   var tr=$(this).parent().parent();  
   var index=tr.find('.transfer').attr('data-id');
 //  $('#transfer'+index).val(''); 
    
});
$('body').delegate('.fare', 'click',function()  
{
   var tr=$(this).parent().parent();  
   var index=tr.find('.fare').attr('data-id');
 //  $('#fare'+index).val(''); 
    
});
$('body').delegate('.other', 'click',function()  
{
   var tr=$(this).parent().parent();  
   var index=tr.find('.other').attr('data-id');
 //  $('#other'+index).val(''); 
});

$('body').delegate('.rate,.days,.parking,.transfer', 'blur',function()  
{  

var tr=$(this).parent().parent();  
var index=tr.find('.rate').attr('data-id');

var rate=$('#rate'+index).val(); 
var parking=$('#parking'+index).val();
var transfer=$('#transfer'+index).val();
var total_fare=$('#total_fare'+index).val();
var days=$('#days'+index).val();

rate = isNaN(parseFloat(rate)) ? 0 : parseFloat(rate);
days = isNaN(parseFloat(days)) ? 0 : parseFloat(days);
parking = isNaN(parseFloat(parking)) ? 0 : parseFloat(parking);
transfer = isNaN(parseFloat(transfer)) ? 0 : parseFloat(transfer);
total_fare = isNaN(parseFloat(total_fare)) ? 0 : parseFloat(total_fare);


rate = parseFloat(rate).toFixed(2);
parking = parseFloat(parking).toFixed(2);
transfer = parseFloat(transfer).toFixed(2);
total_fare = parseFloat(total_fare).toFixed(2);
days = parseFloat(days).toFixed(2);
var sub_total=rate*days;
//alert(sub_total);
//alert(rate);
//alert(days);
//alert(parking);
//sub_total=parseInt(sub_total).toFixed(2);
var total_fare=  +sub_total+ +parking+ +transfer;
total_fare = parseFloat(total_fare).toFixed(2);

tr.find('#total_fare'+index).val(total_fare);

});  


$('body').delegate('.waiting,.transfer,.fare,.other', 'click',function()  
{
 // $(this).val(''); 
   
});

   //script to allow only numbers
    function isNumber(evt) {
        var iKeyCode = (evt.which) ? evt.which : evt.keyCode
        if (iKeyCode != 46 && iKeyCode > 31 && (iKeyCode < 48 || iKeyCode > 57))
            return false;

        return true;
    }    

</script>
<script>
////////////////// Update Button Script /////////////////////////

$(".update_job").find('td[data-id]').on('click', function () {
	
	 var selected_col=$(this).attr('col-id');
	// If update button coloumn is selected
	 if(selected_col==16)
	{
	 var id=$(this).attr('data-id');
	 var waiting_time=$('#waiting_time'+id).val();
	 var waiting=$('#waiting'+id).val();
	 var transfer=$('#transfer'+id).val();
	  var parking=$('#parking'+id).val();
	 var fare=$('#fare'+id).val();
	 var other=$('#other'+id).val();
	 var total_fare=$('#total_fare'+id).val();
	 var dataString='booking_id='+id+'&waiting='+waiting+'&waiting_time='+waiting_time+'&transfer='+transfer+'&parking='+parking+'&fare='+fare+'&other='+other+'&total_fare='+total_fare+'&job_status=2';
	// alert(dataString);
	}
	 $('#loader'+id).show();
	 var url11 = 'ajax/update_driver_booking.php?'+new Date().getTime();
	 $.ajax({
		      type: "POST",
			  url: url11,
			  data: dataString,
			  success: function(data) {
				  if(data=='success')
				  {
				 // alert(data);
				  $('#loader'+id).delay(500).fadeOut("slow");
				  				  }
				  }
			  });
 });
 
$("#company_name").keyup(function(){
		$('#old_customer_id').val('');
		
		//$("#delivery_address").hide();
		//if any other div are open close them or hide them
		var url11 = 'ajax/find_driver.php?'+new Date().getTime();
		$.ajax({
		type: "POST",
		url: url11,
		data:'keyword='+$(this).val(),
		beforeSend: function(){
			$("#company_name").css("background","#F5C211");
		},
		success: function(data){
			$("#suggesstion-box").show();
			$("#suggesstion-box").html(data);
			$("#company_name").css("background","#green");
		}
		});
	});

	
	function selectDriver(val) {
	var data=val;
	 var data = data.split('-');
	var id=data[0];
	var name=data[1];
	var address=data[2];
	var post_code=data[3];
	var phone=data[4];
	//alert(id);
	$('#new_customer').hide();
	//now show the delivery div as well
	
	$("#company_name").val(name);
	$("#old_customer_id").val(id);
    $('#customer_address1').val(address);
	$('#post_code').val(post_code);
	$('#phone').val(phone);
	$("#suggesstion-box").hide();
	}
	// this is code to fire Ajax request to fetch only selected companies in dropdown
$('.from').datepicker({
	
    	onSelect: function() {
			
                $( ".from" ).datepicker( "option", "dateFormat","dd/mm/yy");
		var from=$('#from').val();
		
        }
        });
$('.to').datepicker({
	
    	onSelect: function() {
		$( ".from" ).datepicker( "option", "dateFormat","dd/mm/yy");
		var from=$('#from').val();	
	    $( ".to" ).datepicker( "option", "dateFormat","dd/mm/yy");
		var to=$(this).val();
		
       // if(from>to){alert("Date From is Greater than Date To");return false;}        
		var date='from='+from+'&to='+to;
		var url11 = 'ajax/find_drivers.php?'+new Date().getTime();
		$.ajax
        ({
              type: "Post",
              url: url11,
              data: date,
              success: function(data)
              {
                 $('#jobs_data').html(data);
              }
		});
			
    }
  
  });

 $('#driver_id').select2({width: '100%'});
 </script>   
       
   </body>
</html>
