<?php

require_once '../phpmailer/vendor/autoload.php';   

if (isset($_POST['send-message'])) {

    $recaptchaSecret = '6Lcxg74qAAAAAFSZoDLfNr_lNfzJhG2bXX0HYnz0'; // Use the Secret Key for reCAPTCHA v2
    $recaptchaResponse = $_POST['g-recaptcha-response']; // reCAPTCHA response from form
    $userIP = $_SERVER['REMOTE_ADDR']; // User's IP address

    // Send a POST request to Google to verify reCAPTCHA response
    $response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=$recaptchaSecret&response=$recaptchaResponse&remoteip=$userIP");
    $responseKeys = json_decode($response);

    // Check if reCAPTCHA was successfully validated
    if (intval($responseKeys->success) !== 1) {
        echo "Please complete the CAPTCHA verification.";
    } else {

        $name = $_POST['invest-name'];
        $phone = $_POST['invest-phone'];
        $email = $_POST['invest-email'];
        // $property = $_POST['invest-property'];
        // $message = $_POST['invest-budget'];

        $to = "hello@crowncrestrealty.com";
        $subject = "Invest With Us Enquiry Data";

        $output = "
        <html>
        <head>
        <title>Send Messages</title>
        </head>
        <body>
        <p>Send Messages!</p>
        <table border='1'>
        <tr>
        <th>Name</th>
        <th>Phone</th>
        <th>Email</th>
        // <th>Property Type</th>
        // <th>Budget</th>
        </tr>
        <tr>
        <td>$name</td>
        <td>$phone</td>
        <td>$email</td>
        // <td>$property</td>
        <td>$message</td>
        </tr>
        </table>
        </body>
        </html>
        ";

        $mail = new PHPMailer\PHPMailer\PHPMailer(true);

        try {
            $mail->isSMTP();  
            $mail->Host = 'smtp.gmail.com';  
            $mail->SMTPAuth = true;
            $mail->Username = 'hello@crowncrestrealty.com'; 
            $mail->Password = 'vqviewgdjarydtli';
            $mail->SMTPSecure = PHPMailer\PHPMailer\PHPMailer::ENCRYPTION_STARTTLS;  
            $mail->Port = 587;

            $mail->SMTPDebug = 0;  
            $mail->Debugoutput = 'html'; 

            $mail->setFrom('hello@crowncrestrealty.com', 'Crown Crest Realty');  
            $mail->addAddress($to);  
            $mail->addCC('inquiry@crowncrestrealty.com');  

            $mail->isHTML(true);  
            $mail->Subject = $subject;   
            $mail->Body    = $output;

            if ($mail->send()) {
                // echo "<p style='color: green;'>Your message has been sent successfully! You will be redirected shortly.</p>";
                // Redirect after 2 seconds
               echo "<script>setTimeout(function(){ window.location = 'https://crowncrestrealty.com/thank-you'; }, 2000);</script>";
            } else {
                echo "<p style='color: red;'>Sorry, there was an error sending your message. Please try again later.</p>";
            }
        } catch (PHPMailer\PHPMailer\Exception $e) {
            // Handle exception if needed
            echo "<p style='color: red;'>Message could not be sent.</p>";
        }

    }
}
?>
