<?php
if(basename($_SERVER['PHP_SELF']) == 'loaded.php') 
  $nv=" class='current'";
include'header.php';
$custo=$conde='';
$castoi=$condi='';
$t=$p=0;

$dato=$datos=$Date;

    // *************************** Add payment *******************************
    if(isset($_POST['toll']))
		{
			$custo=$_POST['custo'];
			$bulk=$_POST['bulk'];
			$n=$_POST['n'];
			$dato=$_POST['dato'];
			$pri=str_replace(',', '', $_POST['amo']);
			$curre=str_replace(',', '', $_POST['curre']);
			$amo=$pri*$curre;
			$descri=str_replace("'", "`", $_POST['descri']);
			$type=$_POST['type'];
			$rate=$_POST['curre'];
			$drive=$_POST['drive'];
			$t=1;                   $lim=1000;
			
			if($type=='OTHER EXPENSE')
			    $ty=1;
			else
			    $ty=0;
			
		for($i=1; $i<$n; $i++){
			$plat=$_POST["code$i"];
			$fna=$_POST["plat$i"];
			$dri=$_POST["driv$i"];
			$code=$_POST["trip$i"];
			$amoo=number_format($amo, 2);
				
				if($amo AND ($dri==$drive OR $drive=='ALL DRIVERS')){
	$set=mysqli_query($conn, "INSERT INTO `repair` (`Vehicle`, `Amount`, `Garage`, `Items`, `Issue`, `Date`, `Repair`, `Driver`, `Trip`, `User`, `Time`, `External`, `Plate`, `Type`, `Rate`, `Bulk`) VALUES ('$plat', '$amo', '$type', '0', '$descri', '$dato', '', '$dri', '$code', '$loge', '$Time', '1', '$fna', '$ty', '$rate', '$bulk')");
	
	$tgd=mysqli_query($conn, "INSERT INTO `trage` (`Date`, `Time`, `User`, `Plate`, `Action`, `Descri`) VALUES ('$Date', '$Time', '$loge', '$fna', 'MILEAGE', 'Paid $amoo for mileage (trip)')");	
	
	$seti=mysqli_query($conn, "UPDATE `trips` SET `Mileage`='$amo' WHERE `Number`='$code' AND `Bulk`='$bulk' ORDER BY `Number` DESC LIMIT 1");
				}
		    }
	$conde="AND (`trips`.`Bulk` = '$bulk' AND `vehicles`.`Bulk` = '$bulk')";
		}
		
		
		
		// ************************* Add vehicles ***********************	
		
    if(isset($_POST['dinco']))
		{
	
	    for($i=1; $i<=$_POST['k']; $i++){
	        
//	$so=mysqli_query($conn, "INSERT INTO `income` (`Date`, `Vehicle`, `Distance`, `Driver`, `Weight`, `District`, `Location`, `Descri`, `Amount`,  `Status`, `Rate`, `Customer`, `User`, `Time`, `Trip`, `External`, `Plate`, `Bulk`, `Custo`) VALUES ('$Date', '$pla', '$dista', '$dri', '$capa', '$final', '$loca', '$descri', '$amo', '0', '$curr', '$customer', '$loge', '$Time', '$trino', '1', '$fna', '$bulk', '$custmo')");
            }
		}
		
	// ************************* Add vehicles ***********************			
		
    if(isset($_POST['savalle']))
		{
			$desti=str_replace("'", "`", $_POST['desti']);
			$loca=str_replace("'", "`", $_POST['loca']);
			$final=str_replace("'", "`", $_POST['final']);
			$capa=str_replace(',', '', $_POST['capa']);
			$dista=str_replace(',', '', $_POST['dista']);
			$descri=str_replace("'", "`", $_POST['descri']);
			$custo=$_POST['custo'];
			$bulk=$_POST['bulk'];
			$etd=$_POST['etd'];
			$eta=$_POST['eta'];
			$a=$_POST['a'];
			$lim=1000;
			
			for($t=1; $t<=$a; $t++){
			$pla=$_POST["plate$t"];
	    if($pla){
$doi=mysqli_query($conn, "SELECT `Plate`, `Driver`, `Tripo` FROM `vehicles` WHERE `Number`='$pla'");
			$roi=mysqli_fetch_assoc($doi);
				$fna=$roi['Plate'];
				$dri=$roi['Driver'];
				$trino=$roi['Tripo'];
				
            $custmo='';
	
			$customer=$_POST['customer'];
			$amo=str_replace(',', '', $_POST['amo']);
			$curr=$_POST['currency'];
			
	     if($_POST['customer']=='290009399190'){
                $custmo="WAITING CARGO";
                $buld="99991695392760";
				
	 $eld = strtotime("+3 day", strtotime("$etd"));
				$eld=date("Y-m-d", $eld);
				
	 $ead = strtotime("+7 day", strtotime("$etd"));
				$ead=date("Y-m-d", $ead);
		
$set=mysqli_query($conn, "INSERT INTO `trips` (`Date`, `Time`, `User`, `Vehicle`, `Destination`, `Location`, `Final`, `Capacity`, `Distance`, `ETD`, `ETA`, `Description`, `Status`, `Plate`, `Driver`, `Stadate`, `Destin`, `Findes`, `ELD`, `EAD`, `Bulk`) VALUES ('$Date', '$Time', '$loge', '$pla', '$desti', '$loca', '$final', '$capa', '$dista', '$etd', '$eta', '$descri', '0', '$fna', '$dri', '$etd', '$loca', '$final', '$eld', '$ead', '$buld')");
        $trino=mysqli_insert_id($conn);
            }
            else{
    $doi=mysqli_query($conn, "SELECT `Customer` FROM `account` WHERE `Number`='$customer'");
			$roi=mysqli_fetch_assoc($doi);
				$custmo=$roi['Customer'];
				$buld=$bulk;
	    
	$so=mysqli_query($conn, "INSERT INTO `income` (`Date`, `Vehicle`, `Distance`, `Driver`, `Weight`, `District`, `Location`, `Descri`, `Amount`,  `Status`, `Rate`, `Customer`, `User`, `Time`, `Trip`, `External`, `Plate`, `Bulk`, `Custo`) VALUES ('$Date', '$pla', '$dista', '$dri', '$capa', '$final', '$loca', '$descri', '$amo', '0', '$curr', '$customer', '$loge', '$Time', '$trino', '1', '$fna', '$bulk', '$custmo')");
            }

	$then=mysqli_query($conn, "UPDATE `vehicles` SET `Tripo`='$trino', `Locate`='', `Loading`='0', `Bulk`='$buld', `Custo`='$custmo' WHERE `Number`='$pla' AND `Plate`='$fna' ORDER BY `Number` ASC LIMIT 1");
	
	$tgd=mysqli_query($conn, "INSERT INTO `trage` (`Date`, `Time`, `User`, `Plate`, `Action`, `Descri`) VALUES ('$Date', '$Time', '$loge', '$fna', 'TRIP', 'Added to $custmo')");	
	
	$then=mysqli_query($conn, "UPDATE `trips` SET `Bulk`='$buld', `Custo`='$custmo' WHERE `Number`='$trino' ORDER BY `Number` ASC LIMIT 1");
		    }
		}
	$conde="AND (`trips`.`Bulk` = '$bulk' AND `vehicles`.`Bulk` = '$bulk')";
		$t=1;
	}
	
	
	
	
	
		
	// ************************* Add customer ***********************			
		
    if(isset($_POST['addco']))
		{
			$bulk=time();
			$date=$_POST['date'];
			$descri=str_replace("'", "`", $_POST['descri']);
			$a=$_POST['a'];
			$lim=1000;
			
			for($t=1; $t<=$a; $t++){
			$pla=$_POST["plate$t"];
	    if($pla){
$doi=mysqli_query($conn, "SELECT `Number`, `Plate`, `Driver` FROM `vehicles` WHERE `Number`='$pla'");
			$roi=mysqli_fetch_assoc($doi);
				$fna=$roi['Plate'];
				$dri=$roi['Driver'];
				$nuo=$roi['Number'];
	
$dos=mysqli_query($conn, "SELECT *FROM `trips` WHERE `Vehicle`='$pla' AND `Status`='0' ORDER BY `Number` DESC LIMIT 1");
$ros=mysqli_fetch_assoc($dos);
$loca=$ros['Location'];
$final=$ros['Final'];
$dista=$ros['Distance'];
$capa=$ros['Capacity'];
$tipo=$ros['Number'];
	
            if($_POST['customer']){
			$customer=$_POST['customer'];
			$amo=str_replace(',', '', $_POST['amo']);
			$curr=$_POST['currency'];
	    
        $doi=mysqli_query($conn, "SELECT `Customer` FROM `account` WHERE `Number`='$customer'");
			$roi=mysqli_fetch_assoc($doi);
				$custmo=$roi['Customer'];
	    
	$so=mysqli_query($conn, "INSERT INTO `income` (`Date`, `Vehicle`, `Distance`, `Driver`, `Weight`, `District`, `Location`, `Descri`, `Amount`,  `Status`, `Rate`, `Customer`, `User`, `Time`, `Trip`, `External`, `Plate`, `Bulk`, `Custo`) VALUES ('$date', '$pla', '$dista', '$dri', '$capa', '$final', '$loca', '$descri', '$amo', '0', '$curr', '$customer', '$loge', '$Time', '$tipo', '1', '$fna', '$bulk', '$custmo')");
	
	
	$then=mysqli_query($conn, "UPDATE `vehicles` SET `Bulk`='$bulk', `Locate`='', `Loading`='0', `Custo`='$custmo' WHERE `Number`='$nuo' ORDER BY `Number` ASC LIMIT 1");
	
	$tgd=mysqli_query($conn, "INSERT INTO `trage` (`Date`, `Time`, `User`, `Plate`, `Action`, `Descri`) VALUES ('$Date', '$Time', '$loge', '$fna', 'CUSTOMER', 'Added to $custmo from waiting')");	
		
	$then=mysqli_query($conn, "UPDATE `trips` SET `Description`='$descri', `Bulk`='$bulk', `Custo`='$custmo' WHERE `Number`='$tipo' ORDER BY `Number` DESC LIMIT 1");
            }
		    }
		}
	$conde="AND (`trips`.`Bulk` = '$bulk' AND `vehicles`.`Bulk` = '$bulk')";
	    $t=0;	
	}
	
	
	
	
	
	
	
	
	// ************************** Create new trip ***********************		
		
