<?php
if(basename($_SERVER['PHP_SELF']) == 'mainsto.php') 
  $nv=" class='current'";
include'header.php';
include'connection.php';
$client='';
$in=$lt='';
$custo='';
$conde='';
$p=0;

if(isset($_POST['delo']))
		{
			$rowid=$_POST['rowid'];
			$custo=$_POST['custo'];
			$then=mysqli_query($conn, "UPDATE `vehicles` SET `Status`='1' WHERE `Number`='$rowid' LIMIT 1");
		}

        if(isset($_POST['search']))
		{
			$custo=$_POST['custo'];
			$p=1;
		}
		
		if(isset($_GET['client']))
		{
			$client=$_GET['client'];
			$_SESSION['Client']=$client;
		}
		
		if(isset($_POST['client']))
		{
			$client=$_POST['client'];
			$_SESSION['Client']=$client;
		}

		if($custo){
		if($custo=='ALL' OR $custo=='All' OR $custo=='all' OR $custo=='*')
			$conde="";
		else
			$conde="AND (`Plate` LIKE '%$custo%' OR `Trailer` LIKE '%$custo%' OR `Driver` LIKE '%$custo%')";
		}
			
	$dest=$_SESSION['Client'];
	
	    if($dest=='1'){
	        $condi="AND `Trip`='1'";
	        $in="selected";
	    }
	    elseif($dest=='2'){
	        $condi="AND `Trip`='0'";
	        $lt="selected";
	    }
	    else
	        $condi="";
	    

if($p)
$do=mysqli_query($conn, "SELECT *FROM `vehicles` WHERE `Status`='0' $conde $condi ORDER BY `Number` ASC LIMIT 1000");
else
$do=mysqli_query($conn, "SELECT *FROM `vehicles` WHERE `Status`='0' AND `Trip`='1' ORDER BY RAND() LIMIT 15");
$fo=mysqli_num_rows($do);
?>

<script>
    
