import { IconButton, Popover, Tooltip } from "@mui/material";
import React, { useState } from "react";
import EditIcon from "@mui/icons-material/Edit";
import TuneIcon from "@mui/icons-material/Tune";
import { Grid } from "../../../../components/grid/Grid";
import { useDispatch } from "react-redux";
import { DRAWER } from "../../../../lib/redux/slices/appSlice";
import { ProvinceColdHousesListAddColdHouse } from "../province-cold-houses-list-add-cold-house/ProvinceColdHousesListAddColdHouse";

export const ProvinceStewardShowColdHousesListOperations = ({
  item,
  updateTable,
}) => {
  const [anchorEl, setAnchorEl] = useState(null);
  const dispatch = useDispatch();
  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="left" title="ویرایش سردخانه">
              <IconButton
                color="primary"
                onClick={() => {
                  handleClose();
                  dispatch(
                    DRAWER({
                      title: "ویرایش سردخانه",
                      right: !(window.innerWidth <= 600),
                      bottom: window.innerWidth <= 600,
                      content: (
                        <ProvinceColdHousesListAddColdHouse
                          isEdit
                          item={item}
                          updateTable={updateTable}
                        />
                      ),
                    })
                  );
                }}
              >
                <EditIcon />
              </IconButton>
            </Tooltip>
          </Grid>
        </div>
      </Popover>
    </div>
  );
};
