<?php
session_start();
if(isset($_SESSION['User'])){
include("db.php");
$connect = $conn;
$profileUsername = $_POST['username'] ?? $_SESSION['User'];
if(isset($_POST['submit'])){
  $username = $_POST['username'];
  $car = $_POST['car'];
  $client = $_POST['client'];
  if(empty($_POST['job'])){
      $job = $_POST['employeecode'];
  }else{
  $job = $_POST['job'];
  }
  $description = $_POST['description'];
  $destination = $_POST['destination'];
  $starttime = $_POST['start_time'];
  $endtime = $_POST['end_time'];
  $starttime2 = $_POST['start_time2'];
  $endtime2 = $_POST['end_time2'];
  $startmileage = $_POST['start_mileage'];
  $endmileage = $_POST['end_mileage'];
  $station = $_POST['station_mileage'];
  $stationmileage = $_POST['station_mileage'];
  $amount = $_POST['amount'];
  $litres = $_POST['litres'];
  $expense = $_POST['expense_account'];
  $date = date("Y-m-d");
  $empty = '';
  $empty2 = ' 0 ';
  $empty3 = '';
  $empty4 = '';
  $empty5 = '';

  if($stationmileage > 0){
    $result = "INSERT INTO trips(username,car,client,job,description,destination,date,start_time,end_time,start_time2,end_time2,start_mileage,end_mileage,station_mileage,amount,litres,expense_account)
    VALUES('$username','$car','$client','$job','$description','$destination','$date','$starttime','$endtime','$starttime2','$endtime2','$startmileage','$stationmileage','$station','$amount','$litres','$empty');";
    $result2= "INSERT INTO trips(username,car,client,job,description,destination,date,start_time,end_time,start_time2,end_time2,start_mileage,end_mileage,station_mileage,amount,litres,expense_account)
    VALUES('$username','$car','$client','$job','$description','$destination','$date','$starttime','$endtime','$starttime2','$endtime2','$stationmileage','$endmileage','$empty2','$empty3','$empty4','$empty5');";
    if ($conn->multi_query($result) === TRUE) {
    echo "Success";
}else{
    echo "Error: " . $result . "<br>" . $conn->error;
}
echo "<br>";
if ($conn->multi_query($result2) === TRUE) {
echo "Success";
} else {
echo "Error: " . $result2 . "<br>" . $conn->error;
}

  }else{
    $result3 = "INSERT INTO trips(username,car,client,job,description,destination,date,start_time,end_time,start_time2,end_time2,start_mileage,end_mileage,station_mileage,amount,litres,expense_account)
    VALUES('$username','$car','$client','$job','$description','$destination','$date','$starttime','$endtime','$starttime2','$endtime2','$startmileage','$endmileage','$empty2','$empty3','$empty4','$empty5');";
  }
  echo "<br>";
  if ($conn->multi_query($result3) === TRUE) {
  echo "Success";
} else {
  echo "Error: " . $result3 . "<br>" . $conn->error;
}
}
?>

<!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/Login-Form-Clean.css">
    <link rel="stylesheet" href="assets/css/Login-Form-Dark.css">
    <link rel="stylesheet" href="assets/css/Registration-Form-with-Photo.css">
    <link rel="stylesheet" href="assets/css/styles.css">
</head>

<body style="background-image: url(&quot;assets/img/star-sky.jpg&quot;);">
  <div class="form-container" style="padding-top:60px">
  <div class="card" style=" max-width:900px; width:80%; margin:auto; border-radius:20px;height:470px; ">
    <?php  $name = mysqli_query($conn, "SELECT username FROM trips WHERE username='$profileUsername' ");
            $total = mysqli_query($conn, "SELECT (SUM(end_mileage) - SUM(start_mileage)) AS difference FROM trips WHERE username='$profileUsername'");
            $distance = mysqli_fetch_array($total);
             $get = mysqli_fetch_array($name);
             $username = $get['username'] ?? $profileUsername;
             $mileageTotal = $distance['difference'] ?? 0;
    echo "<form action='welcome2.php' method='POST'><div class='card-header'><h5>All Trips for  ".$username."
    <br>Total Mileage travelled :".$mileageTotal."<input type='hidden' value='".$username."' name='username'><input class='btn btn-info pull-right' type='submit' name='home' value='Home'></div></form><br>";
    $result = mysqli_query($conn, "SELECT * FROM trips WHERE username='$profileUsername' ORDER BY id DESC");
    echo"<div style='overflow-y:scroll; overflow-x:hidden;'>";
      while($row = mysqli_fetch_array($result)){
        $mileage = $row['end_mileage'] - $row['start_mileage'];?>
          <input type="hidden" value="<?php $get['username']?>">
      <p style="background:#acafb5; margin:0 auto;padding:10px 10px 10px 20px; border-radius:20px; width:90%;"class="card-text"><span><b><?php echo $row['date'];?></b></span><br>Car Used : <?php echo $row['car']?><br><span><?php echo $row['start_time'] ?>
      </span> to <span><?php echo $row['end_time2'] ?></span><br>Destination Address : <?php echo $row['destination'] ?><br>
      Closing mileage :<?php echo $row['end_mileage'] ?><br>Total Distance travelled : <?php echo $mileage?></p><br>
      <input type="hidden" value="<?php echo $mileage?>">
    <?php } ?>
    <form>
  </div>

  </div>
    <br>

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

</html>
<?php }else{
  header("Location: index.php");
}?>
