"use client"; import React, { useMemo, useState } from "react"; import { Avatar, Box, Button, MenuItem, Paper, TextField, Typography, } from "@mui/material"; import { UploadFile } from "@mui/icons-material"; import { LocalizationProvider } from "@mui/x-date-pickers/LocalizationProvider"; import { AdapterDateFnsJalali } from "@mui/x-date-pickers/AdapterDateFnsJalali"; import { DatePicker } from "@mui/x-date-pickers/DatePicker"; type IdentityFormData = { applicantId: string; firstName: string; lastName: string; fatherName: string; nationalCode: string; birthDate: string; birthPlace: string; gender: string; religion: string; nationality: string; profilePhotoId: string; }; type IdentityFormErrors = Partial>; const initialForm: IdentityFormData = { applicantId: "", firstName: "", lastName: "", fatherName: "", nationalCode: "", birthDate: "", birthPlace: "", gender: "", religion: "", nationality: "", profilePhotoId: "", }; export default function IdentityForm() { const [formData, setFormData] = useState(initialForm); const [errors, setErrors] = useState({}); const [submitted, setSubmitted] = useState(false); const [profilePhoto, setProfilePhoto] = useState(null); const [profilePhotoPreview, setProfilePhotoPreview] = useState(""); const [profilePhotoError, setProfilePhotoError] = useState(""); const [birthDateValue, setBirthDateValue] = useState(null); const handleProfilePhotoChange = ( event: React.ChangeEvent, ) => { const file = event.target.files?.[0]; if (!file) return; if (!file.type.startsWith("image/")) { setProfilePhoto(null); setProfilePhotoPreview(""); setProfilePhotoError("فقط فایل تصویری مجاز است"); return; } const maxSize = 500 * 1024; // 500KB if (file.size > maxSize) { setProfilePhoto(null); setProfilePhotoPreview(""); setProfilePhotoError("حجم عکس باید حداکثر ۵۰۰ کیلوبایت باشد"); return; } setProfilePhoto(file); setProfilePhotoError(""); const previewUrl = URL.createObjectURL(file); setProfilePhotoPreview(previewUrl); }; const genderOptions = useMemo(() => ["مرد", "زن", "سایر"], []); const religionOptions = useMemo( () => ["اسلام", "مسیحیت", "یهودیت", "زرتشتی", "سایر"], [], ); const handleChange = (field: keyof IdentityFormData) => (event: React.ChangeEvent) => { let value = event.target.value; if (field === "nationalCode") { value = value.replace(/\D/g, "").slice(0, 10); } setFormData((prev) => ({ ...prev, [field]: value, })); if (errors[field]) { setErrors((prev) => ({ ...prev, [field]: "", })); } }; const validate = () => { const newErrors: IdentityFormErrors = {}; let hasError = false; if (!formData.applicantId.trim()) { newErrors.applicantId = "شناسه متقاضی الزامی است"; hasError = true; } if (!formData.firstName.trim()) { newErrors.firstName = "نام الزامی است"; hasError = true; } if (!formData.lastName.trim()) { newErrors.lastName = "نام خانوادگی الزامی است"; hasError = true; } if (!formData.nationalCode.trim()) { newErrors.nationalCode = "کد ملی الزامی است"; hasError = true; } else if (!/^\d{10}$/.test(formData.nationalCode)) { newErrors.nationalCode = "کد ملی باید ۱۰ رقم باشد"; hasError = true; } if (!formData.birthDate.trim()) { newErrors.birthDate = "تاریخ تولد الزامی است"; hasError = true; } if (!formData.gender.trim()) { newErrors.gender = "جنسیت الزامی است"; hasError = true; } if (!formData.nationality.trim()) { newErrors.nationality = "ملیت الزامی است"; hasError = true; } if (!profilePhoto) { setProfilePhotoError("عکس پرسنلی الزامی است"); hasError = true; } else { setProfilePhotoError(""); } setErrors(newErrors); return !hasError; }; const handleSubmit = (event: React.FormEvent) => { event.preventDefault(); setSubmitted(false); if (!validate()) return; const payload = { ...formData, birthDate: formData.birthDate ? new Date(formData.birthDate) : null, fatherName: formData.fatherName || null, birthPlace: formData.birthPlace || null, religion: formData.religion || null, profilePhotoId: formData.profilePhotoId || null, }; console.log("Identity Payload:", payload); setSubmitted(true); }; return (
{ setBirthDateValue(newValue); setFormData((prev) => ({ ...prev, birthDate: newValue ? newValue.toISOString() : "", })); if (errors.birthDate) { setErrors((prev) => ({ ...prev, birthDate: "", })); } }} slotProps={{ textField: { fullWidth: true, error: !!errors.birthDate, helperText: errors.birthDate, }, }} /> {genderOptions.map((item) => ( {item} ))} {religionOptions.map((item) => ( {item} ))} عکس پرسنلی فقط فایل تصویری مجاز است و حجم آن باید حداکثر ۵۰۰ کیلوبایت باشد. {profilePhoto && ( فایل انتخاب‌شده: {profilePhoto.name} )} {profilePhotoError && ( {profilePhotoError} )}
); }