<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require __DIR__ . '/../vendor/autoload.php';
include('../db_connect.php');


// Collect booking data from form
$car_name     = $_POST['car_name'] ?? '';
$car_model    = $_POST['car_model'] ?? '';
$pickup_date  = $_POST['pickup_date'] ?? '';
$dropoff_date = $_POST['dropoff_date'] ?? '';
$total_days   = $_POST['total_days'] ?? '';
$total_price  = $_POST['total_price'] ?? '';
$first_name   = $_POST['first_name'] ?? '';
$last_name    = $_POST['last_name'] ?? '';
$email        = $_POST['email'] ?? '';
$phone        = $_POST['phone'] ?? '';
$address      = $_POST['address'] ?? '';
$city         = $_POST['city'] ?? '';
$state        = $_POST['state'] ?? '';
$note         = $_POST['note'] ?? '';

if (empty($car_name) || empty($email) || empty($pickup_date)) {
    die("Missing required data");
}

// Save booking details in DB
$query = "INSERT INTO bookings 
(car_name, car_model, pickup_date, dropoff_date, total_days, total_price, first_name, last_name, email, phone, address, city, state, note) 
VALUES 
('$car_name', '$car_model', '$pickup_date', '$dropoff_date', '$total_days', '$total_price', '$first_name', '$last_name', '$email', '$phone', '$address', '$city', '$state', '$note')";

if (mysqli_query($conn, $query)) {

    // Prepare booking summary
    $booking_details = "
        <h3>Booking Confirmation</h3>
        <p><strong>Name:</strong> {$first_name} {$last_name}</p>
        <p><strong>Email:</strong> {$email}</p>
        <p><strong>Phone:</strong> {$phone}</p>
        <p><strong>Car:</strong> {$car_name} ({$car_model})</p>
        <p><strong>Pickup Date:</strong> {$pickup_date}</p>
        <p><strong>Dropoff Date:</strong> {$dropoff_date}</p>
        <p><strong>Total Days:</strong> {$total_days}</p>
        <p><strong>Total Amount:</strong> ₹{$total_price}</p>
        <p><strong>Address:</strong> {$address}, {$city}, {$state}</p>
        <p><strong>Note:</strong> {$note}</p>
        <p><strong>Payment Status:</strong> <span style='color:green;'>Paid</span></p>
    ";

    // --------- ADMIN EMAIL ---------
    $mailAdmin = new PHPMailer(true);
    try {
        $mailAdmin->isSMTP();
        $mailAdmin->Host       = 'smtp.gmail.com';
        $mailAdmin->SMTPAuth   = true;
        $mailAdmin->Username   = 'benjamindanieljr11@gmail.com';   // 🔹 your email
        $mailAdmin->Password   = 'tvvvkzlylfgdmcys';    // 🔹 app password
        $mailAdmin->SMTPSecure = 'tls';
        $mailAdmin->Port       = 587;

        $mailAdmin->setFrom('benjamindanieljr11@gmail.com', 'Car Rental System');
        $mailAdmin->addAddress('benjamindanieljr11@gmail.com'); // Admin email

        $mailAdmin->isHTML(true);
        $mailAdmin->Subject = "New Paid Booking - {$car_name}";
        $mailAdmin->Body    = $booking_details;
        $mailAdmin->send();
    } catch (Exception $e) {
        error_log("Admin mail error: " . $mailAdmin->ErrorInfo);
    }

    // --------- CUSTOMER EMAIL ---------
    $mailUser = new PHPMailer(true);
    try {
        $mailUser->isSMTP();
        $mailUser->Host       = 'smtp.gmail.com';
        $mailUser->SMTPAuth   = true;
        $mailUser->Username   = 'benjamindanieljr11@gmail.com';
        $mailUser->Password   = 'tvvvkzlylfgdmcys';
        $mailUser->SMTPSecure = 'tls';
        $mailUser->Port       = 587;

        $mailUser->setFrom('benjamindanieljr11@gmail.com', 'Car Rental Service');
        $mailUser->addAddress($email, "{$first_name} {$last_name}");

        $mailUser->isHTML(true);
        $mailUser->Subject = "Booking Confirmed - {$car_name}";
        $mailUser->Body    = $booking_details;
        $mailUser->send();
    } catch (Exception $e) {
        error_log("Customer mail error: " . $mailUser->ErrorInfo);
    }

    echo "<script>alert('Booking confirmed and email sent successfully!'); window.location.href='../frontend/thankyou.php';</script>";

} else {
    echo "Database Error: " . mysqli_error($conn);
}
?>
