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

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

    const defaultData = {
        isDriver: `${row.original?.is_driver}` || "0",
        provinceId: row.original?.province_id || "",
        edarehShahriId: row.original?.edareh_shahri_id || "",
        name: row.original?.name || "",
        code: row.original?.code || "",
        phone_number: row.original?.mobile || "",
    };
    const handleSubmit = async (result) => {
        const formData = new FormData();
        formData.append("is_driver", result.isDriver);
        formData.append("province_id", result.provinceId);
        formData.append("edareh_shahri_id", result.edarehShahriId);
        formData.append("name", result.name);
        formData.append("code", result.code);
        formData.append("mobile", result.phone_number);

        await requestServer(`${UPDATE_RAHDARAN_ITEM}/${rowId}`, "post", {
            data: formData,
        })
            .then(() => {
                mutate();
                setOpen(false);
            })
            .catch(() => {});
    };
    return (
        <>
            <DialogHeader>
                <DialogTitle>ویرایش راهدار</DialogTitle>
            </DialogHeader>
            <CreateFormContent setOpen={setOpen} defaultValues={defaultData} onSubmitBase={handleSubmit} />
        </>
    );
};
export default EditController;
