"use client"; import React, { useState, useEffect, useRef } from "react"; import { motion, AnimatePresence } from "motion/react"; import { X, User, Building2, Heart, ShieldCheck, Phone, Key, LogIn, ArrowRight, RefreshCw } from "lucide-react"; import { useUserStore, UserRole } from "../lib/store/userStore"; import { authService } from "../lib/services/authService"; import { toast } from "sonner"; import { toPersian } from "../lib/utils"; interface AuthModalProps { isOpen: boolean; onClose: () => void; } export default function AuthModal({ isOpen, onClose }: AuthModalProps) { const { setRole, setLoggedIn, fetchProfile } = useUserStore(); const [view, setView] = useState<"quick" | "phone" | "otp">("quick"); const [phoneNumber, setPhoneNumber] = useState(""); const [otpCode, setOtpCode] = useState(""); const [isLoading, setIsLoading] = useState(false); const [countdown, setCountdown] = useState(0); const otpInputRef = useRef(null); useEffect(() => { if (view === "otp") { const timer = setTimeout(() => { otpInputRef.current?.focus(); }, 100); return () => clearTimeout(timer); } }, [view]); // Reset modal state on open/close useEffect(() => { if (!isOpen) { setView("quick"); setPhoneNumber(""); setOtpCode(""); setIsLoading(false); setCountdown(0); } }, [isOpen]); // Cooldown countdown timer useEffect(() => { if (countdown > 0) { const timer = setTimeout(() => setCountdown(countdown - 1), 1000); return () => clearTimeout(timer); } }, [countdown]); const handleQuickLogin = async (targetRole: UserRole) => { if (targetRole === "User_Guest") { setRole("User_Guest"); setLoggedIn(false); toast.success("به عنوان کاربر مهمان وارد شدید."); onClose(); return; } setIsLoading(true); const testPhone = targetRole === "User_PetOwner" ? "09121111111" : "09122222222"; try { // Step 1: Send OTP to test phone const sendRes = await authService.sendOtp(testPhone); const code = (sendRes as any).code; if (!code) { throw new Error("کد تایید تستی تولید نشد"); } // Step 2: Verify OTP const verifyRes = await authService.verifyOtp(testPhone, code); if (verifyRes.success) { // Force state sync and fetch profile setRole(targetRole); await fetchProfile(); toast.success(`ورود سریع موفق! در نقش ${targetRole === "User_Partner" ? 'همکار' : 'صاحب پت'} وارد شدید.`); onClose(); } } catch (err: any) { toast.error(`خطا در ورود سریع: ${err.message}`); } finally { setIsLoading(false); } }; const handleSendOtp = async (e: React.FormEvent) => { e.preventDefault(); const cleanPhone = phoneNumber.trim(); if (!/^09\d{9}$/.test(cleanPhone)) { toast.error("شماره موبایل نامعتبر است"); return; } setIsLoading(true); try { const res = await authService.sendOtp(cleanPhone); if ((res as any).code) { toast.info(`کد تایید (تست): ${(res as any).code}`, { duration: 10000 }); } else { toast.success("کد تایید پیامک شد"); } setView("otp"); setCountdown(120); } catch (err: any) { toast.error(err.message || "خطا در ارسال کد تایید"); } finally { setIsLoading(false); } }; const handleVerifyOtp = async (e: React.FormEvent) => { e.preventDefault(); const cleanCode = otpCode.trim(); if (cleanCode.length !== 5) { toast.error("کد باید ۵ رقم باشد"); return; } setIsLoading(true); try { const response = await authService.verifyOtp(phoneNumber.trim(), cleanCode); if (response.success) { await fetchProfile(); toast.success("ورود موفقیت‌آمیز بود"); onClose(); } } catch (err: any) { toast.error(err.message || "کد تایید نامعتبر است"); } finally { setIsLoading(false); } }; const handleResendOtp = async () => { if (countdown > 0) return; setIsLoading(true); try { await authService.sendOtp(phoneNumber.trim()); toast.success("کد تایید جدید ارسال شد"); setCountdown(120); } catch (err: any) { toast.error(err.message || "خطا در ارسال مجدد کد"); } finally { setIsLoading(false); } }; return ( {isOpen && ( <> {/* Backdrop */} {/* Modal content */} {/* Top highlight bar */}
{/* Close Button */} {/* Title Header */}

ورود به دنیای کانی‌نا

{view === "otp" ? `کد تایید ارسال شده به شماره ${toPersian(phoneNumber)} را وارد کنید` : "برای دسترسی به پرونده سلامت و سفارشات وارد شوید"}

{/* Content Panel */} {view === "quick" && (

ورود سریع تستی (با دیتابیس واقعی)

)} {view === "phone" && (
setPhoneNumber(e.target.value.replace(/[^0-9]/g, ''))} disabled={isLoading} className="w-full bg-medical-gray-50 border border-medical-gray-200 rounded-2xl py-4 pr-12 pl-4 focus:ring-2 focus:ring-canina-blue/20 outline-none font-bold text-lg text-left tracking-widest" />
)} {view === "otp" && (
setOtpCode(e.target.value.replace(/[^0-9]/g, ''))} disabled={isLoading} className="w-full bg-medical-gray-50 border border-medical-gray-200 rounded-2xl py-4 pr-12 pl-4 focus:ring-2 focus:ring-canina-blue/20 outline-none font-black text-2xl text-center tracking-[0.5em]" />
{countdown > 0 ? ( ارسال مجدد کد پس از {toPersian(countdown)} ثانیه ) : ( )}
)}
)} ); }