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


?>
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <link rel="apple-touch-icon" sizes="76x76" href="../assets/img/apple-icon.png">
  <link rel="icon" type="image/png" href="./assets/img/favicon.png">
  <title>
   Dicomm Driver Logbook
  </title>
  <!--     Fonts and icons     -->
  <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700" rel="stylesheet" />
  <!-- Nucleo Icons -->
  <link href="./assets/css/nucleo-icons.css" rel="stylesheet" />
  <link href="./assets/css/nucleo-svg.css" rel="stylesheet" />
  <!-- Font Awesome Icons -->
  <script src="https://kit.fontawesome.com/42d5adcbca.js" crossorigin="anonymous"></script>
  <link href="./assets/css/nucleo-svg.css" rel="stylesheet" />
  <!-- CSS Files -->
  <link id="pagestyle" href="./assets/css/soft-ui-dashboard.css?v=1.0.1" rel="stylesheet" />
  <script src="https://kit.fontawesome.com/06d73363bc.js" crossorigin="anonymous"></script>
 
</head>

<body class="g-sidenav-show  bg-gray-100">

  <main class="main-content mt-1 border-radius-lg">
    <!-- Navbar -->
   
        
      </div>
    </nav>
    <!-- End Navbar -->
   <div class="container-fluid "> 
   <div class="page-header min-height-200 border-radius-xl mt-4" style="background-image: url('./img/background.jpg'); background-size:cover;">
       
      </div>
      <div class="card card-body blur shadow-blur mx-4 mt-n6">
        <div class="row gx-4">
          

    <?php
        $query = mysqli_query($conn, "SELECT * FROM cars WHERE car = '$_POST[car]' ");
        $details = mysqli_fetch_array($query);

        $getid = mysqli_query($conn, "SELECT * FROM trips WHERE car = '$_POST[car]'");
        $id = mysqli_fetch_array($getid);
    ?>

    <div class="col-12 " style="height:50px;text-align:center;align-items:center">
    <form action="filter-page.php" method="POST">
    <input style="height:40px;" type="text" name="start" placeholder="Station mileage">
          <input style="height:40px;" type="text" name="end" placeholder="Start mileage">
          <input type="hidden" name="car" value="<?php echo $_POST['car']?>">
          <input type="hidden" name="id" value="<?php echo $id['id']?>">
            <input type="submit" class="btn btn-primary" name="filter" value="Filter">
            <input type="submit" formaction="print.php" onclick="$('form').attr('target', '_blank');" class="btn btn-success" name="filter" value="Filter as PDF">
            <input type="submit" formaction="update-trips.php" class="btn btn-info" name="update" value="Expense accounts">
          </form >
<form action="update-trips.php" method="POST">
  <input type="hidden" name="car" value="<?php echo $_POST['car']?>">
  
</form>

          </div>
          
        </div>
      </div>
      </div>
  
    </div><br><br>

    <div class="card-body px-0 pt-0 pb-2">
              <div class="table-responsive p-0">
                <table style="width:100%" class="table align-items-center mb-0">
                  <thead>
                    <tr>
                    <th class="text-uppercase text-secondary text-xxs font-weight-bolder opacity-7 ps-2">Date</th>  
                   
                    <th class="text-uppercase text-secondary text-xxs font-weight-bolder opacity-7 ps-2">Driver</th>                  
                      <th class="text-uppercase text-secondary text-xxs font-weight-bolder opacity-7 ps-2">Job No.</th>                
                      <th class="text-uppercase text-secondary text-xxs font-weight-bolder opacity-7 ps-2">Start Mileage</th>
                      <th class="text-uppercase text-secondary text-xxs font-weight-bolder opacity-7 ps-2">End Mileage</th>
                     
                      <th class="text-uppercase text-secondary text-xxs font-weight-bolder opacity-7 ps-2">Fueling</th>
                      <th class="text-uppercase text-secondary text-xxs font-weight-bolder opacity-7 ps-2">Distance</th>
                      <th class="text-uppercase text-secondary text-xxs font-weight-bolder opacity-7 ps-2">Expense account</th>
                      <th class="text-uppercase text-secondary text-xxs font-weight-bolder opacity-7 ps-2">Client</th> 
                      <th class="text-uppercase text-secondary text-xxs font-weight-bolder opacity-7 ps-2">Destination</th>   
                    </tr>
                  </thead>
                  <tbody>
                  <?php  $result = mysqli_query($conn, "SELECT * FROM trips WHERE car='$_POST[car]'ORDER BY id DESC");
    while($row = mysqli_fetch_array($result)){
    $mileage = $row['end_mileage'] - $row['start_mileage'];?>
                    <tr>
                    <td>
                       <form action="update-trip.php" method="POST">
                         <input type="hidden" name="car" value="<?php echo $row['car']?>">
                         <input type="hidden" name="id" value="<?php echo $row['id']?>">
                         <input style="height:30px" class="btn btn-danger" type="submit" name="update" value="<?php echo $row['date']?>">
                       </form>
                        
                      </td>
                     
                      
                      <td>
                        <p class="text-xs font-weight-bold mb-0 " ><?php echo $row['username']?></p>
                        
                      </td>
                   
                      <td>
                        <p class="text-xs font-weight-bold mb-0" ><?php echo $row['job']?></p>
                        
                      </td>
                     
                      
                      <td>
                        <p class="text-xs font-weight-bold mb-0" style="text-align:center"><?php echo $row['start_mileage']?></p>
                        
                      </td>
                      <td>
                        <p class="text-xs font-weight-bold mb-0" style="text-align:center"><?php echo $row['end_mileage']?></p>
                        
                      </td>
                      
                      <td>
                        <p class="text-xs font-weight-bold mb-0" style="text-align:center"><span style="font-weight:bold; color:red;">
                        <?php
                          if($row['station_mileage'] > 0){
                            echo $row['station_mileage'];
                          }
                        ?>
                      </span></p>
                        
                      </td>
                      <td>
                        <p class="text-xs font-weight-bold mb-0" style="text-align:center"><?php echo $mileage?></p>
                        
                      </td>
                     
                     <td><p class="text-xs font-weight-bold mb-0" style="text-align:center"><?php if(!empty($row['expense_account'])){
                      echo $row['expense_account'];}else{
                        ?><input style="width:80px;" type="text" name="expense_account"><input type="hidden" name="id" value="<?php echo $row['id']?>"><?php
                      } ?></p></td>
                     
                      <td >
                        <p class=" col-1 text-xs font-weight-bold mb-0 " ><?php echo $row['client']?></p>
                        
                      </td>
                      <td>
                        <p class="text-xs font-weight-bold mb-0 " ><?php echo $row['destination']?></p>
                        
                      </td>
                    </tr>
                        <?php } ?>

        </div>
  </main>
  
</body>

