import { Button, Typography } from "@mui/material";
import { useContext } from "react";
import { useDispatch, useSelector } from "react-redux";
import { Grid } from "../../../../components/grid/Grid";
import { AppContext } from "../../../../contexts/AppContext";
import { CLOSE_MODAL, OPEN_MODAL } from "../../../../lib/redux/slices/appSlice";
import { slaughterDeleteAllocatedService } from "../../services/salughter-delete-allocated";
import { slaughterGetUpdatedInventoryStock } from "../../services/salughter-get-updated-inventory-stock";
import { slaughterManageInventoryAllocationsService } from "../../services/salughter-manage-inventory-allocations";
import { slaughterGetKillhouseGuildsService } from "../../services/slaughter-get-killhouse-guilds";
import { slaughterGetKillhouseStewardsService } from "../../services/slaughter-get-killhouse-stewards";
import { SlaughterAddSteward } from "../slaughter-add-steward/SlaughterAddSteward";
import { SubmitAuthCode } from "../submit-auth-code/SubmitAuthCode";
import { slaughterInventoryFinalSubmitService } from "../../services/slaughter-inventory-final-submit";
import { SPACING } from "../../../../data/spacing";

export const SlaughterManageInventoryAllocationOperationsTest = ({
  item,
  disabled,
}) => {
  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 || disabled
          }
          onClick={() => {
            dispatch(
              OPEN_MODAL({
                title: "کداحراز",
                content: <SubmitAuthCode 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(
                            slaughterInventoryFinalSubmitService({
                              steward_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(
                                slaughterManageInventoryAllocationsService({
                                  kill_house_key: inventorySelectedKillHouse,
                                  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: (
                    <SlaughterAddSteward
                      item={item}
                      quantity={item.numberOfCarcasses}
                      weight={item.weightOfCarcasses}
                      stewardKey={item.key}
                    />
                  ),
                })
              );
            }}
          >
            ویرایش
          </Button>
          <Button
            size="small"
            disabled={item.finalRegistration}
            color="error"
            onClick={() => {
              dispatch(
                slaughterDeleteAllocatedService({
                  steward_allocation_key: item.key,
                })
              ).then((r) => {
                dispatch(
                  slaughterGetKillhouseStewardsService({
                    kill_house_key: inventorySelectedKillHouse,
                    date: selectedDate1,
                  })
                );
                dispatch(
                  slaughterGetUpdatedInventoryStock({
                    kill_house_key: inventorySelectedKillHouse,
                    date: selectedDate1,
                  })
                );
                dispatch(
                  slaughterManageInventoryAllocationsService({
                    kill_house_key: inventorySelectedKillHouse,
                    date: selectedDate1,
                  })
                );
                dispatch(
                  slaughterGetKillhouseGuildsService({
                    kill_house_key: inventorySelectedKillHouse,
                    date: selectedDate1,
                  })
                );
              });
            }}
          >
            حذف
          </Button>
        </>
      )}
    </Grid>
  );
};
