import React, { useContext } from "react";
import { Yup } from "../../../../lib/yup/yup";
import { useFormik } from "formik";
import { Grid } from "../../../../components/grid/Grid";
import { Button, TextField } from "@mui/material";
import {
  CLOSE_MODAL,
  LOADING_END,
  LOADING_START,
} from "../../../../lib/redux/slices/appSlice";
import { SPACING } from "../../../../data/spacing";
import { useDispatch } from "react-redux";
import { vetFarmSubmitContradictsService } from "../../services/vet-farm-submit-contdadicts";
import { AppContext } from "../../../../contexts/AppContext";
import { getRoleFromUrl } from "../../../../utils/getRoleFromUrl";

export const VetFarmSubmitContradicts = ({ item, updateTable }) => {
  const [openNotif] = useContext(AppContext);
  const dispatch = useDispatch();
  const formik = useFormik({
    initialValues: {
      realNumber: "",
    },
    validationSchema: Yup.object({
      realNumber: Yup.string()
        .typeError("لطفا فیلد را به درستی وارد کنید.!")
        .required("این فیلد اجباری است!"),
    }),
  });

  return (
    <>
      <Grid
        xs={12}
        container
        spacing={SPACING.SMALL}
        alignItems="center"
        justifyContent="center"
      >
        <Grid xs={10}>
          <TextField
            fullWidth
            id="realNumber"
            size="small"
            label="مقدار واقعی"
            variant="outlined"
            value={formik.values.realNumber}
            error={
              formik.touched.realNumber
                ? Boolean(formik.errors.realNumber)
                : null
            }
            onChange={formik.handleChange}
            onBlur={formik.handleBlur}
            helperText={
              formik.touched.realNumber && Boolean(formik.errors.realNumber)
                ? formik.errors.realNumber
                : null
            }
          />
        </Grid>
        <Grid xs={2}>
          <Button
            size="small"
            variant="outlined"
            onClick={() => {
              dispatch(LOADING_START());
              dispatch(
                vetFarmSubmitContradictsService({
                  key: item.key,
                  quarantine_quantity: formik.values.realNumber,
                })
              ).then((r) => {
                dispatch(LOADING_END());
                if (r.payload.error) {
                  openNotif({
                    vertical: "top",
                    horizontal: "center",
                    msg: "مشکلی بوجود آمده است!",
                    severity: "error",
                  });
                } else {
                  openNotif({
                    vertical: "top",
                    horizontal: "center",
                    msg: "عملیات با موفقیت انجام شد.",
                    severity: "success",
                  });
                  dispatch(CLOSE_MODAL());
                  updateTable();
                }
              });
            }}
          >
            ثبت
          </Button>
        </Grid>
      </Grid>
      <Grid
        xs={12}
        container
        gap={SPACING.SMALL}
        alignItems="center"
        justifyContent="center"
      >
        <Button
          variant="outlined"
          onClick={() => {
            dispatch(LOADING_START());
            dispatch(
              vetFarmSubmitContradictsService({
                key: item.key,
                quarantine_code_state: "noclearance",
                role: getRoleFromUrl(),
              })
            ).then((r) => {
              dispatch(LOADING_END());
              if (r.payload.error) {
                openNotif({
                  vertical: "top",
                  horizontal: "center",
                  msg: "مشکلی بوجود آمده است!",
                  severity: "error",
                });
              } else {
                openNotif({
                  vertical: "top",
                  horizontal: "center",
                  msg: "عملیات با موفقیت انجام شد.",
                  severity: "success",
                });
                dispatch(CLOSE_MODAL());
                updateTable();
              }
            });
          }}
        >
          فاقد کد رهگیری
        </Button>
        <Button
          variant="outlined"
          onClick={() => {
            dispatch(LOADING_START());
            dispatch(
              vetFarmSubmitContradictsService({
                key: item.key,
                quarantine_code_state: "contradiction",
                role: getRoleFromUrl(),
              })
            ).then((r) => {
              dispatch(LOADING_END());
              if (r.payload.error) {
                openNotif({
                  vertical: "top",
                  horizontal: "center",
                  msg: "مشکلی بوجود آمده است!",
                  severity: "error",
                });
              } else {
                openNotif({
                  vertical: "top",
                  horizontal: "center",
                  msg: "عملیات با موفقیت انجام شد.",
                  severity: "success",
                });
                dispatch(CLOSE_MODAL());
                updateTable();
              }
            });
          }}
        >
          مغایرت کد رهگیری
        </Button>
        <Button
          variant="outlined"
          onClick={() => {
            dispatch(LOADING_START());
            dispatch(
              vetFarmSubmitContradictsService({
                key: item.key,
                quarantine_code_state: "notconfirmed",
                role: getRoleFromUrl(),
              })
            ).then((r) => {
              dispatch(LOADING_END());
              if (r.payload.error) {
                openNotif({
                  vertical: "top",
                  horizontal: "center",
                  msg: "مشکلی بوجود آمده است!",
                  severity: "error",
                });
              } else {
                openNotif({
                  vertical: "top",
                  horizontal: "center",
                  msg: "عملیات با موفقیت انجام شد.",
                  severity: "success",
                });
                dispatch(CLOSE_MODAL());
                updateTable();
              }
            });
          }}
        >
          عدم تایید راهداری
        </Button>
      </Grid>
    </>
  );
};
