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

if(isset($_POST['update'])){
$expense = $_POST['expense_account'];
$update = mysqli_query($conn, "UPDATE trips SET expense_account='$expense' WHERE id='$_POST[id]' AND car='$_POST[car]' ORDER BY id DESC ");
}
?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Admin Area | Dashboard</title>
    <!-- Bootstrap core CSS -->
    <link href="../assets/css/bootstrap.min.css" rel="stylesheet">
    <link href="../assets/css/style.css" rel="stylesheet">
    <script src="http://cdn.ckeditor.com/4.6.1/standard/ckeditor.js"></script>

    <nav class="navbar navbar-default">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
            <span class="sr-only"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="#">Driver LogBook</a>
        </div>
        <!--/.nav-collapse -->
      </div>
    </nav>

    <header id="header">
      <div class="container">
        <div class="row">
          <div class="col-md-10">
            <h1><span class="glyphicon glyphicon-cog" aria-hidden="true"></span>Driver LogBook</h1>
          </div>

        </div>
      </div>
    </header>

    <section id="breadcrumb">
      <div class="container">
        <ol class="breadcrumb">
          <li class="active">Keep track of every trip taken the smart way</li>
        </ol>
      </div>
    </section>

    <section id="main">
      <div class="container">
        <div class="row">

          <div style="width:1310px;margin-left:-6%;"class="col-md-9">
            <div class="panel panel-default">
              <div class="panel-heading main-color-bg">
                <?php
                $name = mysqli_query($conn, "SELECT car from cars where car='$_POST[car]'");
                $get = mysqli_fetch_array($name);
                ?>
                <h2 class="panel-title"><?php echo $get['car']?>'Profile</h2>
              </div>
              <div class="panel-body">
                <div style="height:350px; overflow-y:scroll;overflow-x:hidden">
                  <table class="table table-striped table-hover" style="width:100%" >
                    <tr>
                      <th>Date</th><th>Username</th><th>Client</th><th>Job/Employee Number</th><th>Destination</th><th>Description</th><th>Start Mileage</th><th>End Mileage</th><th>Start Time</th><th>End Time</th><th>Start Time2</th><th>End Time2</th><th>Fueling</th><th>Total Mileage</th><th>Expense Account</th>
                    </tr>
                  <?php  $result = mysqli_query($conn, "SELECT * FROM trips WHERE car='$_POST[car]'ORDER BY start_mileage DESC");
                  while($row = mysqli_fetch_array($result)){
                  $mileage = $row['end_mileage'] - $row['start_mileage'];?>
                  <div style="overflow-y:scroll; overflow-x:hidden;">
                    <tr>

                      <td><?php echo $row['date'];?></td>
                      <td><?php echo $row['username'];?></td>
                      <input type="hidden" name="username" value="<?php echo $row['username'];?>">
                      <td><?php echo $row['client'];?></td>
                      <input type="hidden" name="client" value="<?php echo $row['client'];?>">
                      <td><?php echo $row['job'];?></td>
                      <td><?php echo $row['destination']?></td>
                      <td><?php echo $row['description']?></td>
                      <input type="hidden" name="job" value="<?php echo $row['job'];?>">
                      <td><?php echo $row['start_mileage'];?></td>
                      <td><?php echo $row['end_mileage'];?></td>
                      <td><?php echo $row['start_time'];?></td>
                      <input type="hidden" name="s_time" value="<?php echo $row['start_time'];?>">
                      <td><?php echo $row['end_time'];?></td>
                      <input type="hidden" name="e_time" value="<?php echo $row['end_time2'];?>">
                      <td><?php echo $row['start_time2']?></td>
                      <td><?php echo $row['end_time2']?></td>
                      <td><span style="font-weight:bold; color:red;">
                        <?php
                          if($row['station_mileage'] > 0){
                            echo $row['station_mileage'];
                          }
                        ?>
                      </span></td><input type="hidden" name="station_mileage" value="<?php echo $row['station_mileage'];?>">
                      <td><?php echo $mileage;?></td>
                      <td><?php if(!empty($row['expense_account'])){
                      echo $row['expense_account'];}else{
                        ?><form action="filter-page.php" method="POST"><input style="width:80px;" type="text" name="expense_account"><input type="hidden" name="id" value="<?php echo $row['id']?>"><?php
                      } ?></td>
                      <?php
                    }?>
                    </tr>
                  </div>
                  </table>
                </div>
              </div>


            </div>
            <div class="card-footer" style="margin-left:10%;">
            <form action="filter-page.php" method="POST">
            <input type="number" name="start" placeholder="Station Mileage">
            <input type="number" name="end" placeholder="Start Mileage">
            <input type="hidden" name="car" value="<?php echo $get['car']?>">
            <input type="submit" formaction="car-profile.php" class="btn btn-primary" name="update" value="Update">
            <input type="submit" class="btn btn-success" 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">
            </form>
            </div>



            </div>
          </div>

            </div>


          </div>
        </div>
      </div>
    </section>

    <footer id="footer">
      <p>Copyright DICOMM McCANN 2020</p>
    </footer>


  <script>
     CKEDITOR.replace( 'editor1' );
 </script>

    <!-- Bootstrap core JavaScript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script src="js/bootstrap.min.js"></script>


  </body>
</html>
