
<form action="" method="post">


			
            
            <div class="pull-right col-md-12">
                 <div class="col-md-6 pull-left">
<?php
        $rowcount=mysqli_num_rows($res);

		if($rowcount=='0')
		{
			//echo '<label>Total '.$rowcount.' jobs found. Add some bookings.</label>' ;
		}
?>

</div>
<div class="col-md-6">
<input name="delete_booking" style="margin-bottom:10px;" type="submit" class="btn btn-sm btn-danger pull-right" value="Update All Jobs"/>

</div>
</div>

<table  style="font-family:Montserrat; font-size:13px;" id="jobs" class="table table list_jobs table-hover table-bordered table-striped table-info" cellspacing="0" border="1">

        <thead>
			
            <tr class="btn-primary" style="padding:10px;">

                            <th>Job ID</th>
						    <th>Company</th>
                           <th>Client</th>
                           <th>Start Date</th>
                          <th>End Date</th>
                          
						   
                           <th>Rate</th>
                           <th>Days</th>
						   <th>Parking</th>
                           <th>Transfer</th>
                           <!--th>Customer Fare</th>
                           <th>Other</th-->
                           <th>Total</th>
                           
                          
                          
                           <th><input type="checkbox" id="select_all" /></th>
                          
                           
            </tr>

        </thead>

        <tfoot>

            <tr class="btn-primary">
                           <th>Job ID</th>
						    <th>Company</th>
                           <th>Client</th>
                           <th>Start Date</th>
                          <th>End Date</th>
                          
						   
                           <th>Rate</th>
                           <th>Days</th>
						   <th>Parking</th>
                           <th>Transfer</th>
                           <!--th>Customer Fare</th>
                           <th>Other</th-->
                           <th>Total</th>
                           
                           <th></th>
                           
                          
            </tr>

        </tfoot>

        <tbody>
        <div class="col-md-6 pull-left">
			<?php
            $rowcount=mysqli_num_rows($res);

if($rowcount==0)
{
	//echo '<tr style="cursor:pointer; background-color:#F80307; color:#F3EBEB;"><td></td><td></td><td></td><td><p>You have no Jobs, Add Some Jobs.</p></td></tr>';
}
				

//echo '<p style="text-align:center;background-color:light gray; color:#000000; padding:15px; font-size:18px;">Total '.$rowcount.' jobs found. You can see other jobs by applying filter. Total Amount=£'.number_format($grand_total,2).'</p>';

?>
</div>
        <?php
