"use client"; import { useState } from "react"; import { useTranslation } from "react-i18next"; import { motion, AnimatePresence } from "framer-motion"; import toast, { Toaster } from "react-hot-toast"; import { useRouter } from "next/navigation"; import AuthService from "../../services/AuthService"; import InteractiveBackground from "../Animation/Background" import { BackLink, HeaderForm } from "./HeaderLogin"; import HeaderAnimation, { containerVariants, itemVariants } from "../Animation/HeaderLoginAnimation"; import TabsLogin from "./Tabs"; import LoginForm from "./LoginForm"; import CreateNewAccount from "./CreateNewAccount"; import PolicyAndPrivecy from "./PolicyAndPrivecy"; import OtpComponent from "./OtpComponent"; export default function Login() { const router = useRouter(); const { t } = useTranslation(); const [step, setStep] = useState("login"); const [loginMethod, setLoginMethod] = useState("email"); const [isLoading, setIsLoading] = useState(false); const [isSuccess, setIsSuccess] = useState(false); const [formData, setFormData] = useState({ credential: "", //email or phoneNumber password: "", rememberMe: false, }); const [errors, setErrors] = useState({}); const validateForm = () => { const newErrors = {}; if (!formData.credential) { newErrors.credential = loginMethod === "email" ? t("emailRequired") : t("phoneRequired"); } if (!formData.password) { newErrors.password = t("passwordRequired"); } setErrors(newErrors); return Object.keys(newErrors).length === 0; }; const handleLogin = async (e) => { e.preventDefault(); if (!validateForm()) return; setIsLoading(true); setErrors({}); try { const response = await AuthService.login({ loginMethod, credential: formData.credential, password: formData.password, }); switch (response.type) { case "SUCCESS": toast.success(t("loginSuccess")); router.push("/"); break; case "OTP_REQUIRED": toast(t("otpRequired")); setStep("otp"); break; default: toast.error("Login failed the email or password is wrong "); } } catch (err) { console.error("[Login] Error:", err); toast.error(err.message || t("connectionError"), { style: { background: "#fee2e2", color: "#991b1b" }, }); } finally { setIsLoading(false); } }; // Auto-detect login method from input const handleCredentialChange = (value) => { setFormData({ ...formData, credential: value }); if (errors.credential) setErrors({ ...errors, credential: null }); }; const handelTabs = (type) => { setLoginMethod(type); setFormData({ ...formData, credential: "" }); }; return (