"use client" import React, { useEffect, useState } from "react" import { useLanguage } from "@/lib/language-context" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Textarea } from "@/components/ui/textarea" import { Card } from "@/components/ui/card" import { Send, CheckCircle2, AlertCircle } from "lucide-react" declare global { interface Window { grecaptcha?: { ready: (cb: () => void) => void execute: (siteKey: string, options: { action: string }) => Promise } } } export default function ContactForm() { const { t, language } = useLanguage() const siteKey = process.env.NEXT_PUBLIC_RECAPTCHA_SITE_KEY || "6LfqD1osAAAAANd6P-3qin0cRdFQSGX92F02A3dE" const recaptchaAction = "contact_form" const defaultService = t.contactPage.form.serviceOptions.airport const [recaptchaReady, setRecaptchaReady] = useState(false) const [status, setStatus] = useState<"idle" | "loading" | "success" | "error">("idle") const [formData, setFormData] = useState({ name: "", email: "", phone: "", service: defaultService, message: "", consent: false }) useEffect(() => { if (!siteKey) return const scriptId = "recaptcha-v3" const onReady = () => { window.grecaptcha?.ready(() => setRecaptchaReady(true)) } if (document.getElementById(scriptId)) { onReady() } else { const script = document.createElement("script") script.id = scriptId script.src = `https://www.google.com/recaptcha/api.js?render=${siteKey}` script.async = true script.defer = true script.onload = onReady script.onerror = () => setRecaptchaReady(false) document.head.appendChild(script) } // Show captcha badge when on contact page document.body.classList.add("show-captcha") return () => { // Hide captcha badge when leaving contact page document.body.classList.remove("show-captcha") } }, [siteKey]) useEffect(() => { setFormData(prev => ( prev.service === defaultService ? prev : { ...prev, service: defaultService } )) }, [defaultService]) const waitForRecaptcha = (timeoutMs = 4000) => new Promise((resolve, reject) => { const started = Date.now() const tick = () => { if (window.grecaptcha) { window.grecaptcha.ready(() => resolve()) return } if (Date.now() - started >= timeoutMs) { reject(new Error("reCAPTCHA not ready")) return } setTimeout(tick, 150) } tick() }) const getRecaptchaToken = async () => { if (!window.grecaptcha) { throw new Error("reCAPTCHA not ready") } return new Promise((resolve, reject) => { window.grecaptcha?.ready(() => { window.grecaptcha ?.execute(siteKey, { action: recaptchaAction }) .then(resolve) .catch(reject) }) }) } const handleChange = (e: React.ChangeEvent) => { const { name, value, type } = e.target as any setFormData(prev => ({ ...prev, [name]: type === "checkbox" ? (e.target as HTMLInputElement).checked : value })) } const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() if (!recaptchaReady) { try { await waitForRecaptcha() setRecaptchaReady(true) } catch { alert(t.contactPage.form.recaptchaNotReady) return } } if (!formData.consent) { alert(t.contactPage.form.consentRequired) return } setStatus("loading") try { // In a static export environment, this would hit a PHP script on the server // or an external API. Here we assume an API route for local testing. const recaptchaToken = await getRecaptchaToken() const response = await fetch("/contact.php", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ ...formData, recaptchaToken, recaptchaAction, language, }) }) if (response.ok) { setStatus("success") setFormData({ name: "", email: "", phone: "", service: defaultService, message: "", consent: false }) } else { setStatus("error") } } catch (err) { setStatus("error") } } if (status === "success") { return (

{t.contactPage.form.successTitle}

{t.contactPage.form.success}

) } return (
{/* Name */}
{/* Email */}
{/* Phone */}
{/* Service Type */}
{/* Message */}