"use client"; import Button from "@/components/atoms/buttons/Button"; import { useAuth } from "@/lib/auth-context"; import { SHOP_INFO } from "@/lib/constants"; import Image from "next/image"; import Link from "next/link"; import { useRouter } from "next/navigation"; import { SubmitEvent, useState } from "react"; // Static OTP for demo (in production, this would be sent via SMS) const DEMO_OTP = "123456"; export default function RegisterPage() { const router = useRouter(); const { completeRegistration } = useAuth(); const [step, setStep] = useState<"phone" | "otp">("phone"); const [phone, setPhone] = useState(""); const [otp, setOtp] = useState(""); const [errors, setErrors] = useState({ phone: "", otp: "" }); // Validate Vietnamese phone number const validatePhone = (phoneNumber: string): boolean => { // Vietnamese phone format: 10 digits starting with 0 // Valid prefixes: 03, 05, 07, 08, 09 const phoneRegex = /^(0[3|5|7|8|9])[0-9]{8}$/; return phoneRegex.test(phoneNumber); }; const handlePhoneSubmit = (e: SubmitEvent) => { e.preventDefault(); if (!phone.trim()) { setErrors({ ...errors, phone: "Vui lòng nhập số điện thoại" }); return; } if (!validatePhone(phone)) { setErrors({ ...errors, phone: "Số điện thoại không hợp lệ (VD: 0987654321)", }); return; } // Move to OTP step setStep("otp"); setErrors({ phone: "", otp: "" }); }; const handleOtpSubmit = (e: SubmitEvent) => { e.preventDefault(); if (!otp.trim()) { setErrors({ ...errors, otp: "Vui lòng nhập mã OTP" }); return; } if (otp !== DEMO_OTP) { setErrors({ ...errors, otp: "Mã OTP không đúng" }); return; } // Complete registration completeRegistration(phone); router.push("/"); }; const handleBackToPhone = () => { setStep("phone"); setOtp(""); setErrors({ phone: "", otp: "" }); }; return (
{/* Register Form Card */}
{/* Logo & Shop Name */}
{SHOP_INFO.name}

{SHOP_INFO.name}

{step === "phone" ? "Đăng ký tài khoản khách hàng" : "Xác thực số điện thoại"}

{/* Step Indicator */}
1
2
{/* Phone Step */} {step === "phone" && (
{/* Phone Input */}
{ setPhone(e.target.value); setErrors({ ...errors, phone: "" }); }} placeholder="0987654321" className={`text-foreground focus:ring-opacity-20 w-full rounded-xl border bg-white px-10 py-3 transition-all duration-150 outline-none placeholder:text-(--color-text-muted) focus:border-(--color-primary) focus:ring-2 focus:ring-(--color-primary) lg:pl-11 ${errors.phone ? "border-red-400" : "border-(--color-border)"} `} />
{errors.phone && (

{errors.phone}

)}

Nhập số điện thoại Việt Nam (10 số, bắt đầu bằng 0)

{/* Buttons */}
{/* Submit Button */} {/* Back to Login */} Quay lại đăng nhập
)} {/* OTP Step */} {step === "otp" && (
{/* Info Message */}

Mã OTP đã được gửi đến số {phone}

Demo OTP:{" "} {DEMO_OTP}

{/* OTP Input */}
{ setOtp(e.target.value); setErrors({ ...errors, otp: "" }); }} placeholder="Nhập mã OTP" maxLength={6} className={`text-foreground focus:ring-opacity-20 w-full rounded-xl border bg-white px-4 py-3 text-center text-lg tracking-widest transition-all duration-150 outline-none placeholder:text-sm placeholder:tracking-normal placeholder:text-(--color-text-muted) focus:border-(--color-primary) focus:ring-2 focus:ring-(--color-primary) lg:pl-11 ${errors.otp ? "border-red-400" : "border-(--color-border)"} `} />
{errors.otp && (

{errors.otp}

)}
{/* Buttons */}
{/* Submit Button */} {/* Back Button */}
{/* Resend OTP (disabled in demo) */}
)}
); }