function tableToExcel(tableID, filename = 'excel_data') {
      // Get the table
      var table = document.getElementById(tableID);

      // Convert table to a workbook
      var workbook = XLSX.utils.table_to_book(table, { sheet: "Sheet1" });

      // Download the Excel file
      XLSX.writeFile(workbook, filename + ".xlsx");
    }
  </script>
  
  <script src="style/xlsx.full.min.js"></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;Allocate 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 active">
	  <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;Additional Services
                </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>
                    
           
           
       
        <div class="col-lg-10">
                  <div class="row">
           
         <div class="col-lg-1" style="width:40px;"> </div><div class="col-lg-6">
         <form action="crete.php" method="post" class="form-horizontal" enctype="multipart/form-data"> 
           <div class="col-lg-3 hidden-print" title="Create New Vehicle" data-toggle='tooltip' data-placement='top'> <button class="btn btn-success btn-block" type="submit"> Create Vehicle </button> </div></form>
           
         <form action="tools.php" method="post"> 
           <div class="col-lg-3 hidden-print" title="Set Tools&Materials" data-toggle='tooltip' data-placement='top'> <button class="btn btn-warning btn-block" type="submit"> Set Tools </button> </div></form> 
           
         <form action="notes.php" method="post"> 
           <div class="col-lg-3 hidden-print" title="Set Notification" data-toggle='tooltip' data-placement='top'> <button class="btn btn-info btn-block" type="submit"> Set Notifications </button> </div></form>
           
          <form action="scrap.php" method="post"> 
           <div class="col-lg-3 hidden-print" title="Scrapped & Junked Vehicles" data-toggle='tooltip' data-placement='top'> <button class="btn btn-warning btn-block" type="submit"> Scrapped&Junked </button> </div></form>
           
           </div><div class="col-lg-5">
               <div class="col-lg-3"> </div>

        <form action="" method="post" class="form-horizontal ">                  
                       
            <div class="col-lg-6 hidden-print"> 
      <input class="form-control"  name="custo" type="text" id="searchu" autofocus="autofocus" title="Search for plate or driver name" data-toggle="tooltip" data-placement="top">
			</div>                      
                       
                       <div class="col-lg-3 hidden-xs hidden-print" title="Search for plate or driver name" data-toggle="tooltip" data-placement="top">
                        <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> &nbsp;&nbsp; Total Records Found : <b><?php echo" $fo " ?></b></span>
			  <span class="pull-right hidden-print">
			 &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>
			 
			 <span class="pull-right hidden-print">
		&nbsp;&nbsp;&nbsp;&nbsp;<a href="#" onclick="tableToExcel('excel-table', 'List of Vehicles')" class="hidden-print" title='Export to excell' data-toggle='tooltip' data-placement='top'><i class="lnr lnr-line-spacing"></i></a></span>
		
		<span class="pull-right hidden-print" style="font-size:10px; color:red;">For a list of all vehicles, type "<b>ALL</b>" or <b>*</b> and click on search&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>
            <div class="widget-container fluid-height clearfix">
            <div class="widget-content padded clearfix">
               
            <table class="table table-striped table-hover" id="excel-table">     <thead><tr role="row">
                     <th class="hidden-xs"> No </th>
                        <th><div align='center'> TRUCK&nbsp;ID </th>
                        <th><div align='center'> TRAILER&nbsp;ID </th>
                       <th><div align='center'> MAKE </th>
                       <th><div align='center'> CHASSIS TRUCK </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-xs hidden-print" style="width:20px; text-align:center;" colspan='2'> OPTIONS </th>
                     </tr>
                    </thead>
                                        <tbody>
					<?php
					$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'];

			$fuel=$ro['Fuel'];
			$vid=$ro['Plate'];
			$source=$ro['Driver'];
			$tre=$ro['Trailer'];

		$seti=mysqli_query($conn, "SELECT `Number` FROM `trips` WHERE `Vehicle`='$code' ORDER BY `Number` DESC LIMIT 1");
			    $reti=mysqli_fetch_assoc($seti);
			    $trino=$reti['Number'];
			
		$sete=mysqli_query($conn, "SELECT `Idno`, `Contact1`, `Contact2` FROM `employees` WHERE `Fullname`='$source' AND `Fullname`!='' AND `Status`='0' 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="";
			
	$then=mysqli_query($conn, "UPDATE `income` SET `Plate`='$vid' WHERE `Vehicle`='$code' AND `Plate`='' ORDER BY `Number` ASC LIMIT 100");
	
	$then=mysqli_query($conn, "UPDATE `repair` SET `Plate`='$vid' WHERE `Vehicle`='$code' AND `Plate`='' ORDER BY `Number` ASC LIMIT 100");
	
	$then=mysqli_query($conn, "UPDATE `consumption` SET `Plate`='$vid' WHERE `Vehicle`='$code' AND `Plate`='' ORDER BY `Number` ASC LIMIT 100");
	
	$then=mysqli_query($conn, "UPDATE `vehicles` SET `Tripo`='$trino' WHERE `Number`='$code' AND `Plate`='$vid' ORDER BY `Number` ASC LIMIT 1");

		 if($_SESSION['Cancel']){
			 $dbutn='submit';
			 $disa='';
		 }
		 else{
			 $dbutn='button';
			 $disa='disabled';
		 }

		 if($ro['Receive']=='0')
			 $stat="<label style='background-color:#ffff00; padding:2px 15px 2px 15px; margin:0px; width:110px; text-align:center;'>WAITING</label>";
		 else{
			 if($ro['Status']=='0')
				 $stat="<label style='background-color:#00cc99; padding:2px 15px 2px 15px; margin:0px; width:110px; text-align:center; margin:0px;'>AVAILABLE</label>";
			 if($ro['Status']=='1')
				 $stat="<label style='background-color:#ff3366; padding:2px 15px 2px 15px; margin:0px; width:110px; text-align:center;'>DELETED</label>";
			 if($ro['Status']=='2'){
				 $stat="<label style='background-color:#ffcc66; padding:2px 15px 2px 15px; margin:0px; width:110px; text-align:center;'>SOLD&nbsp;OUT</label>";
				 $edo="button";
			 }
		 }
		 if($ro['Status']=='1')
				 $stat="<label style='background-color:#ff3366; padding:2px 15px 2px 15px; margin:0px; width:110px; text-align:center;'>DELETED</label>";

                if($fete)
						$stn="style='padding:1px;'";
				else
						$stn="style='padding:1px; color:blue;'";
		 
		print("<tr><td class='hidden-xs' $stn><div align='right'>$n&nbsp;&nbsp;</td><td $stn><div align='center'> $vid </td>
				<td $stn><div align='center'> $tre </td><td $stn> $make </td>
			  <td $stn> $mode </td><td $stn> $source </td>
			  <td class='text-center' $stn> <font color='#ffffff'>'</font>$idno </td><td $stn><div align='left'>$pcl </td><td $stn><div align='center'> <font color='#ffffff'>'</font>$tle </td>
						   <form method=post action='crete.php'><td class='hidden-xs hidden-print' align='right' style='width:20px; padding:0px;'>
                              <input type='hidden' name='rowid' value='$code'>
                          <button type='$edo' class='btn btn-xs btn-warning hidden-print' name='open' style='height:26px; padding:0px; margin:0px;' title='Edit' data-toggle='tooltip' data-placement='top'>
						  &nbsp;&nbsp;<i class='lnr lnr-pencil'></i>&nbsp;&nbsp;</button></td></form>
						  
						  <form method=post action=''>
						  <td class='hidden-xs hidden-print' align='right' style='width:20px; padding:0px; padding-right:5px;'>");


		echo"<div class='modal fade' id='exampleModal$n' tabindex='-1' role='dialog' aria-labelledby='exampleModalLabel' aria-hidden='true' 
		style='top:120px;'>
  <div class='modal-dialog' role='document'>
    <div class='modal-content'>
      <div class='modal-header'>
        <h5 class='modal-title' id='exampleModalLabel'>DELETE CONFIRMATION 
		<label style='float:right; text-align:right;'><b> $make $mode $vid </b></label></h5>

      </div>
      <div class='modal-body' style='height:40px;'>
        <h5 style='color:#ff0033'>Are you sure you want to delete this item?</h5>
      </div><form method='post' action=''><input type='hidden' name='rowid' value='$code'><input type='hidden' name='custo' value='$custo'>
      <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='$dbutn' name='delo' class='btn btn-sm btn-danger'>YES</button>
      </div></form>
    </div>
  </div>
</div>";
						
						
						
						
						
		echo("<div title='Delete' data-toggle='tooltip' data-placement='top'>
                    <input type='hidden' name='rowid' value='$code'><input type='hidden' name='custo' value='$custo'>
                          <button type='button' class='btn btn-xs btn-danger hidden-print' style='height:26px; padding:0px; margin:0px;' data-placement='top'
						  data-toggle='modal' data-target='#exampleModal$n' $disa>
						  &nbsp;&nbsp;<i class='lnr lnr-trash'></i>&nbsp;&nbsp;</button></div></td></form></tr>");
							$n++;
						}
		?>
						
                    </tbody>
                  </table></div>
                  
                  </div></div>

					 <div class="col-md-12 hidden-xs hidden-print">
                
                
            </div></div>
                  </div>
                  
                  
			  <span class="pull-right hidden-print">
			 &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>
			 
			 <span class="pull-right hidden-print">
		&nbsp;&nbsp;&nbsp;&nbsp;<a href="#" onclick="tableToExcel('excel-table', 'List of Vehicles')" class="hidden-print" title='Export to excell' data-toggle='tooltip' data-placement='top'><i class="lnr lnr-line-spacing"></i></a></span>
      
   </div></div></div>  
   <?php
   include'footer.php';
   ?>
