"use client" import type React from "react" import { useState } from "react" import { Upload } from "lucide-react" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" import { useOpenStore } from "@/lib/hooks" import { useToast } from "@/hooks/use-toast" interface OpenStorePageProps { locale?: string translations?: { title: string firstName: string lastName: string email: string phone: string uploadPatent: string submit: string selectedFile: string firstNameRequired: string lastNameRequired: string emailInvalid: string phoneInvalid: string fileRequired: string fileSizeError: string fileTypeError: string } } interface FormData { firstName: string lastName: string email: string phone: string file: File | null } interface FormErrors { firstName?: string lastName?: string email?: string phone?: string file?: string } export default function OpenStorePage({ locale = "ru", translations }: OpenStorePageProps) { const [formData, setFormData] = useState({ firstName: "", lastName: "", email: "", phone: "+993", file: null, }) const [errors, setErrors] = useState({}) const [fileName, setFileName] = useState("") const { mutate: submitOpenStore, isPending: loading } = useOpenStore() const { toast } = useToast() const t = translations || { title: "Форма подачи заявления на открытие магазина", firstName: "Имя", lastName: "Фамилия", email: "Email", phone: "Телефон", uploadPatent: "Загрузите патент на розничную торговлю (PDF, JPG)", submit: "Отправить", selectedFile: "Выбранный файл", firstNameRequired: "Имя обязательно", lastNameRequired: "Фамилия обязательна", emailInvalid: "Некорректный email", phoneInvalid: "Некорректный номер телефона", fileRequired: "Патент обязателен", fileSizeError: "Файл слишком большой (макс. 25MB)", fileTypeError: "Только PDF и JPG документы", } const validateForm = (): boolean => { const newErrors: FormErrors = {} if (!formData.firstName.trim()) { newErrors.firstName = t.firstNameRequired } if (!formData.lastName.trim()) { newErrors.lastName = t.lastNameRequired } const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/ if (!emailRegex.test(formData.email)) { newErrors.email = t.emailInvalid } const phoneRegex = /^\+?[0-9]{6,15}$/ if (!phoneRegex.test(formData.phone)) { newErrors.phone = t.phoneInvalid } if (!formData.file) { newErrors.file = t.fileRequired } else { const allowedTypes = ["image/jpeg", "image/jpg", "application/pdf"] if (!allowedTypes.includes(formData.file.type)) { newErrors.file = t.fileTypeError } if (formData.file.size > 25 * 1024 * 1024) { newErrors.file = t.fileSizeError } } setErrors(newErrors) return Object.keys(newErrors).length === 0 } const handleInputChange = (e: React.ChangeEvent) => { const { name, value } = e.target setFormData((prev) => ({ ...prev, [name]: value })) if (errors[name as keyof FormErrors]) { setErrors((prev) => ({ ...prev, [name]: undefined })) } } const handleFileChange = (e: React.ChangeEvent) => { const file = e.target.files?.[0] if (file) { setFormData((prev) => ({ ...prev, file })) setFileName(file.name) if (errors.file) { setErrors((prev) => ({ ...prev, file: undefined })) } } } const handleSubmit = (e: React.FormEvent) => { e.preventDefault() if (!validateForm()) return if (formData.file) { submitOpenStore( { firstName: formData.firstName, lastName: formData.lastName, email: formData.email, phone: formData.phone, patentFile: formData.file, }, { onSuccess: () => { toast({ title: "Success", description: "Your store request has been submitted successfully", }) setFormData({ firstName: "", lastName: "", email: "", phone: "+993", file: null, }) setFileName("") }, onError: (error: any) => { toast({ title: "Error", description: error?.message || "Failed to submit store request", variant: "destructive", }) }, }, ) } } return (
{t.title} Заполните форму для подачи заявления
{/* First Name */}
{errors.firstName &&

{errors.firstName}

}
{/* Last Name */}
{errors.lastName &&

{errors.lastName}

}
{/* Email */}
{errors.email &&

{errors.email}

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

{errors.phone}

}
{/* File Upload */}
{fileName && (

{t.selectedFile}: {fileName}

)} {errors.file &&

{errors.file}

}
{/* Submit Button */}
) }