<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php';



if ($_SERVER["REQUEST_METHOD"] === "POST") {
    $tname    = $_POST['tname'] ?? '';
    $name    = $_POST['name'] ?? '';
    $email   = $_POST['email'] ?? '';
    $phone   = $_POST['phone'] ?? '';
    $adult   = $_POST['adult'] ?? '';
    $child   = $_POST['child'] ?? '';
    $checkIn   = $_POST['checkIn'] ?? '';
    $vehicleType   = $_POST['vehicleType'] ?? '';
    $hotelCategory   = $_POST['hotelCategory'] ?? '';
    $msg = $_POST['msg'] ?? '';

    $mail = new PHPMailer(true);

    try {
        // Server settings
        $mail->isSMTP();
        $mail->Host       = 'smtp.gmail.com'; // Change if using another provider
        $mail->SMTPAuth   = true;
        $mail->Username   = 'tripindiabycar@gmail.com';
        $mail->Password   = 'qeitqbetqqcqslyx';
        $mail->SMTPSecure = 'tls';
        $mail->Port       = 587;

        // Recipients
        $mail->setFrom($email, $name);
        $mail->addAddress('tripindiabycar@gmail.com', 'Trip India'); // Receiver email
        $mail->addAddress('tripindiabycarriya@gmail.com', 'Trip India'); // Receiver email
        $mail->addAddress('tripindiabycar.siya@gmail.com', 'Trip India'); // Receiver email

        // Content
        $mail->isHTML(true);
         $mail->Subject = 'New Enquiry from Website';

        // Dynamically build message from available fields
        $body = "<h2>New Enquiry Received</h2><table>";
        foreach ($_POST as $key => $value) {
            if (!empty(trim($value))) {
                $safeKey = ucfirst(htmlspecialchars($key));
                $safeVal = nl2br(htmlspecialchars($value));
                $body .= "<tr><td><strong>{$safeKey}:</strong></td><td>{$safeVal}</td></tr>";
            }
        }
        $body .= "</table>";

        $mail->isHTML(true);
        $mail->Body = $body;

        $mail->send();
        header("location:thanks.php");
        // echo "Enquiry sent successfully.";
    } catch (Exception $e) {
        echo "Error sending email: {$mail->ErrorInfo}";
    }
} else {
    
    echo "Invalid request.";
}
