import { Button, Typography } from "@mui/material";
import { useDispatch } from "react-redux";
import { GuildAddSteward } from "./GuildAddSteward";
import { Grid } from "../../../components/grid/Grid";
import {
  CLOSE_MODAL,
  LOADING_END,
  OPEN_MODAL,
} from "../../../lib/redux/slices/appSlice";
import { guildGetAllocationData } from "../services/guildGetAllocationData";
import { guildGetStewards } from "../services/guild-get-stewards";
import { guildGetInventoryStockService } from "../services/guild-get-inventory-stock";
import { AppContext } from "../../../contexts/AppContext";
import { useContext } from "react";
import { guildDeleteAllocatedService } from "../services/guildDeleteAllocatedService";
import { SPACING } from "../../../data/spacing";
import { guildInventoryFinalSubmitService } from "../services/guildInventoryFinalSubmitService";
import { GuildSubmitAuthCode } from "./GuildSubmitAuthCode";

export const GuildManageInventoryAllocationOperations = ({ item }) => {
  const dispatch = useDispatch();
  const [openNotif] = useContext(AppContext);
  const [, , selectedDate1] = useContext(AppContext);
  //   const { inventorySelectedKillHouse } = useSelector(
  //     (state) => state.slaughterSlice
  //   );

  return (
    <Grid container direction="column">
      {item.systemRegistrationCode && (
        <Button
          size="small"
          disabled={item.finalRegistration || item.loggedRegistrationCode}
          onClick={() => {
            dispatch(
              OPEN_MODAL({
                title: "کداحراز",
                content: <GuildSubmitAuthCode item={item} />,
              })
            );
          }}
        >
          ثبت کداحراز
        </Button>
      )}
      {!item.systemRegistrationCode && (
        <Button
          size="small"
          disabled={item.finalRegistration}
          onClick={() => {
            dispatch(
              OPEN_MODAL({
                title: "ارسال کداحراز",
                content: (
                  <Grid container gap={SPACING.SMALL}>
                    <Typography>
                      در صورت ارسال کداحراز انجام هیچگونه عملیاتی مانند حذف و
                      ویرایش امکان پذیر نمی باشد.
                    </Typography>
                    <Grid
                      container
                      direction="column"
                      gap={SPACING.TINY}
                      width="100%"
                    >
                      <Button
                        fullWidth
                        variant="contained"
                        onClick={() => {
                          dispatch(
                            guildInventoryFinalSubmitService({
                              steward_guild_allocation_list: [item.key],
                            })
                          ).then((r) => {
                            dispatch(CLOSE_MODAL());
                            if (r.payload.error) {
                              openNotif({
                                vertical: "top",
                                horizontal: "center",
                                msg: r.payload.error,
                                severity: "error",
                              });
                            } else {
                              dispatch(
                                guildGetAllocationData({
                                  date: selectedDate1,
                                })
                              );
                              openNotif({
                                vertical: "top",
                                horizontal: "center",
                                msg: "عملیات با موفقیت انجام شد.",
                                severity: "success",
                              });
                            }
                          });
                        }}
                      >
                        تایید
                      </Button>
                      <Button
                        fullWidth
                        color="error"
                        variant="contained"
                        onClick={() => {
                          dispatch(CLOSE_MODAL());
                        }}
                      >
                        لغو
                      </Button>
                    </Grid>
                  </Grid>
                ),
              })
            );
          }}
        >
          ارسال کداحراز
        </Button>
      )}

      {!item.systemRegistrationCode && (
        <>
          <Button
            size="small"
            disabled={item.finalRegistration}
            onClick={() => {
              dispatch(
                OPEN_MODAL({
                  title: "ویرایش تخصیص",
                  content: (
                    <GuildAddSteward
                      stewardKey={item.key}
                      item={item}
                      quantity={item?.numberOfCarcasses}
                      weight={item?.weightOfCarcasses}
                    />
                  ),
                })
              );
            }}
          >
            ویرایش
          </Button>
          <Button
            size="small"
            disabled={item.finalRegistration}
            color="error"
            onClick={() => {
              dispatch(
                guildDeleteAllocatedService({
                  steward_allocation_key: item.key,
                })
              ).then((r) => {
                dispatch(LOADING_END());
                if (r.error) {
                  openNotif({
                    vertical: "top",
                    horizontal: "center",
                    msg: r.payload.error,
                    severity: "error",
                  });
                } else {
                  dispatch(
                    guildGetAllocationData({
                      date: selectedDate1,
                    })
                  );
                  dispatch(guildGetStewards({ date: selectedDate1 }));
                  dispatch(
                    guildGetAllocationData({
                      date: selectedDate1,
                    })
                  );
                  dispatch(
                    guildGetInventoryStockService({
                      date: selectedDate1,
                    })
                  );

                  openNotif({
                    vertical: "top",
                    horizontal: "center",
                    msg: "عملیات با موفقیت انجام شد.",
                    severity: "success",
                  });
                }
              });
            }}
          >
            حذف
          </Button>
        </>
      )}
    </Grid>
  );
};
