import { Switch } from "@mui/material";
import { useState } from "react";
import { useDispatch } from "react-redux";
import { provinceChangeSlaughterCarPermissionService } from "../../services/province-change-slaughter-car-permission";
import { provinceGetOnlyKillHousesService } from "../../services/province-get-only-kill-houses";

export const ProvinceManageSlaughtersCarState = ({ state, killHouseKey }) => {
  const dispatch = useDispatch();
  const [checked, setChecked] = useState(state);

  const handleToggle = () => {
    setChecked(!checked);
    dispatch(
      provinceChangeSlaughterCarPermissionService({
        allow: !state,
        kill_house_key: killHouseKey,
      })
    ).then((r) => {
      dispatch(provinceGetOnlyKillHousesService());
    });
  };

  return <Switch checked={checked} onChange={handleToggle} />;
};
