import { Button, IconButton, Popover, Tooltip } from "@mui/material";
import { useContext, useState } from "react";
import TuneIcon from "@mui/icons-material/Tune";
import { Grid } from "../../../../components/grid/Grid";
import EditIcon from "@mui/icons-material/Edit";
import DeleteIcon from "@mui/icons-material/Delete";
import {
  CLOSE_MODAL,
  DRAWER,
  OPEN_MODAL,
} from "../../../../lib/redux/slices/appSlice";
import { ProvinceWagePaymentCreateTransaction } from "../province-wage-payment-create-transaction/ProvinceWagePaymentCreateTransaction";
import { useDispatch } from "react-redux";
import { AppContext } from "../../../../contexts/AppContext";
import { provinceWagePaymentDeleteTransactionService } from "../../services/province-wage-payment-create-transaction-service";

export const ProvinceWagePaymentTransactionsOperations = ({
  item,
  fetchApiData,
}) => {
  const dispatch = useDispatch();
  const [anchorEl, setAnchorEl] = useState(null);
  const handleClick = (event) => {
    setAnchorEl(event.currentTarget);
  };

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

  const [openNotif] = useContext(AppContext);

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

  // const dispatch = useDispatch();

  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"
            alignItems="center"
            justifyContent="center"
          >
            <Tooltip title="ویرایش" placement="right">
              <IconButton
                color="primary"
                onClick={() => {
                  handleClose();
                  dispatch(
                    DRAWER({
                      title: "ویرایش تراکنش",
                      right: !(window.innerWidth <= 600),
                      bottom: window.innerWidth <= 600,
                      content: (
                        <ProvinceWagePaymentCreateTransaction
                          fetchApiData={fetchApiData}
                          item={item}
                          isEdit
                        />
                      ),
                    })
                  );
                }}
              >
                <EditIcon />
              </IconButton>
            </Tooltip>
            <Tooltip title={"حذف تراکنش"} placement="right">
              <IconButton
                aria-label="delete"
                color="error"
                onClick={() => {
                  handleClose();
                  dispatch(
                    OPEN_MODAL({
                      title: "آیا مطمئن هستید؟",
                      content: (
                        <Grid container gap={2}>
                          <Button
                            variant="contained"
                            onClick={() => {
                              dispatch(
                                provinceWagePaymentDeleteTransactionService(
                                  item?.key
                                )
                              ).then((r) => {
                                if (r.payload.error) {
                                  openNotif({
                                    vertical: "top",
                                    horizontal: "center",
                                    msg: r.payload.error,
                                    severity: "error",
                                  });
                                } else {
                                  dispatch(CLOSE_MODAL());
                                  fetchApiData(1);
                                  openNotif({
                                    vertical: "top",
                                    horizontal: "center",
                                    msg: "عملیات با موفقیت انجام شد.",
                                    severity: "success",
                                  });
                                }
                              });
                            }}
                          >
                            تایید
                          </Button>
                          <Button
                            variant="contained"
                            color="error"
                            onClick={() => {
                              dispatch(CLOSE_MODAL());
                            }}
                          >
                            لغو
                          </Button>
                        </Grid>
                      ),
                    })
                  );
                }}
              >
                <DeleteIcon />
              </IconButton>
            </Tooltip>
          </Grid>
        </div>
      </Popover>
    </div>
  );
};