if(isset($_POST['tridis']))
		{
			$desti=str_replace("'", "`", $_POST['desti']);
			$loca=str_replace("'", "`", $_POST['loca']);
			$final=str_replace("'", "`", $_POST['final']);
			$capa=str_replace(',', '', $_POST['capa']);
			$dista=str_replace(',', '', $_POST['dista']);
			$descri=str_replace("'", "`", $_POST['descri']);
			$etd=$_POST['etd'];
			$eta=$_POST['eta'];
			$a=$_POST['a'];
			$bulk=time();
			
			
			for($t=1; $t<=$a; $t++){
			$pla=$_POST["plate$t"];
    	if($pla){
$doi=mysqli_query($conn, "SELECT `Plate`, `Driver` FROM `vehicles` WHERE `Number`='$pla'");
			$roi=mysqli_fetch_assoc($doi);
				$fna=$roi['Plate'];
				$dri=$roi['Driver'];
				
	 $eld = strtotime("+3 day", strtotime("$etd"));
				$eld=date("Y-m-d", $eld);
				
	 $ead = strtotime("+7 day", strtotime("$etd"));
				$ead=date("Y-m-d", $ead);
		
$set=mysqli_query($conn, "INSERT INTO `trips` (`Date`, `Time`, `User`, `Vehicle`, `Destination`, `Location`, `Final`, `Capacity`, `Distance`, `ETD`, `ETA`, `Description`, `Status`, `Plate`, `Driver`, `Stadate`, `Destin`, `Findes`, `ELD`, `EAD`, `Bulk`) VALUES ('$Date', '$Time', '$loge', '$pla', '$desti', '$loca', '$final', '$capa', '$dista', '$etd', '$eta', '$descri', '0', '$fna', '$dri', '$etd', '$loca', '$final', '$eld', '$ead', '$bulk')");
        $trino=mysqli_insert_id($conn);
            $customo='';
	
	
			$customer=$_POST['customer'];
			$amo=str_replace(',', '', $_POST['amo']);
			$curr=$_POST['currency'];
			
	     if($_POST['customer']=='290009399190'){
                $customo="WAITING CARGO";
                $bulk="99991695392760";
                $addo="TRIP";
            }
            else{
$doi=mysqli_query($conn, "SELECT `Customer` FROM `account` WHERE `Number`='$customer'");
			$roi=mysqli_fetch_assoc($doi);
				$customo=$roi['Customer'];
				$addo="CUSTOMER";
	    
	$so=mysqli_query($conn, "INSERT INTO `income` (`Date`, `Vehicle`, `Distance`, `Driver`, `Weight`, `District`, `Location`, `Descri`, `Amount`,  `Status`, `Rate`, `Customer`, `User`, `Time`, `Trip`, `External`, `Plate`, `Bulk`, `Custo`) VALUES ('$Date', '$pla', '$dista', '$dri', '$capa', '$final', '$loca', '$descri', '$amo', '0', '$curr', '$customer', '$loge', '$Time', '$trino', '1', '$fna', '$bulk', '$customo')");
    	}
    	
	$then=mysqli_query($conn, "UPDATE `vehicles` SET `Tripo`='$trino', `Bulk`='$bulk', `Locate`='', `Loading`='0', `Custo`='$customo' WHERE `Number`='$pla' AND `Plate`='$fna' ORDER BY `Number` ASC LIMIT 1");
	
	$tgd=mysqli_query($conn, "INSERT INTO `trage` (`Date`, `Time`, `User`, `Plate`, `Action`, `Descri`) VALUES ('$Date', '$Time', '$loge', '$fna', '$addo', 'Added to new trip [$customo]')");	
		
	$then=mysqli_query($conn, "UPDATE `trips` SET `Bulk`='$bulk', `Custo`='$customo' WHERE `Number`='$trino' ORDER BY `Number` DESC LIMIT 1");
            
		}
		}
		$t=0;
		}
		
        
        if(isset($_POST['search']))
		    {
			$custoi=$_POST['custoi'];
			$dato=$_POST['dato'];
			$datos=$_POST['datos'];
			$limz=500;
		    
		    if($custoi){
		    $condi="AND `trips`.`Custo` LIKE '%$custoi%' AND `trips`.`Date` BETWEEN '$dato' AND '$datos'";
		    }
		    else{
		    $condi="AND `trips`.`Date` BETWEEN '$dato' AND '$datos'";
		    }
		    }
		else{
		    $condi="AND `trips`.`Offload`='0'";
		    $limz=50;
		}
		
		
	if(isset($_POST['open']))
		{
			$custo=$_POST['custo'];
			$bulk=$_POST['bulk'];
			$_SESSION['Bulk']=$bulk;
			$deloc=$_POST['deloc'];
			$t=1;               $lim=1000;
			
	$conde="AND (`trips`.`Bulk` = '$bulk' AND `vehicles`.`Bulk` = '$bulk')";
		}
		
		
    if(isset($_GET['vehi']) AND isset($_GET['trip']) AND isset($_GET['bulk']))
		{
			$vehi=$_GET['vehi'];
			$trip=$_GET['trip'];
			$bulk=$_GET['bulk'];
			$fna=$_GET['fna'];
			$custo=$_GET['custo'];
			$t=1;               $lim=1000;
			
			if($custo=='WAITING CARGO'){
	$then=mysqli_query($conn, "UPDATE `vehicles` SET `Bulk`='0', `Locate`='ALLOCATED', `Loading`='1', `Custo`='' WHERE `Number`='$vehi' ORDER BY `Number` ASC LIMIT 1");
	
	$tgd=mysqli_query($conn, "INSERT INTO `trage` (`Date`, `Time`, `User`, `Plate`, `Action`, `Descri`) VALUES ('$Date', '$Time', '$loge', '$fna', 'REMOVE', 'Removed from waiting cargo')");	
		
	$deli=mysqli_query($conn, "DELETE FROM `trips` WHERE `Number`='$trip' AND `Bulk`='$bulk' ORDER BY `Number` DESC LIMIT 1");
			}
			else{
            $bullo="99991695392760";
            $custom="WAITING CARGO";
	$then=mysqli_query($conn, "UPDATE `vehicles` SET `Bulk`='$bullo', `Locate`='', `Loading`='0', `Custo`='$custom' WHERE `Number`='$vehi' ORDER BY `Number` ASC LIMIT 1");
	
	$tgd=mysqli_query($conn, "INSERT INTO `trage` (`Date`, `Time`, `User`, `Plate`, `Action`, `Descri`) VALUES ('$Date', '$Time', '$loge', '$fna', 'REMOVE', 'Removed from customer [$custo]')");	
		
	$deli=mysqli_query($conn, "UPDATE `trips` SET `Description`='Removed for waiting', `Custo`='$custom', `Bulk`='$bullo' WHERE `Number`='$trip' AND `Bulk`='$bulk' ORDER BY `Number` DESC LIMIT 1");
			
	$dele=mysqli_query($conn, "DELETE FROM `income` WHERE `Trip`='$trip' AND `Bulk`='$bulk' AND `Vehicle`='$vehi' ORDER BY `Number` DESC LIMIT 1");
			}
			
	$conde="AND (`trips`.`Bulk` = '$bulk' AND `vehicles`.`Bulk` = '$bulk')";
		}
		
		
		
		// ******************** Offload vehicles **********************
    if(isset($_POST['savall']))
		{
			$lokete=str_replace("'", "`", $_POST['lokete']);
			$a=$_POST['a'];
			
			for($t=1; $t<=$a; $t++){
			$pla=$_POST["plate$t"];
			$fna=$_POST["fna$t"];
	if($pla){
	    if($lokete=='DEPANNAGE'){
$doi=mysqli_query($conn, "UPDATE `vehicles` SET `Depane`='1' WHERE `Number`='$pla' ORDER BY `Number` ASC LIMIT 1");

	$tgd=mysqli_query($conn, "INSERT INTO `trage` (`Date`, `Time`, `User`, `Plate`, `Action`, `Descri`) VALUES ('$Date', '$Time', '$loge', '$fna', 'ADDITIONAL FUEL', 'Added vehicle to additional fuel')");
	    }
	    else{
$doi=mysqli_query($conn, "UPDATE `vehicles` SET `Loading`='1', `Bulk`='0', `Custo`='', `Locate`='', `Doff`='$Date' WHERE `Number`='$pla' ORDER BY `Number` ASC LIMIT 1");

	$tgd=mysqli_query($conn, "INSERT INTO `trage` (`Date`, `Time`, `User`, `Plate`, `Action`, `Descri`) VALUES ('$Date', '$Time', '$loge', '$fna', 'OFFLOAD', 'Offload vehicle from trip')");	

$then=mysqli_query($conn, "UPDATE `trips` SET `Offload`='1' WHERE `Vehicle`='$pla' ORDER BY `Number` DESC LIMIT 1");
}
		    }
		        }
		}
		
		
