import {
  Button,
  FormControl,
  InputLabel,
  MenuItem,
  Select,
  Typography,
} from "@mui/material";
import { useContext, useState } from "react";
import { useDispatch, useSelector } from "react-redux";
import { Grid } from "../../../../components/grid/Grid";
import { AppContext } from "../../../../contexts/AppContext";
import { SPACING } from "../../../../data/spacing";
import { CLOSE_MODAL, DRAWER } from "../../../../lib/redux/slices/appSlice";
import { slaughterHouseVetNewRequests } from "../../../slaughter-house-vet/services/slaughter-house-vet-new-requests";
import { vetFarmCancelBarService } from "../../services/vet-farm-cancel-bar";
import { vetFarmGetAllocatedService } from "../../services/vet-farm-get-allocated";
import { vetFarmGetDeletedBarsService } from "../../services/vet-farm-get-deleted-bars";
import { checkPathStartsWith } from "../../../../utils/checkPathStartsWith";

export const VetFarmCancelBar = ({ killHouseRequestKey, updateTable }) => {
  const dispatch = useDispatch();
  const [openNotif, , selectedDate1, , selectedDate2] = useContext(AppContext);
  const [selectedOption, setSelectedOption] = useState(null);
  const selectedSubUser = useSelector(
    (state) => state.userSlice.selectedSubUser
  );
  const handleSelectChange = (event) => {
    setSelectedOption(event.target.value);
  };

  return (
    <Grid container direction="column" width="100%" gap={SPACING.SMALL}>
      <Typography color="error">
        در صورت لغو بار کل اطلاعات حذف گردیده و تعداد مرغ تخصیصی به جوجه ریزی
        مرغدار برمیگردد.
      </Typography>
      <Grid style={{ width: "100%" }}>
        <FormControl>
          <InputLabel id="my-select-label">دلیل لغو بار</InputLabel>
          <Select
            value={selectedOption}
            onChange={handleSelectChange}
            label="دلیل لغو بار"
          >
            <MenuItem value="عدم ارسال راننده توسط کشتارگاه">
              عدم ارسال راننده توسط کشتارگاه
            </MenuItem>
            <MenuItem value="عدم تحویل بار توسط مرغدار">
              عدم تحویل بار توسط مرغدار
            </MenuItem>
          </Select>
        </FormControl>
      </Grid>
      <Button
        disabled={!selectedOption}
        variant="contained"
        onClick={() => {
          dispatch(
            vetFarmCancelBarService({
              kill_house_request_key: killHouseRequestKey,
              message: selectedOption,
            })
          ).then((r) => {
            if (r.payload.status) {
              openNotif({
                vertical: "top",
                horizontal: "center",
                msg: "عملیات با موفقیت انجام شد.",
                severity: "success",
              });
              updateTable();
              dispatch(
                slaughterHouseVetNewRequests({
                  selectedDate1,
                  selectedDate2,
                  role_key: checkPathStartsWith("slaughter")
                    ? selectedSubUser?.key || ""
                    : "",
                })
              );

              dispatch(
                vetFarmGetDeletedBarsService({
                  selectedDate1,
                  selectedDate2,
                  role_key: checkPathStartsWith("slaughter")
                    ? selectedSubUser?.key || ""
                    : "",
                })
              );
              dispatch(
                vetFarmGetAllocatedService({
                  selectedDate1,
                  selectedDate2,
                  role_key: checkPathStartsWith("slaughter")
                    ? selectedSubUser?.key || ""
                    : "",
                })
              );
            } else {
              openNotif({
                vertical: "top",
                horizontal: "center",
                msg: r.payload.result,
                severity: "error",
              });
            }
          });
          dispatch(CLOSE_MODAL());
          dispatch(
            DRAWER({
              right: false,
              bottom: false,
              top: false,
              content: null,
              size: null,
            })
          );
        }}
      >
        ثبت
      </Button>
    </Grid>
  );
};
