<?php
include("../includes/header.php");
error_reporting(E_ALL);
date_default_timezone_set("America/Detroit");
echo("<h1>Ernest's Trip Cost Estimator — Calculations Demo</h1>");
if ($_SERVER["REQUEST_METHOD"] != "POST") {
?>
<form method="POST" action="trip_calculator.php">
<p>Distance (miles): <input type="number" step="any" name="distance" required></p>
<p>Fuel Efficiency (MPG): <input type="number" step="any" name="mpg" required></p>
<p>Gas Price ($/gallon): <input type="number" step="any" name="gas_price" required></p>
<input type="submit" value="Calculate Trip Cost">
</form>
<?php
} else {
echo("<h2>Trip Summary</h2>");
$distance = isset($_POST['distance']) ? floatval($_POST['distance']) : 0;
$mpg = isset($_POST['mpg']) ? floatval($_POST['mpg']) : 0;
$gas_price = isset($_POST['gas_price']) ? floatval($_POST['gas_price']) : 0;
if ($mpg <= 0) {
echo("<p style='color:red;'><b>Error:</b> Fuel efficiency must be greater than zero.</p>");
} else {
$gallons_needed = $distance / $mpg;
$total_cost = $gallons_needed * $gas_price;
$co2_emissions = $gallons_needed * 19.6; // lbs of CO₂ per gallon
echo("<p><b>Distance:</b> {$distance} miles</p>");
echo("<p><b>Fuel Efficiency:</b> {$mpg} MPG</p>");
echo("<p><b>Gas Price:</b> $" . number_format($gas_price, 2) . " per gallon</p>");
echo("<p><b>Gallons Needed:</b> " . number_format($gallons_needed, 2) . "</p>");
echo("<p><b>Total Cost:</b> $" . number_format($total_cost, 2) . "</p>");
echo("<p><b>CO₂ Emissions:</b> " . number_format($co2_emissions, 1) . " lbs</p>");
}
echo("<p><a href='trip_calculator.php'>Calculate Another Trip</a></p>");
}
include("../includes/footer.php");
?>