if($t){
    $doi=mysqli_query($conn, "SELECT `trips`.* FROM `trips` INNER JOIN `vehicles` ON `trips`.`Vehicle` = `vehicles`.`Number` WHERE `trips`.`Status`='0' AND `trips`.`Offload`='0' $conde GROUP BY `trips`.`Vehicle` ORDER BY `trips`.`ETA` DESC LIMIT $lim");
    }
else{
    $doi=mysqli_query($conn, "SELECT COUNT(DISTINCT(`vehicles`.`Number`)) AS `Vehi`, `trips`.* FROM `trips` INNER JOIN `vehicles` ON `trips`.`Bulk` = `vehicles`.`Bulk` WHERE `trips`.`Status`='0' $condi GROUP BY `trips`.`Bulk` ORDER BY `trips`.`Bulk` DESC LIMIT $limz");
        }
    $foi=mysqli_num_rows($doi);
?>


<script>
$(document).ready(function() {
$('input[type="checkbox"]').change(function(){
var total_checked=  $("input[type='checkbox']:checked").length 
$("#d1").html("Selected Vehicles: <b> " + total_checked );
});
/////////////
var total_checked=  $("input[type='checkbox']:checked").length 
$("#d1").html("Selected Vehicles: <b> " + total_checked );
///////

$('input[type="checkbox"]').change(function(){
var total_checked=  $("input[type='checkbox']:checked").length 
$("#d2").html("Selected Vehicles: <b> " + total_checked );
});
/////////////
var total_checked=  $("input[type='checkbox']:checked").length 
$("#d2").html("Selected Vehicles: <b> " + total_checked );
///////

$('input[type="checkbox"]').change(function(){
var total_checked=  $("input[type='checkbox']:checked").length 
$("#d3").html("Selected Vehicles: <b> " + total_checked );
});
/////////////
var total_checked=  $("input[type='checkbox']:checked").length 
$("#d3").html("Selected Vehicles: <b> " + total_checked );
///////
});


   function check_uncheck_checkbox(isChecked) {
	if(isChecked) {
		$('input[type="checkbox"]').each(function() { 
			this.checked = true; 
		});
	} else {
		$('input[type="checkbox"]').each(function() {
			this.checked = false;
		});
	}
}	

 function check_uncheck_checkboxes(isChecked) {
	if(isChecked) {
		$('input[type="checkbox"]').each(function() { 
			this.checked = true; 
		});
	} else {
		$('input[type="checkbox"]').each(function() {
			this.checked = false;
		});
	}
}	

 function check_uncheck_checkboxec(isChecked) {
	if(isChecked) {
		$('input[type="checkbox"]').each(function() { 
			this.checked = true; 
		});
	} else {
		$('input[type="checkbox"]').each(function() {
			this.checked = false;
		});
	}
}

