<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = htmlspecialchars(trim($_POST['nameContact']));
    $email = htmlspecialchars(trim($_POST['emailContact']));
    $message = htmlspecialchars(trim($_POST['messageContact']));
    $recaptchaResponse = $_POST['g-recaptcha-response'];

    // Your reCAPTCHA Secret Key
    $secretKey = "6LdUXFIqAAAAAJR3bnIXi2UUS05LKS7FyB7xxi1G";

    // Verify the reCAPTCHA response
    $recaptchaUrl = "https://www.google.com/recaptcha/api/siteverify";
    $recaptchaData = array(
        'secret' => $secretKey,
        'response' => $recaptchaResponse,
        'remoteip' => $_SERVER['REMOTE_ADDR']
    );

    // Use cURL to send the request to Google's reCAPTCHA API
    $ch = curl_init($recaptchaUrl);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $recaptchaData);
    $recaptchaVerifyResponse = curl_exec($ch);
    curl_close($ch);

    $recaptchaResult = json_decode($recaptchaVerifyResponse);

    if ($recaptchaResult->success) {
        // Validate other form fields
        if (!empty($name) && !empty($email) && !empty($message)) {
            // $to = "naveen.mandal1@gmail.com";  // Replace with your email address
            // $subject = "Contact Form Submission from $name";
            // $body = "Name: $name\nEmail: $email\nMessage:\n$message";
            // $headers = "From: $email";

            // if (mail($to, $subject, $body, $headers)) {
            $successMessage = "Your message has been sent successfully!";
            // } 
            // else {
            //     $errorMessage = "Failed to send your message. Please try again later.";
            // }
        } else {
            $errorMessage = "Please fill in all fields.";
        }
    } else {
        $errorMessage = "reCAPTCHA verification failed. Please try again.";
    }
}
?>

<!DOCTYPE html>
<html lang=en>

<head>
    <meta charset=utf-8 />
    <title>Naveen Mandal - Full Stack Developer | Contact</title>
    <meta http-equiv=X-UA-Compatible content="IE=edge">
    <meta name=viewport content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name=author content="Naveen Mandal - Full Stack Developer" />
    <meta name=description content="Naveen Mandal - Full Stack Developer" />
    <link rel=apple-touch-icon sizes=144x144 href=assets/images/favicons/apple-touch-icon-144x144.png>
    <link rel=apple-touch-icon sizes=114x114 href=assets/images/favicons/apple-touch-icon-114x114.png>
    <link rel=apple-touch-icon sizes=72x72 href=assets/images/favicons/apple-touch-icon-72x72.png>
    <link rel=apple-touch-icon sizes=57x57 href=assets/images/favicons/apple-touch-icon-57x57.png>
    <link rel="shortcut icon" href=assets/images/favicons/favicon.png type=image/png>
    <link rel=stylesheet type=text/css href="assets/styles/style.css" />
    <style>
        .g-recaptcha {
            margin-bottom: 50px;
        }
    </style>
    <script src="https://www.google.com/recaptcha/api.js" async defer></script>
</head>

<body class=bg-triangles-2>
    <?php include_once('header.php'); ?>
    <div class=content data-simplebar>
        <div class="section mt-0">
            <h1 class="title title--h1 title__separate">Contact</h1>
        </div>
        <h1 class="title title--h2">Write a Message</h1>
        <form id=contact-form method=post action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>">
            <div class=row>
                <div class="form-group col-12 col-md-6">
                    <i class="font-icon icon-user"></i>
                    <input type=text class="input input__icon form-control" id=nameContact name=nameContact placeholder="Full name" required=required autocomplete=on oninvalid="setCustomValidity('Fill in the field')" onkeyup="setCustomValidity('')">
                    <div class="help-block with-errors"></div>
                </div>
                <div class="form-group col-12 col-md-6">
                    <i class="font-icon icon-at"></i>
                    <input type=email class="input input__icon form-control" id=emailContact name=emailContact placeholder="Email address" required=required autocomplete=on oninvalid="setCustomValidity('Email is incorrect')" onkeyup="setCustomValidity('')">
                    <div class="help-block with-errors"></div>
                </div>
                <div class="form-group col-12 col-md-12">
                    <textarea class="textarea form-control" id=messageContact name=messageContact placeholder="Your Message" rows=4 required=required oninvalid="setCustomValidity('Fill in the field')" onkeyup="setCustomValidity('')"></textarea>
                    <div class="help-block with-errors"></div>
                </div>
                <div class="form-group col-12">
                    <div class="g-recaptcha" data-sitekey="6LdUXFIqAAAAAPMCiAbEZMwFh1USQ404dxj3TfM_"></div>
                </div>

            </div>
            <div class=row>
                <div class="col-12 col-md-6 order-1 order-md-2 text-right">
                    <button type=submit class=btn><i class="font-icon icon-send"></i> Send Message</button>
                </div>
            </div>
        </form>
    </div>
    <?php include_once('footer.php'); ?>
</body>

</html>