import { MenuItem, Select } from "@mui/material";
import { useContext, useState } from "react";
import { useDispatch } from "react-redux";
import { AppContext } from "../../../../contexts/AppContext";
import { slaughterGetKillerKillhousesService } from "../../../slaughter-house/services/slaughter-get-killers-killhouses";
import { provinceGetSlaughterhousesQuotaService } from "../../services/province-get-slaughterhouses-quota";
import { provinceUpdateSlaughterIdentityService } from "../../services/province-update-slaughter-identity";

export const ProvinceUpdateSlaughterIdentity = ({
  killhouseType,
  slaughterKey,
}) => {
  const dispatch = useDispatch();
  const [openNotif] = useContext(AppContext);
  const [selectedOption, setSelectedOption] = useState(killhouseType);

  const handleOptionChange = (event) => {
    dispatch(
      provinceUpdateSlaughterIdentityService({
        percentage_key: slaughterKey,
        identity: event.target.value,
      })
    ).then((r) => {
      if (r.payload.error) {
        openNotif({
          vertical: "top",
          horizontal: "center",
          msg: r.payload.error,
          severity: "error",
        });
      } else {
        setSelectedOption(event.target.value);
        dispatch(provinceGetSlaughterhousesQuotaService());
        dispatch(slaughterGetKillerKillhousesService());
        openNotif({
          vertical: "top",
          horizontal: "center",
          msg: "عملیات با موفقیت انجام شد.",
          severity: "success",
        });
      }
    });
  };

  return (
    <Select
      label="Select Option"
      value={selectedOption}
      onChange={handleOptionChange}
      style={{ width: 140 }}
      size="small"
    >
      <MenuItem value="KillHouse">کشتارگاه</MenuItem>
      <MenuItem value="Killer">کشتارکن</MenuItem>
    </Select>
  );
};