</script>

<div class="container-fluid main-content">
        <div class="page-title hidden-xs hidden-print">
          <h2 style='margin-top:-20px; margin-bottom: 5px; color:#ffcc33'>
         Vehicles
          </h2>
                 </div>
     
        <div class="row">
  <div class="col-lg-2 hidden-print">
   
   <ul class="list-group">

	   <li class="list-group-item">
              <a href="locate.php">
                <p>
                <i class="lnr lnr-menu-circle"></i>&nbsp;Allocated Vehicles
                </p>
              </a></li> 
                  
			  <li class="list-group-item">
              <a href="ment.php">
                <p>
                <i class="lnr lnr-plus-circle"></i>&nbsp;Repair & Services
                </p>
              </a></li>

    <li class="list-group-item">
	  <a href="mainsto.php">
                <p>
                <i class="lnr lnr-menu-circle"></i>&nbsp;List of Vehicles
                </p>
              </a></li>
                       
            </ul><br><br>

			<li class="list-group-item" style="border:1px dashed #cccccc; border-radius:5px; margin:0px 0px 4px 0px;">
	  <a href="createa.php">
                <p>
                <i class="lnr lnr-plus-circle"></i>&nbsp;Create Customers
                </p>
              </a></li>	
              
            <li class="list-group-item active" style="border:1px dashed #cccccc; border-radius:5px; margin:0px 0px 4px 0px;">
	  <a href="dispatch.php">
                <p>
                <i class="lnr lnr-plus-circle"></i>&nbsp;Create Vehicle Trip
                </p>
              </a></li>	
              
            <li class="list-group-item" style="border:1px dashed #cccccc; border-radius:5px; margin:0px 0px 4px 0px;">
	  <a href="addinco.php">
                <p>
                <i class="lnr lnr-plus-circle"></i>&nbsp;Add Vehicle Income
                </p>
              </a></li>	
			 
                <?php
              if($_SESSION['Cpo']){
                  ?>
			  <li class="list-group-item" style="border:1px dashed #cccccc; border-radius:5px; margin:0px 0px 4px 0px;">
              <a href="purcha.php">
                <p>
                <i class="lnr lnr-plus-circle"></i>&nbsp;Purchase Orders
                </p>
              </a></li>
              <?php
              }
              if($_SESSION['Cpi']){
                  ?>
            <li class="list-group-item" style="border:1px dashed #cccccc; border-radius:5px; margin:0px 0px 4px 0px;">
	  <a href="profo.php">
                <p>
                <i class="lnr lnr-plus-circle"></i>&nbsp;Create Proforma
                </p>
              </a></li>	
              <?php
              }
              ?>
              
            <li class="list-group-item" style="border:1px dashed #cccccc; border-radius:5px; margin:0px 0px 4px 0px;">
	  <a href="payslip.php">
                <p>
                <i class="lnr lnr-plus-circle"></i>&nbsp;Payment Vouchers
                </p>
              </a></li>	
              
            <li class="list-group-item" style="border:1px dashed #cccccc; border-radius:5px; margin:0px 0px 4px 0px;">
	  <a href="conterepo.php">
                <p>
                <i class="lnr lnr-plus-circle"></i>&nbsp;Container Dispatch
                </p>
              </a></li></ul>
  </div>
  
   <?php
          if($_SESSION['Ovt']=='1' OR $_SESSION['Alt']=='1'){
          // *************** Offload vehicles **********************
	include'allocate.php';
          }
   
        include'trimodal.php';
    ?>           
         
        <div class="col-lg-10">
                  <div class="row hidden-print">
         
         <div class="col-lg-5" style="padding-left:40px;">
           <div class="col-lg-3">
               <?php
             if($_SESSION['Cnt']){
                 ?>
       <button class="btn  btn-warning btn-block" type="button" data-toggle='modal' data-target='#Modaldisc' style='font-size:11px;'> Create New Trip </button>
       <?php
             }
             ?>
            </div>
             
           
         <form action="tmile.php" method="post" class="form-horizontal" enctype="multipart/form-data" name='myform'> 
           <div class="col-lg-3 hidden-print"> <button class="btn btn-info btn-block" type="submit" style='font-size:11px;'> Trip Report </button> </div></form> 
           
         <form action="milpay.php" method="post" class="form-horizontal" enctype="multipart/form-data" name='myform'> 
           <div class="col-lg-3 hidden-print"> <button class="btn btn-success btn-block" type="submit" style='font-size:11px;'> Mileage Report </button> </div></form> 
           
          
           <div class="col-lg-3 hidden-print"><?php
           if($_SESSION['Alt']){
               ?>
        <button class="btn  btn-warning btn-block" type="submit" data-toggle='modal' data-target='#Modaldixi' style='font-size:11px;'> Loaded Vehicles </button><?php
        }
        ?></div>
						
        </div>  
           

        <form action="" method="post" class="form-horizontal ">                  
        <div class="col-lg-7 hidden-print"><div class="col-lg-1 text-center"> 				
         </div>
            <div class="col-lg-3">
      <input class="form-control"  name="custoi" type="text" id="searchi" autofocus="autofocus">
			</div> 
			
			 <div class="col-lg-3 hidden-print"> 
           <div class="input-group date" data-provide="datepicker">
      <input class="form-control form-center" name="dato" type="text" value="<?php echo $dato ?>" onkeypress='return isNumberKey(event)' required>
	  <span class="input-group-addon"><i class="lnr lnr-calendar-full"></i></span>
			</div></div>
			
			 <div class="col-lg-3 hidden-print"> 
           <div class="input-group date" data-provide="datepicker">
      <input class="form-control form-center" name="datos" type="text" value="<?php echo $datos ?>" onkeypress='return isNumberKey(event)' required>
	  <span class="input-group-addon"><i class="lnr lnr-calendar-full"></i></span>
			</div></div>
                       
        <div class="col-lg-2 hidden-xs hidden-print">
        <button class="btn  btn-primary btn-block" type="submit" name="search"><i class="lnr lnr-magnifier"></i> Search</button>
                   
					  </div>
                         
                  </div>    
                     
                  
            </form> 
             
               
            </div>
             <div class="row">
            <div class="col-lg-12" style='margin-top:-20px;'>
             <span class="hidden-print"> &nbsp;&nbsp; Total Records Found : <b><?php echo" $foi " ?></b></span><span class="pull-right text-right">&nbsp;&nbsp;&nbsp;&nbsp;<a href="#" onclick="return window.print()" class="hidden-print" title='Print' data-toggle='tooltip' data-placement='top'><i class="lnr lnr-printer"></i></a>&nbsp;&nbsp;&nbsp;</span>
             <?php
             if($t){
                 ?>
              <span class="pull-right hidden-print">
		&nbsp;&nbsp;&nbsp;&nbsp;<a href="#" onclick=window.location.href='data.php' class="hidden-print" title='Export to excell' data-toggle='tooltip' data-placement='top'><i class="lnr lnr-line-spacing"></i></a></span>
		        <?php
             }
             ?>
		
            <div class="widget-container fluid-height clearfix">
            <div class="widget-content padded clearfix">
               
					<?php
			if($t){
	            $t=1;
	            /*
           while($roi=mysqli_fetch_assoc($doi)){
               $loke=$roi['Locate'];
               $dte=$roi['Date'];
               $inra=number_format($roi['Rate'], 2);
               $bulko=$roi['Bulk'];
               $acco=$cust=$custo;
               
    $do=mysqli_query($conn, "SELECT `vehicles`.* FROM `vehicles` WHERE `vehicles`.`Status`='0' AND `vehicles`.`Bulk`='$bulko' ORDER BY `Number` ASC LIMIT 1000");
    */
            $fo=mysqli_num_rows($doi);
        $ipla=$inko=$ino=array();
    echo"<div class='panel panel-default' style='border:1px solid powderblue;'>
			<div class='panel-heading' style='background-color:powderblue; padding-left:30px; font-weight:bold;'> $custo &nbsp; [$deloc] &nbsp;  <label class='pull-right text-info' style='font-weight:bold; padding-right:30px;'> $fo VEHICLES </label></div>
			<form action='' method='post'>
	
			<div class='panel-body' style='padding-top:0px;'> 
			 <table class='table table-striped table-hover' id='htmltable'>     
                    <thead><tr role='row'><th width='1%'> No </th>
                       <th><div align='center'> DATE </th>
                     <th><div align='center'> TRUCK&nbsp;ID </th>
                     <th><div align='center'> TRAILER&nbsp;ID </th>
						 <th><div align='center'> DRIVER'S NAME </th>
					   <th><div align='center'> DRIVING LICENSE </th>
						 <th><div align='center'> PC/LP </th>
						 <th><div align='center'> TELEPHONE </th>
			            <th><div align='center'> LOADED </th>
		<th class='hidden-print' width='1%'><div align='center'> # </th></tr>
                    </thead><tbody>";

	   $n=1;				$tlo=0;                  $dris=array();
		
		 while($reti=mysqli_fetch_assoc($doi)){
			    $trino=$reti['Number'];
			    $desto=$reti['Destination'];
			    $locoo=$reti['Location'];
			    $fino=$reti['Final'];
			    $disto=$reti['Distance'];
			    $capo=$reti['Capacity'];
			    $code=$reti['Vehicle'];
			    $vid=$reti['Plate'];
			    $etao=$reti['ETA'];
			    $etdo=$reti['ETD'];
			    $descro=$reti['Description'];
			    $source=$reti['Driver'];
			    $bul=$bulko=$reti['Bulk'];
			    $cusi=$cust=$reti['Custo'];
			    $offed=$reti['Offload'];
			
		$sete=mysqli_query($conn, "SELECT `employees`.`Idno`, `employees`.`Contact1`, `employees`.`Contact2`, `employees`.`Fullname` FROM `employees` WHERE `Fullname`='$source' ORDER BY `Eid` DESC LIMIT 1");
		    if($fete=mysqli_num_rows($sete)){
			    $rete=mysqli_fetch_assoc($sete);
			        $tle=$rete['Contact1'];
			        $pcl=$rete['Contact2'];
			        $idno=$rete['Idno'];
			        $driv=$rete['Fullname'];
		            }
		        else
		            $tle=$pcl=$idno=$driv="";
			    
			 if(!$source)
			    $source=$driv;
			    $dris[$n]=$source;
			    $trip=$trino;
			    
        $sete=mysqli_query($conn, "SELECT `Bulk`, `Loaded`, `Trailer`, `Tripo` FROM `vehicles` WHERE `Number`='$code' AND `Status`='0'");
                    $rete=mysqli_fetch_assoc($sete);
                            $buko=$rete['Bulk'];
                            $load=$rete['Loaded'];
                            $tre=$rete['Trailer'];
                            $tripo=$rete['Tripo'];
                            if($rete['Loaded']){
                                $lodo="YES";
                                $tlo++;
                            }
                            else
                                $lodo="NO";
                            
                if(!$tle OR !$pcl OR !$idno)
					$stn="style='padding:0px; color:blue;'";
				else
					$stn="style='padding:0px;'";
						
						
					if($offed=='1')
						$stn="style='padding:0px; color:red;'";
						
		if($bul!=$buko AND $cusi=='WAITING CARGO')
			$stn="style='padding:0px; color:red;'";

		print("<tr><td $stn><div align='center'> $n </td>
		<td class='text-center' $stn> $etdo </td>
		<td $stn><div align='center'> $vid </td>
		<td $stn><div align='center'> $tre </td>
			  <td $stn> $source </td>
			  <td CLASS='text-left' $stn> $idno </td>
			  <td CLASS='text-left' $stn> $pcl </td>
			  <td CLASS='text-right' $stn> $tle </td>
			  <td $stn><div align='center'> $lodo </td>
			  <td class='hidden-print' $stn><div align='center'><button type='button' class='btn btn-xs btn-warning hidden-print' style='height:18px; padding:0px; margin:3px;'>&nbsp;&nbsp;<i class='lnr lnr-pencil'></i>&nbsp;&nbsp;</button></td></tr>");
				$n++;       
						}
			$unlo=$fo-$tlo;
				
	
    
    echo"<tr><th colspan='9' class='text-center'> Total Vehicles: $fo &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Unloaded Vehicles: $unlo &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Loaded Vehicles: $tlo </th><th class='hidden-print'> </th></tr></table>
    <div class='clearfix'></div>
            </div>
        </div>";
        
                $t++;
  //  }
					}
					else{
	echo"<div class='table-responsive'>
<table class='table table-striped table-hover table-sm' style='font-size:8px;'>
	<thead><tr role='row'><th width='4px'> &nbsp;No&nbsp;</th>
		<th class='text-center' width='8%'> Date </th>
		<th> Customer </th><th> Vehicles </th>
    <th> Destination </th><th> Location </th><th> Final </th>
    <th> Load&nbsp;Size </th><th> Distance </th>
    <th><div align='center'> Unloaded </th>
   <th><div align='center'> Loaded </th>
  <th class='hidden-print' style='width:20px; text-align:center;'> # </th></tr>
                    </thead><tbody>";
		
					$n=1;					$tot=$tlo=$tun=0;
		while($ro=mysqli_fetch_assoc($doi)){
$code=$ro['Number'];
$pla=$ro['Vehi'];
$loce=$ro['Location'];
$dicha=$ro['Final'];
$dese=$ro['Destination'];
$desc=$ro['Description'];
$capa=$ro['Capacity'];									
$capao=number_format($capa, 2);
$caps=number_format($capa);
$dista=$ro['Distance'];									
$distao=number_format($dista, 2);
$dists=number_format($dista);
$bulk=$ro['Bulk'];
$cusi=$ro['Custo'];
    $tco=0;
    $deloc="$dese / $loce - $dicha";

$d='KM';
$c='TON';

$etd=$ro['ETD'];
$eta=$ro['ETA'];

if($cusi=='WAITING CARGO')
$stl="style='padding:1px; font-size:12px; color:blue;'";
else
$stl="style='padding:1px; font-size:12px;'";

if($fo=='1')
	$ttl="";
else
	$ttl="title='$desc &nbsp; $bulk'";
	
    $dte=$ro['Date'];
    
    if($cusi=='WAITING CARGO')
        $dte=$Date;
        
         $sete=mysqli_query($conn, "SELECT `Loaded` FROM `vehicles` WHERE `Tripo`='$code' AND `Status`='0'");
                $fete=mysqli_num_rows($sete);
                
                $lodo=$fete;
                $unlo=$pla-$lodo;
	   
print("<tr $ttl data-toggle='tooltip' data-placement='top'>
<td $stl><div align='center'> $n&nbsp;</td>
<td $stl><div align='center'>$dte</td>
<td $stl>$cusi</td><td $stl><center>$pla</td><td $stl> $dese </td>
<td $stl> $loce </td><td $stl>&nbsp;&nbsp;$dicha </td>
	<td $stl><div align='right'>&nbsp;$capao&nbsp;$c&nbsp;</td>
    <td $stl><div align='right'>&nbsp;$distao&nbsp;$d&nbsp;</td>
	<td $stl><div align='center'>&nbsp;$unlo&nbsp;</td>
	<td $stl><div align='center'>&nbsp;$lodo&nbsp;</td>				  
			<form method=post action=''>
	<td class='hidden-print' align='right' style='width:20px; padding:0px;'>
              <input type='hidden' name='bulk' value='$bulk'>
              <input type='hidden' name='custo' value='$cusi'>
              <input type='hidden' name='deloc' value='$deloc'>
    <button type='submit' name='open' class='btn btn-xs btn-success hidden-print' style='height:18px; padding:0px; margin:3px;'>&nbsp;&nbsp;<i class='lnr lnr-download'></i>&nbsp;&nbsp;</button></td></form><tr>");
     $n++;           $tot+=$pla;         $tlo+=$lodo;         $tun+=$unlo;
		}
		
		echo"<tr><th colspan='3' style='padding-left:40px;'> Total Vehicles </th><th class='text-center'>$tot</th><th colspan='5'> </th><th class='text-center'> $tun </th><th class='text-center'> $tlo </th><th> </th></tr></table>
		
		</div><br>";
					    
					}
		?>
	
            
            </div></div><span class="pull-right text-right">&nbsp;&nbsp;&nbsp;&nbsp;<a href="#" onclick="return window.print()" class="hidden-print" title='Print' data-toggle='tooltip' data-placement='top'><i class="lnr lnr-printer"></i></a>&nbsp;&nbsp;&nbsp;</span>
             <?php
             if($t){
                 ?>
              <span class="pull-right hidden-print">
		&nbsp;&nbsp;&nbsp;&nbsp;<a href="#" onclick=window.location.href='data.php' class="hidden-print" title='Export to excell' data-toggle='tooltip' data-placement='top'><i class="lnr lnr-line-spacing"></i></a></span>
		        <?php
             }
             ?>
                  </div>
      
   </div></div></div>  
   <?php
   include'footer.php';
   ?>
