195 lines
7.7 KiB
TypeScript
195 lines
7.7 KiB
TypeScript
'use client';
|
||
import { useState, useEffect } from 'react';
|
||
import { PhoneCall, X, CheckCircle2, AlertCircle } from 'lucide-react';
|
||
|
||
interface ContactAdvisorButtonProps {
|
||
productId?: string;
|
||
productName?: string;
|
||
hideFloatingButton?: boolean;
|
||
}
|
||
|
||
export default function ContactAdvisorButton({ productId, productName, hideFloatingButton = false }: ContactAdvisorButtonProps) {
|
||
const [isOpen, setIsOpen] = useState(false);
|
||
|
||
useEffect(() => {
|
||
const handler = () => setIsOpen(true);
|
||
window.addEventListener('canina-open-advisor-modal', handler);
|
||
return () => window.removeEventListener('canina-open-advisor-modal', handler);
|
||
}, []);
|
||
const [firstName, setFirstName] = useState('');
|
||
const [lastName, setLastName] = useState('');
|
||
const [phone, setPhone] = useState('');
|
||
const [isSubmitting, setIsSubmitting] = useState(false);
|
||
const [status, setStatus] = useState<{ success: boolean; message: string } | null>(null);
|
||
|
||
const handleSubmit = async (e: React.FormEvent) => {
|
||
e.preventDefault();
|
||
setStatus(null);
|
||
|
||
const cleanPhone = phone.trim().replace(/\s+/g, '');
|
||
if (!/^09\d{9}$/.test(cleanPhone)) {
|
||
setStatus({ success: false, message: 'لطفاً شماره موبایل ۱۱ رقمی معتبر وارد کنید (مثال: ۰۹۱۲۳۴۵۶۷۸۹)' });
|
||
return;
|
||
}
|
||
|
||
try {
|
||
setIsSubmitting(true);
|
||
const res = await fetch(`${process.env.NEXT_PUBLIC_API_URL || '/api'}/public/lead`, {
|
||
method: 'POST',
|
||
headers: { 'Content-Type': 'application/json' },
|
||
body: JSON.stringify({
|
||
firstName: firstName.trim(),
|
||
lastName: lastName.trim(),
|
||
phone: cleanPhone,
|
||
productId: productId || undefined,
|
||
}),
|
||
});
|
||
|
||
const data = await res.json();
|
||
if (res.ok && (data.ok || data.success)) {
|
||
setStatus({
|
||
success: true,
|
||
message: data.message || 'درخواست شما ثبت شد. مشاوران تخصصی ما به زودی با شما تماس خواهند گرفت.',
|
||
});
|
||
setFirstName('');
|
||
setLastName('');
|
||
setPhone('');
|
||
} else {
|
||
setStatus({
|
||
success: false,
|
||
message: data.message || 'خطا در ثبت درخواست. لطفاً دوباره تلاش کنید.',
|
||
});
|
||
}
|
||
} catch {
|
||
setStatus({
|
||
success: false,
|
||
message: 'خطا در برقراری ارتباط با سرور. لطفاً دقایقی دیگر تلاش فرمایید.',
|
||
});
|
||
} finally {
|
||
setIsSubmitting(false);
|
||
}
|
||
};
|
||
|
||
return (
|
||
<>
|
||
{/* Floating Action Button */}
|
||
{!hideFloatingButton && (
|
||
<button
|
||
type="button"
|
||
onClick={() => setIsOpen(true)}
|
||
className="fixed bottom-6 left-6 z-40 flex items-center gap-2.5 rounded-full bg-emerald-600 px-5 py-3.5 text-white font-bold shadow-lg shadow-emerald-600/30 transition-all hover:bg-emerald-700 hover:scale-105 active:scale-95"
|
||
>
|
||
<PhoneCall className="h-5 w-5 animate-pulse" />
|
||
<span>مشاوره و تماس رایگان</span>
|
||
</button>
|
||
)}
|
||
|
||
{/* Modal */}
|
||
{isOpen && (
|
||
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/60 p-4 backdrop-blur-sm">
|
||
<div className="relative w-full max-w-md rounded-2xl bg-white p-6 shadow-2xl font-vazir text-right text-gray-800">
|
||
<button
|
||
onClick={() => {
|
||
setIsOpen(false);
|
||
setStatus(null);
|
||
}}
|
||
className="absolute top-4 left-4 rounded-lg p-1 text-gray-400 hover:bg-gray-100 hover:text-gray-700"
|
||
>
|
||
<X className="h-5 w-5" />
|
||
</button>
|
||
|
||
<div className="flex items-center gap-3 border-b border-gray-100 pb-4">
|
||
<div className="rounded-xl bg-emerald-50 p-2.5 text-emerald-600">
|
||
<PhoneCall className="h-6 w-6" />
|
||
</div>
|
||
<div>
|
||
<h3 className="font-bold text-gray-900 text-lg">درخواست مشاوره تخصصی کنینا</h3>
|
||
<p className="text-xs text-gray-500">
|
||
{productName ? `برای محصول: ${productName}` : 'مشاوره اختصاصی مکملها با دامپزشک'}
|
||
</p>
|
||
</div>
|
||
</div>
|
||
|
||
{status && (
|
||
<div
|
||
className={`mt-4 flex items-start gap-3 rounded-xl p-3.5 text-sm ${
|
||
status.success ? 'bg-emerald-50 text-emerald-800' : 'bg-red-50 text-red-800'
|
||
}`}
|
||
>
|
||
{status.success ? (
|
||
<CheckCircle2 className="h-5 w-5 shrink-0 text-emerald-600 mt-0.5" />
|
||
) : (
|
||
<AlertCircle className="h-5 w-5 shrink-0 text-red-600 mt-0.5" />
|
||
)}
|
||
<span>{status.message}</span>
|
||
</div>
|
||
)}
|
||
|
||
{!status?.success ? (
|
||
<form onSubmit={handleSubmit} className="mt-4 space-y-3.5">
|
||
<div>
|
||
<label className="block text-xs font-bold text-gray-700 mb-1">نام</label>
|
||
<input
|
||
type="text"
|
||
value={firstName}
|
||
onChange={(e) => setFirstName(e.target.value)}
|
||
placeholder="مثال: علی"
|
||
className="w-full rounded-xl border border-gray-200 px-3.5 py-2.5 text-sm focus:border-emerald-600 focus:outline-none"
|
||
required
|
||
/>
|
||
</div>
|
||
|
||
<div>
|
||
<label className="block text-xs font-bold text-gray-700 mb-1">نام خانوادگی</label>
|
||
<input
|
||
type="text"
|
||
value={lastName}
|
||
onChange={(e) => setLastName(e.target.value)}
|
||
placeholder="مثال: محمدی"
|
||
className="w-full rounded-xl border border-gray-200 px-3.5 py-2.5 text-sm focus:border-emerald-600 focus:outline-none"
|
||
required
|
||
/>
|
||
</div>
|
||
|
||
<div>
|
||
<label className="block text-xs font-bold text-gray-700 mb-1">شماره تلفن همراه</label>
|
||
<input
|
||
type="tel"
|
||
dir="ltr"
|
||
value={phone}
|
||
onChange={(e) => setPhone(e.target.value)}
|
||
placeholder="09121234567"
|
||
className="w-full rounded-xl border border-gray-200 px-3.5 py-2.5 text-sm font-mono text-right focus:border-emerald-600 focus:outline-none"
|
||
required
|
||
/>
|
||
<p className="mt-1 text-[11px] text-gray-400">مشاوران ما در کمتر از ۵ دقیقه با شما تماس خواهند گرفت.</p>
|
||
</div>
|
||
|
||
<div className="pt-2">
|
||
<button
|
||
type="submit"
|
||
disabled={isSubmitting}
|
||
className="w-full rounded-xl bg-emerald-600 py-3 text-sm font-bold text-white shadow-md transition-all hover:bg-emerald-700 disabled:opacity-50"
|
||
>
|
||
{isSubmitting ? 'در حال ثبت...' : 'ثبت درخواست تماس'}
|
||
</button>
|
||
</div>
|
||
</form>
|
||
) : (
|
||
<div className="pt-4 text-center">
|
||
<button
|
||
type="button"
|
||
onClick={() => setIsOpen(false)}
|
||
className="rounded-xl bg-gray-100 px-6 py-2.5 text-sm font-bold text-gray-700 hover:bg-gray-200"
|
||
>
|
||
بستن پنجره
|
||
</button>
|
||
</div>
|
||
)}
|
||
</div>
|
||
</div>
|
||
)}
|
||
</>
|
||
);
|
||
}
|