import { ATIVITY_RECEIPT_DAMAGE_ITEMS } from "@/core/utils/routes";
import useRequest from "@/lib/hooks/useRequest";
import { Switch } from "@mui/material";
import { useState } from "react";

const Activity = ({ rowId, mutate, status }) => {
    const [disabled, setDisabled] = useState(false);
    const [checked, setChecked] = useState(status == 1);
    const requestServer = useRequest();

    const handleSwitch = (event) => {
        const newChecked = event.target.checked;
        setChecked(newChecked);
        setDisabled(true);

        requestServer(`${ATIVITY_RECEIPT_DAMAGE_ITEMS}/${rowId}`, "post")
            .then(() => {
                mutate();
            })
            .catch(() => {
                setChecked(!newChecked);
            })
            .finally(() => {
                setDisabled(false);
            });
    };

    return <Switch size="small" disabled={disabled} checked={checked} onChange={handleSwitch} />;
};

export default Activity;
