<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php';

header('Content-Type: application/json');

if (
    $_SERVER["REQUEST_METHOD"] === "POST" &&
    !empty($_POST['name']) &&
    !empty($_POST['phone']) &&
    !empty($_POST['destination']) &&
    !empty($_POST['package']) &&
    !empty($_POST['trip_date']) &&
    !empty($_POST['persons']) &&
    !empty($_POST['message'])
) {

    // Sanitize inputs
    $name        = htmlspecialchars(strip_tags($_POST['name']));
    $phone       = htmlspecialchars(strip_tags($_POST['phone']));
    $destination = htmlspecialchars(strip_tags($_POST['destination']));
    $package     = htmlspecialchars(strip_tags($_POST['package']));
    $trip_date   = htmlspecialchars(strip_tags($_POST['trip_date']));
    $persons     = htmlspecialchars(strip_tags($_POST['persons']));
    $message     = htmlspecialchars(strip_tags($_POST['message']));

    $mail = new PHPMailer(true);

    try {

        /* ================= SMTP CONFIG ================= */
        $mail->isSMTP();
        $mail->Host       = 'mail.clouddreamstech.in';
        $mail->SMTPAuth   = true;
        $mail->Username   = 'info@clouddreamstech.in';
        $mail->Password   = 'y&Zvy.r2BFX8EQTn';
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; // SSL
        $mail->Port       = 465;

        // SSL options (recommended for shared hosting)
        $mail->SMTPOptions = [
            'ssl' => [
                'verify_peer'       => false,
                'verify_peer_name'  => false,
                'allow_self_signed' => true,
            ],
        ];
        /* ================================================= */

        // Sender & Receiver
        $mail->setFrom('info@clouddreamstech.in', 'MR.Taxi');
        $mail->addAddress('gokulnath@clouddreams.in', 'Website Booking');

        // Email content
        $mail->isHTML(true);
        $mail->Subject = "New Trip Booking Request - $name";

        $mail->Body = "
<!DOCTYPE html>
<html>
<head>
<meta charset='UTF-8'>
</head>
<body style='margin:0; padding:0; background:#f2f4f7; font-family: Arial, Helvetica, sans-serif;'>

<table width='100%' cellpadding='0' cellspacing='0' style='background:#f2f4f7; padding:30px 0;'>
<tr>
<td align='center'>

<table width='600' cellpadding='0' cellspacing='0' style='background:#ffffff; border-radius:8px; overflow:hidden;'>

    <!-- Header -->
    <tr>
        <td style='background:#2E7D32; padding:18px; text-align:center; color:#ffffff;'>
            <h2 style='margin:0; font-size:20px;'>New Booking Request</h2>
            <p style='margin:5px 0 0; font-size:13px;'>Buildintec Website</p>
        </td>
    </tr>

    <!-- Content -->
    <tr>
        <td style='padding:25px;'>

            <table width='100%' cellpadding='8' cellspacing='0' style='border-collapse:collapse;'>

                <tr>
                    <td width='35%' style='font-weight:bold; color:#333;'>Name</td>
                    <td style='color:#555;'>$name</td>
                </tr>

                <tr style='background:#f9f9f9;'>
                    <td style='font-weight:bold;'>Phone</td>
                    <td>$phone</td>
                </tr>

                <tr>
                    <td style='font-weight:bold;'>Destination</td>
                    <td>$destination</td>
                </tr>

                <tr style='background:#f9f9f9;'>
                    <td style='font-weight:bold;'>Package</td>
                    <td>$package</td>
                </tr>

                <tr>
                    <td style='font-weight:bold;'>Trip Date</td>
                    <td>$trip_date</td>
                </tr>

                <tr style='background:#f9f9f9;'>
                    <td style='font-weight:bold;'>No. of Persons</td>
                    <td>$persons</td>
                </tr>

            </table>

            <!-- Message -->
            <div style='margin-top:20px;'>
                <p style='font-weight:bold; margin-bottom:8px;'>Message</p>
                <div style='background:#f5f7fa; padding:12px; border-radius:6px; color:#555;'>
                    $message
                </div>
            </div>

        </td>
    </tr>

    <!-- Footer -->
    <tr>
        <td style='background:#f1f1f1; padding:15px; text-align:center; font-size:12px; color:#777;'>
            This enquiry was submitted through the Buildintec website.
        </td>
    </tr>

</table>

</td>
</tr>
</table>

</body>
</html>
";


        $mail->send();

        echo json_encode([
            "status" => "success",
            "message" => "Your booking request has been sent successfully."
        ]);
        exit;
    } catch (Exception $e) {
        echo json_encode([
            "status" => "failed",
            "message" => "Mailer Error: " . $mail->ErrorInfo
        ]);
        exit;
    }
} else {
    echo json_encode([
        "status" => "failed",
        "message" => "Please fill all required fields."
    ]);
    exit;
}
