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

if ($_SERVER["REQUEST_METHOD"] === "POST") {
    $name = $_POST["name"];
    $email = $_POST["email"];
    $phone = $_POST["phone"];
    // $service = $_POST["service"];
    // $desc = $_POST["message"];

    // Prepare the email message to recipients
    $to = "offplan@gfsrealestate.ae";
    $subject = "VIOLET";
    $message = "Name: $name\n";
    $message .= "Email: $email\n";
    $message .= "Phone: $phone\n";
    // $message .= "Service: $service\n";
    // $message .= "Message: $desc\n";

    $headers = "From: $name\r\n";
    $headers .= "Reply-To: $email\r\n";
    $headers .= "Content-Type: text/plain; charset=UTF-8\r\n";
    
    // Flag to check if the email was sent successfully
    $emailSent = mail($to, $subject, $message, $headers);
    
    // Prepare the HTML email template for the user
    $userSubject = "Your enquiry has been received.";
    $userMessage = "
    <html>
    <body style='padding: 20px; margin: 0;'>

    <table class='card' style='max-width: 750px; margin: 0 auto; border: 1px solid #ddd; width: 100%;'>
        <tr>
            <td class='card-head' style='background: #F9F7FC; padding: 10px;'>
                <table width='100%'>
                    <tr>
                        <td style='vertical-align: middle; padding-right: 10px;'>
                            <img src='https://designworktesting.com/projects/violet/assets/images/logo.png' alt='' style='display: block; max-width: 100%; margin: auto; height: auto;'>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
        <tr>
            <td class='card-body' style='padding: 30px;'>
                <table class='card-body-main' style='background: #f9f7fc; padding: 15px; width: 100%;'>
                    <tr>
                        <td style='text-align: center;'>
                            <img src='https://designworktesting.com/projects/violet/assets/images/card-img.png' alt='' style='object-fit: cover; margin-top: -60px; display: block; width: 100%; height: auto;'>
                        </td>
                    </tr>
                    <tr>
                        <td style='text-align: center;'>
                            <h4 style='font-size: 27px; margin: 10px 0; color: #626262;'>Thank you very much for your Inquiry</h4>
                            <p style='color: #626262; font-size: 18px;'>One of our property consultants will contact you shortly.</p>
                            <div class='yours' style='text-align: center; margin-top: 19px;'>
                                <p style='margin: 0; color: #626262; font-size: 18px;'>Yours sincerely,</p>
                                <span style='font-size: 18px; font-weight: 600;'>Team GFS Real Estate</span><br>
                                <span style='font-size: 16px; color: #626262;'>+97 155 265 6958</span><br>
                                <span style='font-size: 16px; color: #626262;'><a href='mailto:offplan@gfsrealestate.ae'>offplan@gfsrealestate.ae</a></span>
                            </div>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>

</body>
    </html>";

    $userHeaders = "From: Violet\r\n"; // Replace with your email address
    $userHeaders .= "Reply-To: no-reply@violet.com\r\n"; // Replace with your email address
    $userHeaders .= "Content-Type: text/html; charset=UTF-8\r\n"; // Set content type to HTML

    $userEmailSent = mail($email, $userSubject, $userMessage, $userHeaders);
    
    // Return JSON response based on whether both emails were sent successfully
    if ($emailSent && $userEmailSent) {
        echo json_encode(['status' => 'success']);
    } else {
        echo json_encode(['status' => 'error']);
    }
}
?>
