import { EDIT_VIOLATION } 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 EditViolationForm from "./EditViolationForm";
import moment from "jalali-moment";

const EditController = ({ rowId, mutate, setOpen, row }) => {
    const requestServer = useRequest({ notificationSuccess: true });
    const [endDate] = row?.original?.enter_station?.split(" ") || [null];

    const defaultData = {
        end_date: endDate,
        end_km: "",
        end_time: row?.original?.enter_time ? new Date(row.original.enter_time) : null,
    };
    const handleSubmit = async (result) => {
        const payload = {};

        if (result.end_date && result.end_time) {
            payload.enter_time = `${result.end_date} ${moment(result.end_time).format("HH:mm")}`;
        }
        payload.end_km = result.end_km;

        await requestServer(`${EDIT_VIOLATION}/${rowId}`, "post", {
            data: payload,
        })
            .then(() => {
                mutate();
                setOpen(false);
            })
            .catch(() => {});
    };

    return (
        <>
            <DialogHeader>
                <DialogTitle>ثبت تردد</DialogTitle>
            </DialogHeader>
            <EditViolationForm
                setOpenMachinesDialog={setOpen}
                defaultValues={defaultData}
                onSubmitBase={handleSubmit}
            />
        </>
    );
};
export default EditController;
