import { IconButton, Popover, Tooltip } from "@mui/material";
import React, { useContext, useState } from "react";
import { useDispatch } from "react-redux";
import TuneIcon from "@mui/icons-material/Tune";
import DeleteIcon from "@mui/icons-material/Delete";
import EditIcon from "@mui/icons-material/Edit";
import { Grid } from "../../../../components/grid/Grid";
import { AppContext } from "../../../../contexts/AppContext";
import { slaughterDeleteAllocationForFreezing } from "../../services/slaughter-delete-allocation-for-freezing";
import { OPEN_MODAL } from "../../../../lib/redux/slices/appSlice";
import { SlaughterAllocateToFreezing } from "../slaughter-allocate-to-freezing/SlaughterAllocateToFreezing";

export const SlaughterFreezingAllocationsOperations = ({
  item,
  fetchApiData,
  updateInventory,
}) => {
  const dispatch = useDispatch();
  const [openNotif] = useContext(AppContext);

  const [anchorEl, setAnchorEl] = useState(null);

  const handleClick = (event) => {
    setAnchorEl(event.currentTarget);
  };

  const handleClose = () => {
    setAnchorEl(null);
  };

  const open = Boolean(anchorEl);
  const id = open ? "popover" : undefined;

  return (
    <div>
      <IconButton
        aria-describedby={id}
        variant="contained"
        color="primary"
        onClick={handleClick}
      >
        <TuneIcon />
      </IconButton>
      <Popover
        anchorOrigin={{
          vertical: "bottom",
          horizontal: "right",
        }}
        transformOrigin={{
          vertical: "top",
          horizontal: "left",
        }}
        id={id}
        open={open}
        anchorEl={anchorEl}
        onClose={handleClose}
      >
        <div style={{ padding: "20px" }}>
          <Grid container direction="column">
            <Tooltip placement="right" title="حذف انجماد">
              <IconButton
                aria-label="delete"
                color="error"
                onClick={() => {
                  handleClose();

                  dispatch(
                    slaughterDeleteAllocationForFreezing(item?.key)
                  ).then((r) => {
                    if (r.payload.error) {
                      openNotif({
                        vertical: "top",
                        horizontal: "center",
                        msg: "مشکلی پیش آمده است",
                        severity: "error",
                      });
                    } else {
                      fetchApiData();
                      updateInventory();
                      openNotif({
                        vertical: "top",
                        horizontal: "center",
                        msg: "عملیات با موفقیت انجام شد.",
                        severity: "success",
                      });
                    }
                  });
                }}
              >
                <DeleteIcon />
              </IconButton>
            </Tooltip>
            <Tooltip placement="right" title="ویرایش انجماد">
              <IconButton
                aria-label="delete"
                color="primary"
                onClick={() => {
                  handleClose();

                  dispatch(
                    OPEN_MODAL({
                      title: "ویرایش تخصیص به انجماد",
                      content: (
                        <SlaughterAllocateToFreezing
                          fetchItems={fetchApiData}
                          updateTable={updateInventory}
                          isEdit
                          item={item}
                        />
                      ),
                    })
                  );
                }}
              >
                <EditIcon />
              </IconButton>
            </Tooltip>
          </Grid>
        </div>
      </Popover>
    </div>
  );
};
