import { createEmployees, editEmployees, getDetailEmployees, } from "@/pages/employees/lib/api"; import formatPhone from "@/shared/lib/formatPhone"; import onlyNumber from "@/shared/lib/onlyNumber"; import { Form, FormControl, FormField, FormItem, FormMessage, } from "@/shared/ui/form"; import { Input } from "@/shared/ui/input"; import { Label } from "@/shared/ui/label"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/shared/ui/select"; import { zodResolver } from "@hookform/resolvers/zod"; import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import { Loader, X } from "lucide-react"; import { useEffect, type Dispatch, type SetStateAction } from "react"; import { useForm } from "react-hook-form"; import { useTranslation } from "react-i18next"; import { toast } from "sonner"; import z from "zod"; const roles = ["buxgalter", "operator"] as const; // ✅ Conditional schema - create uchun password majburiy, edit uchun yo'q const createEmployeeSchema = z.object({ firstname: z.string().min(2, "Kamida 2 ta belgidan iborat bo'lishi kerak"), lastname: z.string().min(2, "Kamida 2 ta belgidan iborat bo'lishi kerak"), phone: z.string().min(9, "Telefon raqam noto'g'ri"), role: z.string().min(1, { message: "Majburiy maydon" }), password: z .string() .min(6, "Parol kamida 6 ta belgidan iborat bo'lishi kerak"), }); const editEmployeeSchema = z.object({ firstname: z.string().min(2, "Kamida 2 ta belgidan iborat bo'lishi kerak"), lastname: z.string().min(2, "Kamida 2 ta belgidan iborat bo'lishi kerak"), phone: z.string().min(9, "Telefon raqam noto'g'ri"), role: z.string().min(1, { message: "Majburiy maydon" }), }); type CreateEmployeeFormValues = z.infer; type EditEmployeeFormValues = z.infer; type EmployeeFormValues = CreateEmployeeFormValues | EditEmployeeFormValues; const EditEmployees = ({ modalMode, editId, showModal, setEditId, setShowModal, }: { modalMode: "add" | "edit"; showModal: boolean; setEditId: Dispatch>; editId: number | null; setShowModal: Dispatch>; }) => { const { t } = useTranslation(); const queryClient = useQueryClient(); // ✅ Dinamik schema - modalMode'ga qarab const form = useForm({ resolver: zodResolver( modalMode === "add" ? createEmployeeSchema : editEmployeeSchema, ), defaultValues: { firstname: "", lastname: "", phone: "+998", role: "", ...(modalMode === "add" && { password: "" }), }, }); const { data } = useQuery({ queryKey: ["detail_employees", editId], queryFn: () => getDetailEmployees({ id: editId! }), select(data) { return data.data.data; }, enabled: !!editId, }); useEffect(() => { if (data && editId) { form.reset({ firstname: data.first_name, lastname: data.last_name, phone: data.phone, role: data.role, }); } else if (!editId && showModal) { form.reset({ firstname: "", lastname: "", phone: "+998", role: "", ...(modalMode === "add" && { password: "" }), }); } }, [data, editId, showModal, modalMode, form]); const { mutate: create, isPending } = useMutation({ mutationFn: ({ body, }: { body: { first_name: string; last_name: string; phone: string; email: string | null; role: "buxgalter" | "operator"; password: string; }; }) => createEmployees({ body }), onSuccess: () => { queryClient.refetchQueries({ queryKey: ["detail_employees"] }); queryClient.refetchQueries({ queryKey: ["employees"] }); setShowModal(false); setEditId(null); }, onError: () => { toast.error(t("Xatolik yuz berdi"), { position: "top-center", richColors: true, }); }, }); const { mutate: edit, isPending: editPengding } = useMutation({ mutationFn: ({ body, id, }: { id: number; body: { first_name: string; last_name: string; phone: string; email: string | null; role: "buxgalter" | "operator"; }; }) => editEmployees({ id, body }), onSuccess: () => { queryClient.refetchQueries({ queryKey: ["detail_employees"] }); queryClient.refetchQueries({ queryKey: ["employees"] }); setShowModal(false); setEditId(null); }, onError: () => { toast.error(t("Xatolik yuz berdi"), { position: "top-center", richColors: true, }); }, }); function onSubmit(values: EmployeeFormValues) { if (modalMode === "add" && editId === null) { create({ body: { email: null, first_name: values.firstname, last_name: values.lastname, phone: onlyNumber(values.phone), role: values.role as "buxgalter" | "operator", password: (values as CreateEmployeeFormValues).password, }, }); } else if (modalMode === "edit" && editId !== null) { edit({ id: editId, body: { email: null, first_name: values.firstname, last_name: values.lastname, phone: onlyNumber(values.phone), role: values.role as "buxgalter" | "operator", }, }); } } return (

{modalMode === "add" ? t("Xodim qo'shish") : t("Xodimni tahrirlash")}

( )} /> ( )} /> ( )} /> {/* ✅ Password field - faqat "add" modeda ko'rinadi */} {modalMode === "add" && ( ( )} /> )} ( )} />
); }; export default EditEmployees;