$id=0;
$i=0;
$tdays=0;
                while($job=mysqli_fetch_assoc($res))

                  {?>      
				  <?php $id=$job['booking_id'];?>
                  <tr class="update_job" row-id="<?php echo $job['booking_id'];?>">
                 
						   <?php
						   
						   if ($job['job_status']==2){?>
                           
							 <td style="background-color:#46E804; color:#0F0F0F;">
                             
                                
						    <?php echo $job['booking_id']; 
						   }else{
								
								echo '<td style="background-color:#F1070B; color:#FFF8F8;">'.$job['booking_id'];
								}
							?>
                           
                            
                            </td>
                           <td><?php echo $job['customer_name'];?></td>
						    <td><?php echo $job['company_contact'];?></td>
                           <td>
						   <?php //$start = date("d/m/y", strtotime($job['start_date'])); 
						   
						  // echo $start  ;?>
						   <input required name="start_date[]" type="text" data-id=<?php echo $id;?> id="start_date<?php echo $id;?>" class="form-control start_date" value=<?php echo $start = date("d/m/Y", strtotime($job['start_date'])); ?>  onkeypress="javascript:return isNumber(event)">
						   
						   </td>
                            <td>
						   <?php //$end = date("d/m/y", strtotime($job['end_date'])); 
						   
						   $end_date1= date("d/m/Y", strtotime($job['end_date']));
						   $days=$job['days'];
						     if($job['substitute']!="")
					 {
					 $driver_id=$job['driver_id'];
					 $customers_sql11="select * from driver_substitute where substitute_id='driver_id' AND booking_id='$id'";
									$t_result11=mysqli_query($con,$customers_sql11);
									while($sub11=mysqli_fetch_assoc($t_result11))
									{
									// $start_date = date("d/m/Y",strtotime($sub11['start_date']));
					   $end_date1 = date("d/m/Y",strtotime($sub11['start_date']));
					   $tdays=$tdays+$sub11['days'];
					    $days=$job['days']-$tdays;
						
						}
						$d=$days-1;
						$end_date1= date('d/m/Y', strtotime($job['start_date']. ' + '.$d.' days'));
						?>
						 <input required name="end_date11[]" type="text" data-id=<?php echo $id;?> id="end_date<?php echo $id;?>" class="form-control end_date" value=<?php echo $end_date1; ?>  onkeypress="javascript:return isNumber(event)">
						 <?php
					 }
					 else
				{
						   //echo $end  ;?>
						   
						   <input required name="end_date[]" type="text" data-id=<?php echo $id;?> id="end_date<?php echo $id;?>" class="form-control end_date" value=<?php echo date("d/m/Y", strtotime($job['end_date'])); ?>  onkeypress="javascript:return isNumber(event)">
					 	   
					<?php
					}
					?>	   
						   </td>
                           
                           <input name="substitute_id[]" type="hidden" id="substitue_id" value=<?php echo $job['driver_id']; ?>>
                           <input name="driver_id[]" type="hidden" id="driver_id" value=<?php echo $job['driver_id']; ?>>
                          <input name="id[]" type="hidden" id="id" value=<?php echo $job['booking_id']; ?>>
                        <?php $id=$job['booking_id'];?>
                         <input name="parking_id[]" type="hidden" id="parking_id" value=<?php echo $job['parking']; ?>>
                       <input name="transfer_id[]" type="hidden" id="transfer_id" value=<?php echo $job['transfer']; ?>>
					    <input name="rate_id[]" type="hidden" id="rate_id" value=<?php echo $job['rate']; ?>>
                      
                        <!--td><input name="waiting[]" type="text" data-id=<?php echo $id;?> id="waiting<?php echo $id;?>" class="form-control waiting" value="<?php echo $job['waiting_charges'];?>"  onkeypress="javascript:return isNumber(event)"></td>
                        <td><input name="parking[]" type="text" data-id=<?php echo $id;?> id="parking<?php echo $id;?>" class="form-control parking" value="<?php echo $job['parking']; ?>"  onkeypress="javascript:return isNumber(event)"></td>
                        <td><input name="fare[]" type="text" data-id=<?php echo $id;?> id="fare<?php echo $id;?>" class="form-control fare" value="<?php echo $job['fare']; ?>"  onkeypress="javascript:return isNumber(event)"></td>
                        <td><input name="other[]" type="text" data-id=<?php echo $id;?> id="other<?php echo $id;?>" class="form-control other" value="<?php echo $job['other']; ?>" onkeypress="javascript:return isNumber(event)"></td-->
                        
						 <td><input required name="rate[]" type="text" data-id=<?php echo $id;?> id="rate<?php echo $id;?>" class="form-control rate" value=<?php echo $job['supp_cost']; ?>  onkeypress="javascript:return isNumber(event)"></td>
                        <td>
						<?php
						 if($job['substitute']!="")
					 {
					 ?>
						<input required name="days11[]" type="text" data-id=<?php echo $id;?> id="days<?php echo $id;?>" class="form-control days" value=<?php echo $days; ?>  onkeypress="javascript:return isNumber(event)">
				<?php
				}
				else
				{
				?>
				<input required name="days[]" type="text" data-id=<?php echo $id;?> id="days<?php echo $id;?>" class="form-control days" value=<?php echo $job['days']; ?>  onkeypress="javascript:return isNumber(event)">
				<?php
				}
				?>
						</td>
                        
						 <td><input required name="parking[]" type="text" data-id=<?php echo $id;?> id="parking<?php echo $id;?>" class="form-control parking" value=<?php echo $job['parking']; ?>  onkeypress="javascript:return isNumber(event)"></td>
						
                        <td><input required name="transfer[]" type="text" data-id=<?php echo $id;?> id="transfer<?php echo $id;?>" class="form-control transfer" value=<?php echo $job['transfer']; ?>  onkeypress="javascript:return isNumber(event)"></td>
						
                      
                        
                         <td><input readonly name="total_fare[]" type="text" data-id=<?php echo $id;?> id="total_fare<?php echo $id;?>" class="form-control total_fare" value=<?php echo $job['supp_cost']*$days+$job['parking']+$job['transfer']; ?>></td>
						 
                 <td style="text-align:right; font-size:20px;"><input name="selected_booking[]" type="checkbox" class="checkbox selected_booking" value=<?php echo $job['booking_id'];?>></td>
              </tr>
			 
   <?php } ?>
     <?php
               while($job_sub=mysqli_fetch_assoc($res_sub))

                  {
				  ?>
				   <?php $id=$job_sub['booking_id'];?>
				  <tr class="update_job" row-id="<?php echo $job_sub['booking_id'];?>">
				  <td style="background-color:#46E804; color:#0F0F0F;">
                    <?php          
				  echo $job_sub['booking_id'];
				  ?>
				   </td>
				   <td><?php
			$sql_sub_book="SELECT * FROM bookings WHERE id=".$job_sub['booking_id'];
			$res_sub_book=mysqli_query($con,$sql_sub_book);	
			$book_sub=mysqli_fetch_assoc($res_sub_book);   
			//echo $book_sub['customer_id'];
$sql_sub_cust="SELECT * FROM customers WHERE id=".$book_sub['customer_id'];
$res_sub_cust=mysqli_query($con,$sql_sub_cust);	
			$cust_sub=mysqli_fetch_assoc($res_sub_cust);   
				    echo $cust_sub['name'];?></td>
						    <td><?php 
							$sql_sub_comp="SELECT * FROM company_contact WHERE id=".$book_sub['passenger_id'];
							$res_sub_comp=mysqli_query($con,$sql_sub_comp);	
			$comp_sub=mysqli_fetch_assoc($res_sub_comp);   
							echo $comp_sub['name'];?></td>
                           <td>
						   <?php //$start = date("d/m/y", strtotime($job['start_date'])); 
						   
						  // echo $start  ;?>
						   <input required name="start_date[]" type="text" data-id=<?php echo $id;?> id="start_date<?php echo $id;?>" class="form-control start_date" value=<?php echo $start = date("d/m/Y", strtotime($job_sub['start_date'])); ?>  onkeypress="javascript:return isNumber(event)">
						   
						   </td>
                            <td>
						   <?php //$end = date("d/m/y", strtotime($job['end_date'])); 
						   
						   //echo $end  ;?>
						   
						   <input required name="end_date[]" type="text" data-id=<?php echo $id;?> id="end_date<?php echo $id;?>" class="form-control end_date" value=<?php echo $start = date("d/m/Y", strtotime($job_sub['end_date'])); ?>  onkeypress="javascript:return isNumber(event)">
						   
						   </td>
                           
                           
                           <input name="driver_id[]" type="hidden" id="driver_id" value=<?php echo $job_sub['driver_id']; ?>>
						    <input name="substitute_id[]" type="hidden" id="substitue_id" value=<?php echo $job_sub['substitute_id']; ?>>
                          <input name="id[]" type="hidden" id="id" value=<?php echo $job_sub['booking_id']; ?>>
                        <?php $id=$job_sub['booking_id'];?>
                         <input name="parking_id[]" type="hidden" id="parking_id" value=<?php echo $job_sub['parking']; ?>>
                       <input name="transfer_id[]" type="hidden" id="transfer_id" value=<?php echo $job_sub['transfer']; ?>>
                        <input name="rate_id[]" type="hidden" id="rate_id" value=<?php echo $job_sub['transfer']; ?>>
						 <td><input required name="rate[]" type="text" data-id=<?php echo $id;?> id="rate<?php echo $id;?>" class="form-control rate" value=<?php echo $job_sub['supp_cost']; ?>  onkeypress="javascript:return isNumber(event)"></td>
                        <td><input required name="days[]" type="text" data-id=<?php echo $id;?> id="days<?php echo $id;?>" class="form-control days" value=<?php echo $job_sub['days']; ?>  onkeypress="javascript:return isNumber(event)"></td>
                        
						 <td><input required name="parking[]" type="text" data-id=<?php echo $id;?> id="parking<?php echo $id;?>" class="form-control parking" value=<?php echo $job_sub['parking']; ?>  onkeypress="javascript:return isNumber(event)"></td>
						
                        <td><input required name="transfer[]" type="text" data-id=<?php echo $id;?> id="transfer<?php echo $id;?>" class="form-control transfer" value=<?php echo $job_sub['transfer']; ?>  onkeypress="javascript:return isNumber(event)"></td>
						
                      
                        
                         <td><input readonly name="total_fare[]" type="text" data-id=<?php echo $id;?> id="total_fare<?php echo $id;?>" class="form-control total_fare" value=<?php echo $job_sub['supp_cost']*$job_sub['days']+$job_sub['parking']+$job_sub['transfer']; ?>></td>
						 
                 <td style="text-align:right; font-size:20px;"><input name="selected_booking[]" type="checkbox" class="checkbox selected_booking" value=<?php echo $job_sub['booking_id'];?>></td>
				   
				   </tr>
				   
				   <?php
				  }
				  ?>  
   		 </tbody>
		 
			   </table>
