import {
  FormControlLabel,
  IconButton,
  Popover,
  Switch,
  Tooltip,
} 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 { DRAWER } from "../../../../lib/redux/slices/appSlice";
import { ProvinceJahadEditCooperative } from "../province-jahad-edit-cooperative/ProvinceJahadEditCooperative";
import { AppContext } from "../../../../contexts/AppContext";
import { provinceJahadEditCooperativeService } from "../../services/province-jahad-submit-cooperatives";

export const ProvinceJahadCooperativesOperations = ({
  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(
                  DRAWER({
                    title: "ویرایش اطلاعات  ",
                    right: !(window.innerWidth <= 600),
                    bottom: window.innerWidth <= 600,
                    content: (
                      <ProvinceJahadEditCooperative
                        updateTable={updateTable}
                        getDashboardData={getDashboardData}
                        item={item}
                      />
                    ),
                  })
                );
              }}
            >
              <EditIcon />
            </IconButton>
          </Tooltip>

          <FormControlLabel
            control={
              <Switch
                size="small"
                checked={item?.active}
                onChange={() => {
                  dispatch(
                    provinceJahadEditCooperativeService({
                      cooperative_key: item?.key,
                      active: !item?.active,
                    })
                  ).then((r) => {
                    if (r.payload.error) {
                      openNotif({
                        vertical: "top",
                        horizontal: "center",
                        msg: r.payload.error,
                        severity: "error",
                      });
                    } else {
                      updateTable();
                      openNotif({
                        vertical: "top",
                        horizontal: "center",
                        msg: "عملیات با موفقیت انجام شد.",
                        severity: "success",
                      });
                    }
                  });
                }}
                color="primary"
              />
            }
            label={item?.active ? "فعال" : "غیرفعال"}
            style={{
              justifyContent: "center",
              alignItems: "center",
              display: "flex",
            }}
          />

          {/* <Tooltip title={"حذف "} placement="left-start">
            <IconButton aria-label="delete" color="error">
              <DeleteIcon />
            </IconButton>
          </Tooltip> */}
        </div>
      </Popover>
    </Grid>
  );
};
