"use client"; import { useTranslations } from "next-intl"; import Image from "next/image"; import { useState, useEffect } from "react"; import { X } from "lucide-react"; import { usePriceModalStore } from "@/store/useProceModalStore"; export function PriceModal() { const t = useTranslations("priceModal"); const { isOpen, product, closeModal } = usePriceModalStore(); const [formData, setFormData] = useState({ name: "", phone: "+998 ", captcha: "", }); const [errors, setErrors] = useState({ name: "", phone: "", captcha: "", }); const [captchaCode, setCaptchaCode] = useState(""); const [isSubmitting, setIsSubmitting] = useState(false); // Generate random captcha useEffect(() => { if (isOpen) { const code = Math.random().toString(36).substring(2, 8).toUpperCase(); setCaptchaCode(code); } }, [isOpen]); // Reset form when modal closes useEffect(() => { if (!isOpen) { setFormData({ name: "", phone: "+998 ", captcha: "" }); setErrors({ name: "", phone: "", captcha: "" }); } }, [isOpen]); // Prevent body scroll when modal is open useEffect(() => { if (isOpen) { document.body.style.overflow = "hidden"; } else { document.body.style.overflow = "unset"; } return () => { document.body.style.overflow = "unset"; }; }, [isOpen]); const formatPhoneNumber = (value: string) => { const numbers = value.replace(/\D/g, ""); if (!numbers.startsWith("998")) { return "+998 "; } let formatted = "+998 "; const rest = numbers.slice(3); if (rest.length > 0) formatted += rest.slice(0, 2); if (rest.length > 2) formatted += " " + rest.slice(2, 5); if (rest.length > 5) formatted += " " + rest.slice(5, 7); if (rest.length > 7) formatted += " " + rest.slice(7, 9); return formatted; }; const handlePhoneChange = (e: React.ChangeEvent) => { const formatted = formatPhoneNumber(e.target.value); setFormData({ ...formData, phone: formatted }); if (errors.phone) { setErrors({ ...errors, phone: "" }); } }; const handleInputChange = (e: React.ChangeEvent) => { const { name, value } = e.target; setFormData({ ...formData, [name]: value }); if (errors[name as keyof typeof errors]) { setErrors({ ...errors, [name]: "" }); } }; const validateForm = () => { const newErrors = { name: "", phone: "", captcha: "", }; if (!formData.name.trim()) { newErrors.name = t("validation.nameRequired"); } const phoneNumbers = formData.phone.replace(/\D/g, ""); if (phoneNumbers.length !== 12) { newErrors.phone = t("validation.phoneRequired"); } else if (!phoneNumbers.startsWith("998")) { newErrors.phone = t("validation.phoneInvalid"); } if (!formData.captcha.trim()) { newErrors.captcha = t("validation.captchaRequired"); } else if (formData.captcha.toUpperCase() !== captchaCode) { newErrors.captcha = t("validation.captchaRequired"); } setErrors(newErrors); return !newErrors.name && !newErrors.phone && !newErrors.captcha; }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!validateForm()) { return; } setIsSubmitting(true); try { // API call logikangiz await new Promise((resolve) => setTimeout(resolve, 1500)); // Success alert(t("success")); closeModal(); } catch (error) { alert(t("error")); } finally { setIsSubmitting(false); } }; if (!isOpen || !product) return null; return (
{/* Backdrop */}
{/* Modal */}
{/* Close button */} {/* Content */}

{t("title")}

{/* Product Info */}
{product.name}

{product.name}

{t("product.inStock")}
{/* Form */}
{/* Name */}
{errors.name && (

{errors.name}

)}
{/* Phone */}
{errors.phone && (

{errors.phone}

)}
{/* Captcha */} {/*
{captchaCode}
{errors.captcha && (

{errors.captcha}

)}
*/} {/* Submit Button */}
); }