import useRequest from "@/lib/hooks/useRequest";
import CreateFormContent from "../Create/Form/CreateFormContent";
import { UPDATE_PERMISSION } from "@/core/utils/routes";
import { DialogTitle } from "@mui/material";
import { DialogHeader } from "next/dist/client/components/react-dev-overlay/internal/components/Dialog";

const EditController = ({ rowId, mutate, setOpen, row }) => {
    const requestServer = useRequest({ notificationSuccess: true });

    const defaultData = {
        name: row.original?.name || "",
        name_fa: row.original?.name_fa || "",
        description: row.original?.description || "",
        type: row.original?.type || "",
        type_fa: row.original?.type_fa || "",
        need_province: row.original?.need_province || false,
        need_edare_shahri: row.original?.need_edare_shahri || false,
    };
    const handleSubmit = async (result) => {
        const formData = new FormData();
        formData.append("name", result.name);
        formData.append("name_fa", result.name_fa);
        formData.append("description", result.description);
        formData.append("type", result.type);
        formData.append("type_fa", result.type_fa);
        formData.append("need_province", result.need_province ? 1 : 0);
        formData.append("need_edare_shahri", result.need_edare_shahri ? 1 : 0);
        await requestServer(`${UPDATE_PERMISSION}/${rowId}`, "post", {
            data: formData,
        })
            .then(() => {
                mutate();
                setOpen(false);
            })
            .catch(() => {});
    };
    return (
        <>
            <DialogHeader>
                <DialogTitle>ویرایش سطح دسترسی</DialogTitle>
            </DialogHeader>
            <CreateFormContent setOpen={setOpen} defaultValues={defaultData} onSubmitBase={handleSubmit} />
        </>
    );
};
export default EditController;
