<?php
include("db.php");

?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
    <title>Driver Logbook</title>
    <link rel="stylesheet" href="assets/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="assets/fonts/font-awesome.min.css">
    <link rel="stylesheet" href="assets/css/Registration-Form-with-Photo.css">
    <link rel="stylesheet" href="assets/css/styles.css">
</head>
<div class="login-clean" style="height: 760px;background-image: url(&quot;assets/img/star-sky.jpg&quot;);">
  <div class="form-container" style="padding-top:60px">
  <div class="card" style=" max-width:100%; width:100%; margin:auto; border-radius:20px;">
  <div class="card-header">
    <?php
    $name = mysqli_query($conn, "SELECT car from cars where car='$_POST[car]'");
    $get = mysqli_fetch_array($name);
    ?>
    <h5 class="mb-0" style="float:left;"><?php echo $get['car']?>'Profile</h5>
  </div>
  <div class="card-body" style="overflow-y:scroll; overflow-x:hidden;">
    <table class="table table-striped" >
      <form action="print.php" method="POST">
        <input type="hidden" name="car" value="<?php echo $get['car']?>">
      <tr>
      <?php
    if(isset($_POST['filter'])){
        $start = $_POST['start'];
        $end = $_POST['end'];
        $cost = mysqli_query($conn, "SELECT * FROM trips WHERE car='$_POST[car]' AND start_mileage  BETWEEN '$start' and '$end' ORDER BY id DESC ");

          $mileage = array();
        while($row = mysqli_fetch_array($cost)){
          $mileage[] = $row['end_mileage'] - $row['start_mileage'] ;

    }?>
      <input type="hidden" name="start" value="<?php echo $start?>">
      <input type="hidden" name="start" value="<?php echo $end?>">

        <input type="hidden" name="total_distance" value="<?php print_r(array_sum($mileage));?>">
        <th>Date</th><th>Username</th><th>Client</th><th>Number</th><th>Start Mileage</th><th>End Mileage</th><th>Start time</th><th>End Time</th><th>Driving Time</th><th>Distance</th><th>Fuel Cost</th><th>Literage</th><th>Cost/trip</th><th>Account</th>
      </tr>
    <?php
        $result = mysqli_query($conn, "SELECT * FROM trips WHERE car='$_POST[car]'  AND start_mileage BETWEEN '$start' and '$end' ORDER BY start_mileage ASC  ");

          while($row = mysqli_fetch_array($result)){
            $distance = $row['end_mileage'] - $row['start_mileage'];


    ?>
    <div style="overflow-y:scroll; overflow-x:hidden;">
      <tr>
        <td style="width:150px;"><?php echo $row['date'];?></td>
        <input type="hidden" name="date" value="<?php echo $row['date'];?>">
        <td><?php echo $row['username'];?></td>
        <input type="hidden" name="username" value="<?php echo $row['username'];?>">
        <td style="width:170px"><?php echo $row['client'];?></td>
        <input type="hidden" name="client" value="<?php echo $row['client'];?>">
        <td><?php echo $row['job'];?></td>
        <input type="hidden" name="job" value="<?php echo $row['job'];?>">
        <td><?php

         echo $row['start_mileage'];?>
         <input type="hidden" name="start_mileage" value="<?php $row['station_mileage']?>">
      </td>
        <td><?php
        if($row['station_mileage'] > 0){
       echo "<p style='color:red; font-weight:bold'>".$row['station_mileage']."</p>";?>
       <input type="hidden" name="start_mileage" value="<? $row['station_mileage']?>">
       <?php
     }else{
        echo $row['end_mileage'];?>
        <input type="hidden" name="end_mileage" value="<?php echo $row['end_mileage'];?>">
      <?php }?></td>
        <td><?php echo $row['start_time']?></td>
        <input type="hidden" name="start_time" value="<?php echo $row['start_time']?>">
        <td><?php echo $row['end_time2']?></td>
        <input type="hidden" name="end_time" value="<?php echo $row['end_time2']?>">
        <?php $dt = (strtotime($row['end_time']) - strtotime($row['start_time']))/60 + (strtotime($row['end_time2']) - strtotime($row['start_time2']))/60;?>
        <td><?php echo sprintf("%d hr/s<br>%02d min", floor($dt/60), $dt%60);?></td>
        <input type="hidden" name="driving_time" value="<?php echo $row['end_mileage'];?>">
        <td><?php if($row['station_mileage'] > 0){echo $row['station_mileage'] - $row['start_mileage'];}else{ echo $distance;}?></td>
        <input type="hidden" name="distance" value="<?php echo $distance;?>">
        <?php
          $total_cost = mysqli_query($conn, "SELECT DISTINCT amount from trips WHERE car='$_POST[car]' AND start_mileage BETWEEN '$start' and '$end' AND amount > 0");
          $get = mysqli_fetch_array($total_cost);
        ?>
        <td><?php echo $row['amount'];?></td>
        <input type="hidden" name="account" value="<?php echo $get['amount'];?>">
        <td><?php echo $row['litres'];?></td>
        <td><?php
        $cost = $get['amount'];
        $cost_per_trip =  ($cost*($row['end_mileage'] - $row['start_mileage'])/(array_sum($mileage)));
        echo number_format((float)$cost_per_trip,2,'.','');
        $tot_cost[] = ($row['end_mileage'] - $row['start_mileage'])/(array_sum($mileage))*$cost;

        ?>

      </td><td><?php echo $row['expense_account']?></td>
        <input type="hidden" name="cost_per_trip" value="<?php echo number_format((float)$cost_per_trip,2,'.','');?>">
        <?php
     }} ?>
      </tr>
          <td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td><h4><?php print_r(array_sum($mileage));
          ?></h4></td><td><td></td></td><td><h4><?php
          $sum_ = array_sum($tot_cost);
        //  $tota = print_r($sum_);

          $tott = number_format($sum_,2,'.','');
          echo $tott?></h4></td><td></td>
          <input type="submit" name="submit">

    </form>
</div>

    </table>


  </div>

</div>
</body>

</html>
