<?php
if(basename($_SERVER['PHP_SELF']) == 'offloading.php') 
  $nv=" class='current'";
include'header.php';
$custo='';
$pr=0;


    if(isset($_POST['search']))
		{
			$custo=$_POST['custo'];
		}
		
	if(isset($_POST['delo']))
		{
			$custo=$_POST['custo'];
			$code=$_POST['code'];
			$fna=$_POST['fna'];
$doi=mysqli_query($conn, "UPDATE `vehicles` SET `Loading`='0', `Gara`='0' WHERE `Number`='$code' ORDER BY `Number` ASC LIMIT 1");	

$then=mysqli_query($conn, "UPDATE `trips` SET `Offload`='0' WHERE `Vehicle`='$code' ORDER BY `Number` DESC LIMIT 1");
		}
		
		
	if(isset($_POST['savall']))
		{
			$lokete=str_replace("'", "`", $_POST['lokete']);
			$a=$_POST['a'];
			if($lokete=='GARAGE')
			$addo="GARAGE";
			else
			$addo="ALLOCATE";
			
			for($t=1; $t<=$a; $t++){
			$pla=$_POST["plate$t"];
			$fna=$_POST["fna$t"];
	if($pla){
$doi=mysqli_query($conn, "UPDATE `vehicles` SET `Locate`='$lokete', `Custo`='', `Gdate`='$Date', `Gara`='0' 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', '$addo', 'Added vehicle to $lokete')");	
	
	$seco=mysqli_query($conn, "UPDATE `trips` SET `Offload`='1' WHERE `Vehicle`='$pla' AND `Offload`='0'");
		    }
		        }
		}
		
		
		if($custo)
			$conde="AND (`Plate` LIKE '%$custo%' OR `Driver` LIKE '%$custo%' OR `Locate` LIKE '%$custo%')";
		else
		    $conde="AND `Loading`='1'";
		    
		    if(isset($_POST['openi']))
		        {
			    $loko=$_POST['loko'];
			    $conde="AND `Locate`='$loko'";
			    $pr=1;
		        }
?>


