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

require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';

// Set response header to JSON
header('Content-Type: application/json');

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Get json payload if sent via fetch
    $json = file_get_contents('php://input');
    $data = json_decode($json, true);

    if (!$data) {
        $data = $_POST; // fallback to regular post
    }

    $name = isset($data['name']) ? htmlspecialchars($data['name']) : 'Not Provided';
    $email = isset($data['email']) ? htmlspecialchars($data['email']) : 'Not Provided';
    $phone = isset($data['phone']) ? htmlspecialchars($data['phone']) : 'Not Provided';
    $from = isset($data['from']) ? htmlspecialchars($data['from']) : 'Not Provided';
    $to = isset($data['to']) ? htmlspecialchars($data['to']) : 'Not Provided';
    $vehicle = isset($data['vehicle']) ? htmlspecialchars($data['vehicle']) : 'Not Provided';
    $passengers = isset($data['passengers']) ? htmlspecialchars($data['passengers']) : 'Not Provided';
    $luggage_type = isset($data['luggage_type']) ? htmlspecialchars($data['luggage_type']) : 'Not Provided';
    $luggage_count = isset($data['luggage_count']) ? htmlspecialchars($data['luggage_count']) : 'Not Provided';
    $date = isset($data['date']) ? htmlspecialchars($data['date']) : 'Not Provided';
    $time = isset($data['time']) ? htmlspecialchars($data['time']) : 'Not Provided';
    $ampm = isset($data['ampm']) ? htmlspecialchars($data['ampm']) : 'Not Provided';

    $mail = new PHPMailer(true);

    try {
        // Server settings
        $mail->isSMTP();
        $mail->Host       = 'mail.perthtaxi.com.au';
        $mail->SMTPAuth   = true;
        $mail->Username   = 'info@perthtaxi.com.au';
        $mail->Password   = '7b9mqm2r3c';
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; // Enable implicit TLS encryption
        $mail->Port       = 465;

        // Recipients
        $mail->setFrom('info@perthtaxi.com.au', 'Perth Taxi Website');
        $mail->addAddress('info@perthtaxi.com.au'); // Send to self
        // $mail->addReplyTo($email, $name); // Optional: add user's email as reply to

        // Content
        $mail->isHTML(true);
        $mail->Subject = 'New Taxi Reservation from ' . $name;
        
        $emailBody = "
            <h2>New Taxi Reservation</h2>
            <table border='1' cellpadding='10' cellspacing='0' style='border-collapse: collapse;'>
                <tr><td><strong>Name</strong></td><td>{$name}</td></tr>
                <tr><td><strong>Email</strong></td><td>{$email}</td></tr>
                <tr><td><strong>Phone</strong></td><td>{$phone}</td></tr>
                <tr><td><strong>From Destination</strong></td><td>{$from}</td></tr>
                <tr><td><strong>To Destination</strong></td><td>{$to}</td></tr>
                <tr><td><strong>Vehicle Type</strong></td><td>{$vehicle}</td></tr>
                <tr><td><strong>Number of Passengers</strong></td><td>{$passengers}</td></tr>
                <tr><td><strong>Luggage Option</strong></td><td>{$luggage_type}</td></tr>
                <tr><td><strong>Number of Luggage</strong></td><td>{$luggage_count}</td></tr>
                <tr><td><strong>Pickup Date</strong></td><td>{$date}</td></tr>
                <tr><td><strong>Pickup Time</strong></td><td>{$time} {$ampm}</td></tr>
            </table>
            <br><p>This message was sent securely from the booking form on your website.</p>
        ";

        $mail->Body    = $emailBody;
        $mail->AltBody = strip_tags(str_replace(['<br>', '</tr>'], ["\n", "\n"], $emailBody));

        $mail->send();
        
        echo json_encode(['status' => 'success', 'message' => 'Message has been sent']);
    } catch (Exception $e) {
        echo json_encode(['status' => 'error', 'message' => "Message could not be sent. Mailer Error: {$mail->ErrorInfo}"]);
    }
} else {
    echo json_encode(['status' => 'error', 'message' => 'Invalid request method.']);
}
?>
