<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Google reCAPTCHA Secret Key
    $secretKey = "6LdCtAMrAAAAAA7JPtx1Zb8mLxOxphnk4UER7UcY"; // Replace with your Secret Key
    $responseKey = $_POST["g-recaptcha-response"];
    $userIP = $_SERVER["REMOTE_ADDR"];

    // Verify reCAPTCHA response with Google
    $url = "https://www.google.com/recaptcha/api/siteverify?secret=$secretKey&response=$responseKey&remoteip=$userIP";
    $response = file_get_contents($url);
    $response = json_decode($response);

    //if (!$response->success) {
    //    echo "<script>alert('reCAPTCHA verification failed. Please try again.'); window.history.back();</script>";
    //    exit;
    //}

    // Sanitize and validate inputs
    $firstName = trim($_POST['firstName']);
    $email = trim($_POST['email']);
    $phone = trim($_POST['phone']);
    $message = trim($_POST['message']);

    if (empty($firstName) || empty($email) || empty($phone) || empty($message)) {
        echo "<script>alert('All fields are required!'); window.history.back();</script>";
        exit;
    }

    // Validate email format
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        echo "<script>alert('Invalid email format!'); window.history.back();</script>";
        exit;
    }

    // Recipient Email
    $to = "admin@ombrellacompany.com"; // Change this to your email dinesh@timelogica.com  admin@ombrellacompany.com    collaborations@chiacai.com

    // Email Subject
    $subject = "New Inquiry from Ombrella Company site - $firstName";

    // Email Content
    $emailContent = "
    <html>
    <head>
        <title>New Inquiry</title>
    </head>
    <body>
        <h2>New Inquiry Details</h2>
        <p><strong>Name:</strong> $firstName</p>
        <p><strong>Email:</strong> $email</p>
        <p><strong>Phone:</strong> $phone</p>
        <p><strong>Message:</strong> $message</p>
    </body>
    </html>
    ";

    // Email Headers
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
    $headers .= "From: noreply@yourdomain.com" . "\r\n"; // Change this to your email
    $headers .= "Reply-To: $email" . "\r\n";

    // Send Email
    if (mail($to, $subject, $emailContent, $headers)) {
        echo "<script>alert('Message Sent Successfully!'); window.location.href='index.php';</script>";
    } else {
        echo "<script>alert('Error sending message. Please try again later.'); window.history.back();</script>";
    }
}
?>