<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 );
///////
});


   function check_uncheck_checkbox(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 active">
              <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" 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>	
              
            <li class="list-group-item" style="border:1px dashed #cccccc; border-radius:5px; margin:0px 0px 4px 0px;">
	  <a href="ddlist.php">
                <p>
                <i class="lnr lnr-plus-circle"></i>&nbsp;Mileage Deduction
                </p>
              </a></li>	
  </div>
                    
          <?php
          if($_SESSION['Avt']=='1'){
          // *************** Relocate vehicles **********************
	    echo"<div class='modal fade' id='Modaldis' tabindex='-1' role='dialog' aria-labelledby='exampleModalLabel' aria-hidden='true'>
  <div class='modal-dialog' role='document' style='width:70%;'>
  <div class='modal-content'><div class='modal-header text-left'>
        <h5 class='modal-title' id='exampleModalLabel'>VEHICLES IN PARKING 
        <span class='pull-right'>  </span></h5>

      </div><form action='' method='post'>
      <div class='modal-body text-left'><div class='row'>

    <div class='col-md-12 text-left' style='height:380px; overflow-x:auto;'>";
        
        $a=1;
    $dos=mysqli_query($conn, "SELECT `Number`, `Plate`, `Locate`, `Gara` FROM `vehicles` WHERE `Status`='0' AND `Trip`='1' AND `Loading`='1' AND `Locate`='' AND `Gara`='0' ORDER BY `Plate` ASC");
    if(mysqli_num_rows($dos)){
        echo" &nbsp;&nbsp;&nbsp;&nbsp; <b class='caret'></b> OFFLOADED <hr style='margin-top:0px; margin-bottom:5px;'>";
		while($ros=mysqli_fetch_assoc($dos)){
			$numb=$ros['Number'];
			$plate=$ros['Plate'];
			$bd=$ros['Locate'];
			$bor="";
	echo"<label class='btn btn-default active' for='my$a' style='width:110px; $bor' title='$bd' data-toggle='tooltip' data-placement='top'>
	<input type='hidden' name='fna$a' value='$plate'>
          <input id='my$a' type='checkbox' value='$numb' name='plate$a' style='border:0px; height:12px;'> $plate </label>";
                 $a++;
		}
    }
		
	
    $dos=mysqli_query($conn, "SELECT `Number`, `Plate`, `Locate`, `Gara` FROM `vehicles` WHERE `Status`='0' AND `Trip`='1' AND `Loading`='1' AND `Locate`='' AND `Gara`='1' ORDER BY `Plate` ASC");
     if(mysqli_num_rows($dos)){
        echo"<br><br> &nbsp;&nbsp;&nbsp;&nbsp; <font color='blue'> <b class='caret'></b> FROM GARAGE </font><hr <hr style='margin-top:0px; margin-bottom:5px;'>";
		while($ros=mysqli_fetch_assoc($dos)){
			$numb=$ros['Number'];
			$plate=$ros['Plate'];
			$bd=$ros['Locate'];
			$bor="border:1px solid blue;";
	echo"<label class='btn btn-default active' for='my$a' style='width:110px; $bor' title='$bd' data-toggle='tooltip' data-placement='top'>
	<input type='hidden' name='fna$a' value='$plate'>
          <input id='my$a' type='checkbox' value='$numb' name='plate$a' style='border:0px; height:12px;'> $plate </label>";
                 $a++;
		}
     }
		
		
    $dos=mysqli_query($conn, "SELECT `Number`, `Plate`, `Locate`, `Gara` FROM `vehicles` WHERE `Status`='0' AND `Trip`='1' AND `Loading`='1' AND `Locate`='GARAGE' ORDER BY `Plate` ASC");
     if(mysqli_num_rows($dos)){
        echo"<br><br> &nbsp;&nbsp;&nbsp;&nbsp; <font color='red'> <b class='caret'></b> IN GARAGE </font><hr <hr style='margin-top:0px; margin-bottom:5px;'>";
		while($ros=mysqli_fetch_assoc($dos)){
			$numb=$ros['Number'];
			$plate=$ros['Plate'];
			$bd=$ros['Locate'];
			if($bd)
			$bor="border:1px solid red;";
			elseif($ros['Gara'])
			$bor="border:1px solid blue;";
			else
			$bor="";
	echo"<label class='btn btn-default active' for='my$a' style='width:110px; $bor' title='$bd' data-toggle='tooltip' data-placement='top'>
	<input type='hidden' name='fna$a' value='$plate'>
          <input id='my$a' type='checkbox' value='$numb' name='plate$a' style='border:0px; height:12px;'> $plate </label>";
                 $a++;
		}
     }
    
    echo"</div></div></div><div class='row'> </div>
	    
	   <input type='hidden' name='a' value='$a'>	
	<div class='modal-header' style='padding-top:15px; height:50px;'>
	
	<div class='col-md-4' style='padding-left:40px;'>
	<input type='checkbox' name='checkall' id='checkall' style='height:22px; width:24px; margin-top:0px;' onClick='check_uncheck_checkbox(this.checked);'> &nbsp; <button type='button' class='btn btn-secondary' id='d1'></button></div>
    
    <div class='col-md-4'>
    <select class='form-control' name='lokete' style='height:30px;' required>
    <option value=''> SELECT DESTINATION </option>
    <option value='GARAGE'> SEND TO GARAGE </option>
    <option value='ALLOCATED'> ALLOCATED </option></select></div>
    
        <div class='col-md-4 text-right'>
	<button type='button' class='btn btn-xs btn-danger' data-dismiss='modal' style='width:80px; height:28px;'> CANCEL </button>
        <button type='submit' name='savall' class='btn btn-xs btn-success' style='width:80px; height:28px;'> SAVE </button></div>
        </div></form>
    </div></div>
    </div>";
          }
    ?>
    
        <div class="col-lg-10">
    <div class="row"><div class="col-lg-6" style="padding-left:40px;">
           <div class="col-lg-3 hidden-print"> <button class="btn  btn-warning btn-block" type="button" data-toggle='modal' data-target='#Modaldis'> Allocate </button> </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"> Trip Report </button> </div></form> 
           
         <form action="milerepo.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"> Mileage Report </button> </div></form> 
           
         <form action="status.php" method="post" class="form-horizontal" enctype="multipart/form-data" name='myform'> 
           <div class="col-lg-3 hidden-print"> <button class="btn btn-warning btn-block" type="submit"> Status Report </button> </div></form> 
						
        </div>  
           
           
           
           
        <div class="col-lg-6">   
           <div class="col-lg-3"> </div>
           

        <form action="" method="post" class="form-horizontal ">                  
                       
            <div class="col-lg-5 hidden-xs hidden-print"> 
      <input class="form-control"  name="custo" type="text" id="searchu" autofocus="autofocus">
			</div>                      
                       
                       <div class="col-lg-3 hidden-xs hidden-print">
                        <button class="btn  btn-primary btn-block" type="submit" name="search"><i class="lnr lnr-magnifier"></i> Search</button>
                   
					  </div>
                         
                      
                     
                  
            </form> </div>
             
               
            </div>
             <div class="row">
            <div class="col-lg-12" style='margin-top:-20px;'>
             <span> &nbsp;&nbsp; </span>
             <?php
             if($pr){
                 echo"<span class='pull-right text-right'>
             <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>";
             }
             ?>
            <div class="widget-container fluid-height clearfix">
            <div class="widget-content padded clearfix">
               
                                
					<?php
	            $b=1;
            echo"<div class='panel panel-default' style='border:1px solid powderblue;'><form action='' method='post'>
			<div class='panel-heading' style='background-color:powderblue; padding-left:30px; font-weight:bold;'> VEHICLES IN PARKING
			<input type='hidden' name='loko' value='$loke'>";
			if(!$pr)
		echo"<button type='submit' name='openi' class='btn btn-xs btn-success pull-right hidden-print' style='height:18px; padding:1px 10px 1px 10px; margin:0px; margin-right:30px;'><i class='lnr lnr-download'></i> OPEN </button></div></form>";
		
		echo"<div class='panel-body' style='padding:0px;'>
			 <table class='table table-striped table-hover'>     
            <tr role='row'><th class='hidden-xs'> 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'> COMING&nbsp;FROM </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 class='hidden-print' width='10px'> # </th>
                     </tr><tbody>";

		
	$do=mysqli_query($conn, "SELECT *FROM `vehicles` WHERE `Status`='0' AND `Locate`='' AND `Loading`='1' $conde ORDER BY `Doff` ASC LIMIT 450");	
	            $n=1;				$to=0;
		while($ro=mysqli_fetch_assoc($do)){
			$code=$ro['Number'];
			$make=$ro['Make'];
			$mode=$ro['Chassis1'];
			$chassis=$ro['Chassis2'];
			$year=$ro['Year'];
			$dte=$ro['Doff'];

			$fuel=$ro['Fuel'];
			$vid=$ro['Plate'];
			$source=$ro['Driver'];
			$tre=$ro['Trailer'];
			
	$seti=mysqli_query($conn, "SELECT `Number`, `Location`, `Destination` FROM `trips` WHERE `Vehicle`='$code' ORDER BY `Number` DESC LIMIT 1");
			    $reti=mysqli_fetch_assoc($seti);
			    $trino=$reti['Number'];
			    $deso=$reti['Destination'];
			    $loce=$reti['Location'];
			
		$sete=mysqli_query($conn, "SELECT `Idno`, `Contact1`, `Contact2` FROM `employees` WHERE `Fullname`='$source' AND `Fullname`!='' 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'];
		    }
		    else
		        $tle=$pcl=$idno="";
	
                if($ro['Gara'])
					$stn="style='padding:0px; color:blue;'";
                elseif(!$tle OR !$pcl)
					$stn="style='padding:0px; color:red;'";
				else
					$stn="style='padding:0px;'";
					
	if($_SESSION['Cancel']){
	    echo"<div class='modal fade' id='exampleModal$b' tabindex='-1' role='dialog' aria-labelledby='exampleModalLabel' aria-hidden='true' style='top:220px;'>
  <div class='modal-dialog' role='document'>
    <div class='modal-content'><div class='modal-header'>
        <h5 class='modal-title' id='exampleModalLabel'>REMOVE CONFIRMATION
        <label class='pull-right'> $vid </label></h5>

      </div>
      <div class='modal-body' style='height:40px;'>
        <h5 style='color:#ff0033'>Are you sure you want to remove this vehicle?</h5>
      </div><form method='post' action=''><input type='hidden' name='code' value='$code'><input type='hidden' name='custo' value='$custo'><input type='hidden' name='fna' value='$vid'>
      <div class='modal-footer' style='margin-top:-10px; height:50px; padding-top:5px; border:0px solid blue;'>
        <button type='button' class='btn btn-sm btn-success' data-dismiss='modal'>&nbsp;NO&nbsp;</button>
        <button type='submit' name='delo' class='btn btn-sm btn-danger'>YES</button>
      </div></form>
    </div>
  </div>
</div>";
}
		 
		print("<tr><td class='hidden-xs' $stn><div align='right'>$n&nbsp;&nbsp;</td><td $stn><div align='center'> $dte </td><td $stn><div align='center'> $vid </td><td $stn><div align='center'> $tre </td><td $stn> $deso / $loce </td>
		<td $stn> $source </td><td CLASS='text-center' $stn> $idno </td>
			  <td $stn><div align='left'> $pcl </td>
		<td $stn><div align='right'> $tle </td>
	<td class='hidden-print' align='right' style='width:10px; padding:0px 5px 0px 0px;'><div title='Remove' data-toggle='tooltip' data-placement='top'><button type='button' class='btn btn-xs btn-link text-danger hidden-print' style='height:18px; padding:0px; margin:0px;' data-placement='top' data-toggle='modal' data-target='#exampleModal$b'><i class='lnr lnr-trash'></i></button></div></td></tr>");
							$n++;                   $b++;
						}
			echo" </tbody>
                  </table>
        <div class='clearfix'></div>
            </div>
                
        </div>";
    
		?>
            
            </div></div>
                </div>
                  
            
             <?php
             if($pr){
                 echo"<span class='pull-right text-right'>
             <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;&nbsp; </span>";
             }
             ?>
      
   </div></div></div>  
   <?php
   include'footer.php';
   ?>
