import React, { useState } from "react";
import {
  IconButton,
  List,
  ListItemButton,
  ListItemIcon,
  ListItemText,
  Popover,
} from "@mui/material";
import TuneIcon from "@mui/icons-material/Tune";
import EditOutlinedIcon from "@mui/icons-material/EditOutlined";
import DeleteOutlineIcon from "@mui/icons-material/DeleteOutline";
import { useDispatch, useSelector } from "react-redux";
import { Grid } from "../../../../components/grid/Grid";
import { OPEN_MODAL } from "../../../../lib/redux/slices/appSlice";
import { ProvinceStewardEditSaleOutOperation } from "../province-steward-edit-sale-out-operation/ProvinceStewardEditSaleOutOperation";
import { stewardDeleteOutSellService } from "../../../guild/services/steward-sell-out-delete-service";
import { fetchSlaughterBroadcastAndProducts } from "../../../slaughter-house/services/handle-fetch-slaughter-products";

export const ProvinceDispenserStewardSaleOutOperation = ({
  fetchApiData,
  item,
  fetchData,
  updateTable,
  priceInfo,
  fetchDashboardData,
  remainWeight,
}) => {
  const [anchorEl, setAnchorEl] = useState(null);
  const dispatch = useDispatch();
  const selectedSubUser = useSelector(
    (state) => state.userSlice.selectedSubUser
  );
  const handleClick = (event) => {
    setAnchorEl(event.currentTarget);
  };

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

  const handleEditClick = () => {
    handleClose();
    dispatch(
      OPEN_MODAL({
        title: "ویرایش تخصیص",
        content: (
          <ProvinceStewardEditSaleOutOperation
            fetchDashboardData={fetchDashboardData}
            fetchData={fetchData}
            updateTable={updateTable}
            fetchApiData={fetchApiData}
            editData={item}
            priceInfo={priceInfo}
            remainWeight={remainWeight}
          />
        ),
      })
    );
  };

  const handleDeleteClick = () => {
    handleClose();
    dispatch(stewardDeleteOutSellService(item?.key)).then(() => {
      dispatch(
        fetchSlaughterBroadcastAndProducts({
          role_key: selectedSubUser?.key || "",
        })
      );
      fetchApiData(1);
      setAnchorEl(null);
    });
  };

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

  return (
    <Grid container>
      <IconButton
        disabled={item?.registrationCode}
        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}
      >
        <List sx={{ py: 0 }}>
          <ListItemButton onClick={handleEditClick}>
            <ListItemIcon>
              <EditOutlinedIcon fontSize="small" color="primary" />
            </ListItemIcon>
            <ListItemText
              primary="ویرایش"
              primaryTypographyProps={{
                variant: "body2",
                color: "primary",
              }}
            />
          </ListItemButton>
          <ListItemButton
            disabled={item?.registrationCode}
            onClick={handleDeleteClick}
          >
            <ListItemIcon>
              <DeleteOutlineIcon fontSize="small" color="error" />
            </ListItemIcon>
            <ListItemText
              primary="حذف"
              primaryTypographyProps={{
                variant: "body2",
                color: item?.registrationCode ? "text.secondary" : "error",
              }}
            />
          </ListItemButton>
        </List>
      </Popover>
    </Grid>
  );
};
