<html>
<head>
<title>
Calculate Mileage
</title>

<!-- Set the font size -->
<STYLE TYPE="text/css">
TD{font-size: 30px;}
BODY{font-size: 30px;}
</STYLE>
<script>
function myThing(url) {
    window.open(url, "pictureWindow", "width=1600, height=1000");
}
</script>
</head>

<body>
<h1>Your Gas Mileage is:</h1>
<?php

  // Initialize 
  $file = "mileage.csv";

  // Collect the input from the form and
  // Convert US gal. to litres if required
  // Calculate total cost.
  if ($_POST['type'] == 'submit') {
      $odo = $_POST['odo'];
      $litres = $_POST['litres'];
      $costpl = $_POST['cost'];
      $car = $_POST['cars'];

      // Set image and csv for vehicle
      if ($car == "prius") {
          $carImage = "prius.jpg";
          $file = "mileagePrius.csv";
      } else {
          $carImage = "pathfinder.jpg";
          $file = "mileagePathfinder.csv";
      }
      // Is it in gallons?
      if ($litres < 0) {
          $litres = -$litres;
          $litres = $litres * 3.78541;
          $costpl = $costpl / 3.78541;
      }
      $cost = round($litres * $costpl/100, 2);
      $location = $_POST['location'];
      $time = $_POST['time'];

      // Get last odomenter reading and subtract from current.
      $fh = fopen($file, 'r');
      $nl = "*_*_*";
      do {
          $line = $nl;
      } while (($nl = fgets($fh)) != "");
      $lineVals = explode(',', $line);
      $km = $odo - $lineVals[3];

      // Calculate the various values.
      $imp_gallons = $litres / 4.546;
      $miles = $km/1.609;

      $litresPer100Km = round($litres / $km * 100, 2);
      $mpg = round($miles/$imp_gallons, 0);

      // Display the calculations, time and location.
      echo "<img src=$carImage alt=\"\" width=\"300\" height=\"175\"><br>";
      echo "Kilometers = $km<br>";
      echo "Cost = $cost<br>";
      echo "Litres per 100 kilometers = $litresPer100Km <br>";
      echo "Miles per gallon = $mpg <br><br>";
      echo "Cost per kilometer = $" . round($cost/$km, 2) . "<br>";
      echo "Cost per mile = $" . round($cost/$miles, 2) . "<br><br>";
      echo "Time: " . $time . "<br>";
      echo "Location = $location<br>";

      // Save the current information.
      if ($output = fopen ($file, 'a')) {
          $cpk = round($cost/$km, 2);
          $cpm = round($cost/$miles, 2);
          list($lat, $long) = explode(',',$location);
          $url = "maps.google.com/maps?z=18&t=m&q=loc:$lat+$long";
          fputs ($output, "$time,$litres,$costpl,$odo,$km,$cost,$litresPer100Km,$mpg,$cpk,$cpm,$url\n");
          fclose($output);

      // Error - data file could not be opened.
      } else {
          echo "Could not open data file for append.";
      }
  }

    //  Or Display a table of all the data
    if ($_POST['type'] == 'display') {

        // Set image and csv for vehicle
        $car = $_POST['cars'];
        if ($car == "prius") {
            $carImage = "prius.jpg";
            $file = "mileagePrius.csv";
        } else {
            $carImage = "pathfinder.jpg";
            $file = "mileagePathfinder.csv";
        }
        echo "<img src=$carImage alt=\"\" width=\"300\" height=\"175\"><br>";
        if($input = fopen ($file, 'r')) {
            echo "<table border=\"2px\"><b><tr><td>time</td><td>litres</td><td>cost/l</td><td>Odo</td><td>km</td><td>cost</td><td>l/100km</td><td>mpg</td><td>$/km</td><td>$/mile</td><td>map</td></tr></b>";
            while (!feof($input)) {
                $line = fgets($input);
                if ($line) {
                    list($time,$litres,$costpl,$odo,$km, $cost, $litresPer100Km, $mpg, $cpk, $cpm, $url) = explode(',', $line);
                    $url = trim($url);
                    $url = "https://$url";
                    $map = "<button onclick=\"myThing('" . $url . "')\"><img src=\"map.jpeg\"></button>";
                    echo "<tr><td>$time</td><td>$litres</td><td>$costpl</td><td>$odo<td>$km</td><td>$cost</td><td>$litresPer100Km</td><td>$mpg</td><td>$cpk</td><td>$cpm</td><td>$map</td></tr>\n";
                }
            }
            echo "</table>";
        } else {
            echo "Could not open data file for read.";
        }
  }
?>
</body>
</html>