import React, { useState } from 'react' import { CheckCircle2Icon, MailIcon, MapPinIcon, PhoneIcon, SendIcon } from 'lucide-react' import Swal from 'sweetalert2' import { Reveal } from './Reveal' import { Button } from './Button' import { useSite } from '../api' import { useLang } from '../i18n/lang' import { useFieldSaver } from '../admin/AdminContext' import { InlineEdit } from './admin/InlineEdit' import { SingletonPill } from '../admin/Editable' import { API_URL } from '../config' import { track } from '../analytics' export function ContactSection() { const { brand, contact_section: content } = useSite() const { t } = useLang() const save = useFieldSaver() const [form, setForm] = useState({ name: '', phone: '', note: '', website: '' }) const [sent, setSent] = useState(false) const [sending, setSending] = useState(false) const saveContent = (field: string, value: string) => save('contact-section', content.id, { [field]: value }, (d) => { ;(d.contact_section as unknown as Record)[field] = value }) const saveBrand = (field: string, value: string) => save('brand', brand.id, { [field]: value }, (d) => { ;(d.brand as unknown as Record)[field] = value }) const showErrorAlert = (title: string, text: string) => { Swal.fire({ icon: 'warning', title: title, text: text, background: '#1e1a16', color: '#F5F3EF', iconColor: '#d5874c', confirmButtonText: t('understood'), buttonsStyling: false, customClass: { container: 'backdrop-blur-md bg-ink/70', popup: 'font-pinar border border-white/10 rounded-2xl shadow-2xl backdrop-blur-xl bg-char/95', title: 'font-pinar font-bold text-lg text-ivory pt-2', htmlContainer: 'font-pinar font-light text-sm leading-8 text-ivory/70', confirmButton: 'mt-6 rounded-full bg-copper text-char font-bold px-10 py-3 transition-all duration-300 ease-lux hover:scale-105 hover:shadow-[0_0_20px_rgba(197,143,92,0.5)]', }, showClass: { popup: 'swal2-show', backdrop: 'swal2-backdrop-show', }, hideClass: { popup: 'swal2-hide', backdrop: 'swal2-backdrop-hide', }, }) } const submit = async (e: React.FormEvent) => { e.preventDefault() if (sending) return const nameValue = form.name.trim() const phoneValue = form.phone.trim() if (nameValue.length < 3) { showErrorAlert(t('incompleteInfo'), t('nameRequired')) return } const phoneRegex = /^[\d۰-۹\+\-\s]{8,15}$/ if (!phoneValue || !phoneRegex.test(phoneValue)) { showErrorAlert(t('phoneInvalidTitle'), t('phoneInvalidText')) return } setSending(true) try { const res = await fetch(`${API_URL}/api/contact`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name: nameValue, phone: phoneValue, note: form.note.trim(), website: form.website }), }) if (!res.ok) { const data = await res.json().catch(() => null) const detail = typeof data?.detail === 'string' ? data.detail : t('formCheck') showErrorAlert(t('submitErrorTitle'), detail) return } track('contact-submit') setSent(true) } catch { showErrorAlert(t('connectionErrorTitle'), t('connectionErrorText')) } finally { setSending(false) } } return (
{/* باکس اطلاعات تماس */}

saveContent('label', v)} />

saveContent('title', v)} />

saveContent('lead', v)} />

{/* باکس فرم ارسال درخواست */}
{sent ? (

saveContent('success_title', v)} />

saveContent('success_text', v)} />

) : (
{/* honeypot — خارج از دید؛ فقط ربات‌ها آن را پر می‌کنند */} setForm({ ...form, website: e.target.value })} tabIndex={-1} autoComplete="off" aria-hidden="true" className="pointer-events-none absolute -left-[9999px] h-0 w-0 opacity-0" />
setForm({ ...form, name: e.target.value })} placeholder={content.name_placeholder} className="mt-3 w-full rounded-2xl border border-white/10 bg-ink/60 px-5 py-3.5 text-sm font-light text-ivory placeholder:text-ivory/35 backdrop-blur-md transition-all duration-300 shadow-[inset_0_2px_4px_rgba(0,0,0,0.3)] focus:border-copper/70 focus:bg-ink/80 focus:shadow-[0_0_15px_rgba(197,143,92,0.25)] focus:outline-none" />
setForm({ ...form, phone: e.target.value })} placeholder={content.phone_placeholder} className="mt-3 w-full rounded-2xl border border-white/10 bg-ink/60 px-5 py-3.5 text-sm font-light ltr:text-left rtl:text-right text-ivory placeholder:text-ivory/35 backdrop-blur-md transition-all duration-300 shadow-[inset_0_2px_4px_rgba(0,0,0,0.3)] focus:border-copper/70 focus:bg-ink/80 focus:shadow-[0_0_15px_rgba(197,143,92,0.25)] focus:outline-none" />