"use client"; import { Box, Typography, useTheme, useMediaQuery, Chip, Button, FormHelperText, } from "@mui/material"; import CheckCircleIcon from "@mui/icons-material/CheckCircle"; import { ErrorMessage, Form, FormikProps } from "formik"; import BusinessIcon from "@mui/icons-material/Business"; import LocationOnIcon from "@mui/icons-material/LocationOn"; import LocalHospitalIcon from "@mui/icons-material/LocalHospital"; import { useGetAllCenters, useSelectCenter } from "@/hooks/center.hook"; import { RegistrationCenterFormProps, RegistrationCenterFormValues, } from "./RegistrationCenterForm"; import { CenterItem } from "@/core/types"; import { Warning } from "@mui/icons-material"; import { toast } from "sonner"; import { useEffect } from "react"; // تعریف اینترفیس برای تمیزی بیشتر interface InnerFormProps extends FormikProps, RegistrationCenterFormProps {} export default function InnerRegistrationCenterForm(props: InnerFormProps) { const { data,error } = useGetAllCenters(); const { mutateAsync, isPending } = useSelectCenter(); const theme = useTheme(); const isMobile = useMediaQuery(theme.breakpoints.down("md")); const handleBack = () => { props.update({ registrationCenter: props.values }); props.setStep((prev) => Math.max(1, prev - 1)); }; const handleCenterSelect = (center: CenterItem) => { props.setFieldValue("selectedCenter", center); }; const handleNext = async () => { // اعتبارسنجی دستی کل فرم const errors = await props.validateForm(); props.update({ registrationCenter: props.values, }); // اگر در گام فعلی خطایی وجود ندارد، برو مرحله بعد if (Object.keys(errors).length === 0) { if (props.step === 12) { props.submitForm(); // ثبت نهایی } else { props.setStep(props.step + 1); } } // try { // const applicant = await mutateAsync(props.values.selectedCenter?.id!); // props.update({ // applicantId: applicant.id, // registrationCenter: props.values, // }); // localStorage.setItem( // "applicationDraft", // JSON.stringify({ // applicantId: applicant.id, // registrationCenter: props.values, // formStep: applicant.formStep, // }), // ); // props.setStep(2); // } catch (error: any) { // console.log(error) // toast.error(error?.message || "خطا در ثبت مرکز"); // } }; const renderCenterList = () => (
{data?.data.map((center: CenterItem) => { const isSelected = props.values.selectedCenter?.id === center.id; return (
handleCenterSelect(center)} sx={{ p: 2.5, borderRadius: "18px", border: isSelected ? "2px solid #2563eb" : props.errors.selectedCenter ? "2px solid #d32f2f" : "1px solid #e2e8f0", backgroundColor: isSelected ? "#eff6ff" : "#fff", cursor: "pointer", transition: "all 0.25s ease", "&:hover": { borderColor: "#2563eb" }, }} > {center.name} {center.address} {center.isUrgent && ( {/* */} استخدام فوري )} {isSelected && }
); })}
{/* نمایش پیام خطا به صورت تمیز زیر لیست */}
); return (
{/* رندر محتوای استپ ها */} {props.step === 1 ? ( renderCenterList() ) : ( محتوای مرحله {props.step} )}
); }