import { Controller, useForm } from "react-hook-form"; import { Grid } from "../../../components/Grid/Grid"; import { FormApiBasedAutoComplete } from "../../../components/FormItems/FormApiBasedAutoComplete"; import { zValidateAutoComplete, zValidateAutoCompleteOptional, zValidateNumber, zValidateString, } from "../../../data/getFormTypeErrors"; import { zodResolver } from "@hookform/resolvers/zod"; import { z } from "zod"; import AutoComplete from "../../../components/AutoComplete/AutoComplete"; import { getMonthsList } from "../../../data/getMonths"; import { RadioGroup } from "../../../components/RadioButton/RadioGroup"; import { useEffect, useRef, useState } from "react"; import ToggleButton from "../../../components/ToggleButton/ToggleButton"; import Typography from "../../../components/Typography/Typography"; import Textfield from "../../../components/Textfeild/Textfeild"; import { useApiRequest } from "../../../utils/useApiRequest"; type Props = { item: any; getData: () => void; onSubmit: (data: any) => void; setFormRef: (ref: HTMLFormElement | null) => void; visible: boolean; }; const saleTypes = [ { value: "gov", label: "دولتی" }, { value: "free", label: "آزاد" }, ]; const groupTypes = [ { value: "روستایی", key: "rural", disabled: false }, { value: "صنعتی", key: "industrial", disabled: false }, { value: "عشایری", key: "nomadic", disabled: true }, ]; const posSaleTypes = [ { value: "هردو", key: "all", disabled: false }, { value: "بر اساس وزن", key: "weight", disabled: false }, { value: "بر اساس تعداد راس دام", key: "count", disabled: false }, ]; export const QuotaLevel1 = ({ item, onSubmit, setFormRef, visible }: Props) => { const [hasDistributionLimit, setHasDistributionLimit] = useState( item?.distribution_mode?.length ? true : false, ); const internalRef = useRef(null); const [livestockTypes, setLivestockTypes] = useState< Array<{ livestock_group: string; livestock_type: any; weight_type: any; quantity_kg: number; fa: string; }> >([]); useEffect(() => { if (visible) { setFormRef(internalRef.current); } }, [visible]); const schema = z.object({ product: zValidateNumber("محصول"), month_choices: zValidateAutoComplete("سهمیه ماه"), distribution_mode: hasDistributionLimit ? zValidateAutoComplete("محدودیت توزیع دوره") : zValidateAutoCompleteOptional(), sale_license: zValidateAutoComplete("مجوز فروش"), pos_sale_type: zValidateString("نوع فروش در دستگاه"), sale_type: zValidateString("نوع فروش"), group: zValidateAutoComplete("گروه"), quota_weight: zValidateNumber("وزن سهمیه"), sale_unit: zValidateNumber("نوع مولفه"), }); type FormValues = z.infer; const { control, handleSubmit, setValue, getValues, trigger, formState: { errors }, } = useForm({ resolver: zodResolver(schema), defaultValues: { product: item?.product ? item?.product?.product_id : "", month_choices: item?.month_choices || [], distribution_mode: item?.distribution_mode || [], sale_license: item?.sale_license || [], sale_type: item?.sale_type || "gov", group: item?.group || [], pos_sale_type: item?.pos_sale_type || "all", quota_weight: item?.quota_weight || 0, sale_unit: item?.sale_unit ? item?.sale_unit?.id : "", }, }); const { data: livestockData } = useApiRequest({ api: "/livestock/web/api/v1/livestock_type/", method: "get", params: { page: 1, page_size: 100 }, queryKey: ["livestockTypes"], }); useEffect(() => { if (livestockData?.results) { const getQuantity = (allocate: any, group: string) => { const result = item?.livestock_allocations?.find( (option: any) => option?.livestock_type?.weight_type === allocate?.weight_type && option?.livestock_group === group && option?.livestock_type?.name === allocate?.name, ); return result?.quantity_kg || 0; }; const formattedData = livestockData.results.flatMap((item: any) => [ { livestock_group: "rural", livestock_type: item.id, weight_type: item.weight_type, quantity_kg: getQuantity(item, "rural"), fa: item.name, }, { livestock_group: "industrial", livestock_type: item.id, weight_type: item.weight_type, quantity_kg: getQuantity(item, "industrial"), fa: item.name, }, ]); setLivestockTypes(formattedData); } }, [livestockData, item]); const handleQuantityChange = (index: number, value: number) => { setLivestockTypes((prev) => { const newTypes = [...prev]; newTypes[index] = { ...newTypes[index], quantity_kg: value }; return newTypes; }); }; const findLivestockIndex = ( group: string, weightType: string, fa: string, ) => { return livestockTypes.findIndex( (item) => item.livestock_group === group && item.weight_type === weightType && item.fa === fa, ); }; const handleSubmitForm = (data: FormValues) => { onSubmit({ ...data, livestockTypes, hasDistributionLimit }); }; return (
( <> { setValue("product", r); trigger("product"); }} /> )} /> ( )} /> ( { setValue("month_choices", keys); trigger("month_choices"); }} error={!!errors.month_choices} helperText={errors.month_choices?.message} title="سهمیه ماه" /> )} /> ( { setValue("sale_license", keys); trigger("sale_license"); }} error={!!errors.sale_license} helperText={errors.sale_license?.message} title="مجوز فروش" /> )} /> ( { setValue("sale_type", e.target.value); }} /> )} /> ( { setValue("group", keys); trigger("group"); }} error={!!errors.group} helperText={errors.group?.message} title="گروه" /> )} /> ( <> { setValue("sale_unit", r); trigger("sale_unit"); }} /> )} /> { if (!hasDistributionLimit) { setValue("distribution_mode", []); trigger("distribution_mode"); } setHasDistributionLimit(!hasDistributionLimit); }} title="محدودیت توزیع دوره" aria-label="Toggle like" /> {hasDistributionLimit && ( ( { setValue("distribution_mode", keys); trigger("distribution_mode"); }} error={!!errors.distribution_mode} helperText={errors.distribution_mode?.message} title="محدودیت توزیع دوره" /> )} /> )} ( { setValue("pos_sale_type", keys.toString()); trigger("pos_sale_type"); }} error={!!errors.pos_sale_type} helperText={errors.pos_sale_type?.message} title="نوع فروش در دستگاه" /> )} /> {getValues("group")?.includes("rural") && ( <> سهمیه دام روستایی سنگین {livestockTypes ?.filter( (option) => option.livestock_group === "rural" && option?.weight_type === "H", ) .map((item, i) => { const index = findLivestockIndex("rural", "H", item.fa); return ( { const value = parseInt(e.target.value.replace(/,/g, "")) || 0; if (index !== -1) { handleQuantityChange(index, value); } }} /> ); })} سبک {livestockTypes ?.filter( (option) => option.livestock_group === "rural" && option?.weight_type === "L", ) .map((item, i) => { const index = findLivestockIndex("rural", "L", item.fa); return ( { const value = parseInt(e.target.value.replace(/,/g, "")) || 0; if (index !== -1) { handleQuantityChange(index, value); } }} /> ); })} )} {getValues("group")?.includes("industrial") && ( <> سهمیه دام صنعتی سنگین {livestockTypes ?.filter( (option) => option.livestock_group === "industrial" && option?.weight_type === "H", ) .map((item, i) => { const index = findLivestockIndex( "industrial", "H", item.fa, ); return ( { const value = parseInt(e.target.value.replace(/,/g, "")) || 0; if (index !== -1) { handleQuantityChange(index, value); } }} /> ); })} سبک {livestockTypes ?.filter( (option) => option.livestock_group === "industrial" && option?.weight_type === "L", ) .map((item, i) => { const index = findLivestockIndex( "industrial", "L", item.fa, ); return ( { const value = parseInt(e.target.value.replace(/,/g, "")) || 0; if (index !== -1) { handleQuantityChange(index, value); } }} /> ); })} )}
); };