import { Dialog, DialogTitle, IconButton } from "@mui/material";
import CreateFormContent from "./CreateFormContent";
import CloseIcon from "@mui/icons-material/Close";
import useRequest from "@/lib/hooks/useRequest";
import { CREATE_PERMISSION } from "@/core/utils/routes";
import { DialogHeader } from "next/dist/client/components/react-dev-overlay/internal/components/Dialog";

const CreateForm = ({ open, setOpen, mutate }) => {
    const requestServer = useRequest({ notificationSuccess: true });
    const defaultValues = {
        name: "",
        name_fa: "",
        description: "",
        type: "",
        type_fa: "",
        need_province: false,
        need_edare_shahri: false,
    };
    const onSubmit = 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(CREATE_PERMISSION, "post", {
            data: formData,
        })
            .then(() => {
                mutate();
                setOpen(false);
            })
            .catch(() => {});
    };
    return (
        <Dialog open={open} fullWidth maxWidth="sm">
            <IconButton
                aria-label="close"
                onClick={() => setOpen(false)}
                sx={(theme) => ({
                    position: "absolute",
                    right: 8,
                    top: 8,
                    zIndex: 50,
                    color: theme.palette.grey[500],
                })}
            >
                <CloseIcon />
            </IconButton>
            <DialogHeader>
                <DialogTitle>ثبت سطح دسترسی جدید</DialogTitle>
            </DialogHeader>
            {open && (
                <CreateFormContent
                    defaultValues={defaultValues}
                    onSubmitBase={onSubmit}
                    setOpen={setOpen}
                    mutate={mutate}
                />
            )}
        </Dialog>
    );
};
export default CreateForm;