</form>
<div id="myModal" class="modal fade" role="dialog">
  <div class="modal-dialog">

    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Success</h4>
      </div>
      <div class="modal-body">
        <p>Records Updated Succesfully.</p>
      </div>
      <div class="modal-footer">
        <button id="close_me" type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div>

  </div>
</div>
<?php
if(isset($_POST['selected_booking'])){
/*	
echo "<pre>";
   print_r($_POST);
echo "</pre>";
 */
        $cp=0;
		$cpp=0;
		$ct=0; 
        $ctt=0;
		
        $bookingIds = $_POST;
		$count=count($_POST['selected_booking']);
			
		for ($i=0;$i<$count;$i++){
        $index = array_search($_POST['selected_booking'][$i] ,$bookingIds['id']);
	    //echo "index=".$index;
		$substitute_id=$con->real_escape_string($_POST['substitute_id'][$index]);
			$driver_id=$con->real_escape_string($_POST['driver_id'][$index]);
			 $rate=$con->real_escape_string($_POST['rate'][$index]);
		  $days=$con->real_escape_string($_POST['days'][$index]);
		   $parking=$con->real_escape_string($_POST['parking'][$index]);
		   $transfer=$con->real_escape_string($_POST['transfer'][$index]);
		  $total_fare=$con->real_escape_string($_POST['total_fare'][$index]);

		  $start_date=$con->real_escape_string($_POST['start_date'][$index]);
		  $start_date = str_replace('/', '-', $start_date);
		  $start_date = date("Y-m-d", strtotime($start_date));
		 
  		  $end_date=$con->real_escape_string($_POST['end_date'][$index]);
		  $end_date = str_replace('/', '-', $end_date);
		  $end_date = date("Y-m-d", strtotime($end_date));
		  
		  $parking_id=$con->real_escape_string($_POST['parking_id'][$index]);
		  $transfer_id=$con->real_escape_string($_POST['transfer_id'][$index]);
		  $rate_id=$con->real_escape_string($_POST['rate_id'][$index]);
		//  $waiting_time=$con->real_escape_string($_POST['waiting_time'][$index]);
		 // $waiting_charges=$con->real_escape_string($_POST['waiting'][$index]);
		 // $parking=$con->real_escape_string($_POST['parking'][$index]);
		 // $fare=$con->real_escape_string($_POST['fare'][$index]);
		  // $other=$con->real_escape_string($_POST['other'][$index]);
		 // $total_fare=$con->real_escape_string($_POST['total_fare'][$index]);
		  
		  $booking_id=$con->real_escape_string($_POST['selected_booking'][$i]);
		 // echo '<script>alert("'.$booking_id.'"); <script>';
		 // echo '<script>alert("'.$driver_id.'"); <script>';
		 // echo '<script>alert("'.$substitute_id.'");<script>';
/*		  
$sql1="UPDATE driver_bookings SET
waiting_time='$waiting_time', 
waiting_charges='$waiting_charges', 
parking='$parking', 
fare='$fare', 
total_fare='$total_fare', 
other='$other',
status='2',
invoice_status=1
WHERE booking_id='$booking_id'";
*/
$sql11="SELECT * FROM driver_bookings WHERE booking_id='$booking_id' AND driver_id='$substitute_id'";
$sql11result=mysqli_query($con,$sql11);
$sql11driver=mysqli_num_rows($sql11result);
 //echo '<script>alert("'.$sql11driver.'"); <script>';
if($sql11driver>0)
{
$sql1="UPDATE driver_bookings SET

parking='$parking', 
transfer='$transfer',
total_fare='$total_fare', 
status='2',
invoice_status=1
WHERE booking_id='$booking_id'";
//echo $sql1;
//exit;
$ctotal= $rate_id*$days+$parking+$transfer; 
$sql2="UPDATE bookings SET
supp_cost='$rate', 

days='$days', 
total_fare='$ctotal', 
job_status='2',
start_date='$start_date',
end_date='$end_date',
parking='$parking',
transfer='$transfer'

WHERE id='$booking_id'";



if (!mysqli_query($con,$sql1))
  {
  echo("Error description: " . mysqli_error($con));
  exit;
  }
  
 if (!mysqli_query($con,$sql2))
  {
  echo("Error description: " . mysqli_error($con));
  exit;
  }

}
elseif($sql11driver<1)
{
//echo '<script>alert("'.$booking_id.'"); <script>';
$sql111="UPDATE driver_substitute SET
supp_cost='$rate', 
days='$days', 
parking='$parking', 
transfer='$transfer',
total_fare='$total_fare', 
status='2',
start_date='$start_date',
end_date='$end_date'
WHERE booking_id='$booking_id'";

if (!mysqli_query($con,$sql111))
  {
  echo("Error description: " . mysqli_error($con));
  exit;
  }
  
$sqlbook="SELECT * FROM bookings WHERE id='$booking_id'";
$sqlbookresult=mysqli_query($con,$sqlbook);
$sqlbookdriver=mysqli_fetch_assoc($sqlbookresult);
  $cp=$sqlbookdriver['parking'] - $parking_id;
  
  $cpp=$cp + $parking;
 
  $ct=$sqlbookdriver['transfer']-$transfer_id;
  $ctt=$ct+$transfer;
  
  $cd=$sqlbookdriver['days'];
  $ctotal=$sqlbookdriver['rate']*$cd+$cpp+$ctt;
 
$sql2book="UPDATE bookings SET
supp_cost='$rate', 

days='$days', 
total_fare='$ctotal', 
job_status='2',

parking='$cpp',
transfer='$ctt'

WHERE id='$booking_id'";
if (!mysqli_query($con,$sql2book))
  {
  echo("Error description: " . mysqli_error($con));
  exit;
  }  

}

  echo '<script>alert("Bookings Updated Successfully"); window.location.replace("list_driver_jobs.php");</script>';
         }
       }
    ?>
