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 } from "react-redux";
import { Grid } from "../../../../components/grid/Grid";
import { OPEN_MODAL } from "../../../../lib/redux/slices/appSlice";
import { ProvinceDispenserEditSaleOut } from "../province-dispenser-edit-sale-out/ProvinceDispenserEditSaleOut";
import { provinceDispenserDeleteService } from "../../../slaughter-house/services/province-dispenser-delete-sale-out";

export const ProvinceDispenserSaleOutOperation = ({
  fetchApiData,
  item,
  fetchData,
  updateTable,
  priceInfo,
  fetchDashboardData,
}) => {
  const [anchorEl, setAnchorEl] = useState(null);
  const dispatch = useDispatch();

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

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

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

  const handleDeleteClick = () => {
    dispatch(
      provinceDispenserDeleteService({
        key: item.key,
      })
    ).then(() => {
      fetchDashboardData();
      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>
  );
};
