import {
  Button,
  IconButton,
  Popover,
  Tooltip,
  Typography,
} from "@mui/material";
import { useContext, useState } from "react";
import DeleteIcon from "@mui/icons-material/Delete";
import TuneIcon from "@mui/icons-material/Tune";
import EditIcon from "@mui/icons-material/Edit";
import { Grid } from "../../../../components/grid/Grid";
import { useDispatch } from "react-redux";
import { ProvinceJahadSubmitAllocation } from "../province-jahad-submit-allocation/ProvinceJahadSubmitAllocation";
import { CLOSE_MODAL, OPEN_MODAL } from "../../../../lib/redux/slices/appSlice";

import { provinceJahadDeleteAllocationService } from "../../services/province-jahad-submit-allocation";
import { AppContext } from "../../../../contexts/AppContext";

export const ProvinceJahadAllocationOperations = ({
  item,
  // fileUrl,
  updateTable,
  getDashboardData,
}) => {
  const [anchorEl, setAnchorEl] = useState(null);
  const dispatch = useDispatch();
  const handleClick = (event) => {
    setAnchorEl(event.currentTarget);
  };

  const [openNotif] = useContext(AppContext);

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

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

  return (
    <Grid container>
      <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", display: "flex", flexDirection: "column" }}
        >
          <Tooltip title={"ویرایش"} placement="left-start">
            <IconButton
              aria-label="delete"
              color="primary"
              onClick={() => {
                handleClose();
                dispatch(
                  OPEN_MODAL({
                    title: "ویرایش",
                    content: (
                      <ProvinceJahadSubmitAllocation
                        updateTable={updateTable}
                        getDashboardData={getDashboardData}
                        item={item}
                      />
                    ),
                  })
                );
              }}
            >
              <EditIcon />
            </IconButton>
          </Tooltip>

          <Tooltip title={"حذف "} placement="left-start">
            <IconButton
              aria-label="delete"
              color="error"
              onClick={() => {
                dispatch(
                  OPEN_MODAL({
                    title: "حذف تخصیص",
                    content: (
                      <Grid
                        container
                        xs={12}
                        justifyContent="center"
                        alignItems="center"
                      >
                        <Typography variant="body2" color="red">
                          آیا از حذف تخصیص اطمینان دارید؟
                        </Typography>
                        <Grid
                          container
                          xs={12}
                          mt={2}
                          justifyContent="center"
                          alignItems="center"
                          spacing={2}
                        >
                          <Grid item xs={6}>
                            <Button
                              onClick={() => {
                                dispatch(
                                  provinceJahadDeleteAllocationService(
                                    item?.key
                                  )
                                ).then((r) => {
                                  if (r.payload.error) {
                                    openNotif({
                                      vertical: "top",
                                      horizontal: "center",
                                      msg: r.payload.error,
                                      severity: "error",
                                    });
                                  } else {
                                    openNotif({
                                      vertical: "top",
                                      horizontal: "center",
                                      msg: "عملیات با موفقیت انجام شد.",
                                      severity: "success",
                                    });
                                    updateTable();
                                    getDashboardData();
                                    dispatch(CLOSE_MODAL());
                                  }
                                });
                              }}
                              variant="contained"
                              color="error"
                              fullWidth
                            >
                              تایید
                            </Button>
                          </Grid>
                          <Grid item xs={6}>
                            <Button
                              variant="contained"
                              color="primary"
                              fullWidth
                            >
                              رد
                            </Button>
                          </Grid>
                        </Grid>
                      </Grid>
                    ),
                  })
                );
              }}
            >
              <DeleteIcon />
            </IconButton>
          </Tooltip>

          {/* {getRoleFromUrl() === "Cooperative" && (
            <>
              <Button
                variant="outlined"
                size="small"
                onClick={() => {
                  handleClose();
                  dispatch(
                    OPEN_MODAL({
                      title: "ثبت مقدار دریافتی",
                      content: (
                        <ProvinceJahadUnionSubmitRealWeight
                          item={item}
                          updateTable={updateTable}
                          getDashboardData={getDashboardData}
                        />
                      ),
                    })
                  );
                }}
              >
                ثبت مقدار دریافتی
              </Button>
            </>
          )} */}
        </div>
      </Popover>
    </Grid>
  );
};
