import { Button, TextField } from "@mui/material";
import { useContext, useState } from "react";
import { useDispatch } from "react-redux";
import { Grid } from "../../../../components/grid/Grid";
import { AppContext } from "../../../../contexts/AppContext";
import { SPACING } from "../../../../data/spacing";
import { CLOSE_MODAL } from "../../../../lib/redux/slices/appSlice";
import { provinceRejectSlaughterFreeBuyService } from "../../services/province-reject-slaughter-free-buy";

export const ProviceRejectFreeBuy = ({ item, updateTable }) => {
  const dispatch = useDispatch();
  const [openNotif] = useContext(AppContext);
  const [text, setText] = useState("");

  const handleTextChange = (event) => {
    setText(event.target.value);
  };

  return (
    <Grid container direction="column" gap={SPACING.TINY} width="100%">
      <TextField
        label="دلیل رد درخواست"
        variant="outlined"
        fullWidth
        value={text}
        onChange={handleTextChange}
      />
      <Button
        variant="contained"
        color="primary"
        onClick={() => {
          dispatch(
            provinceRejectSlaughterFreeBuyService({
              state: "rejected",
              kill_request_key: item.key,
              direct_buying_message: text,
            })
          ).then((r) => {
            if (r.error) {
              openNotif({
                vertical: "top",
                horizontal: "center",
                msg: r.error,
                severity: "error",
              });
            } else {
              updateTable();
              openNotif({
                vertical: "top",
                horizontal: "center",
                msg: "عملیات با موفقیت انجام شد.",
                severity: "success",
              });
              dispatch(CLOSE_MODAL());
            }
          });
        }}
      >
        ثبت رد درخواست
      </Button>
    </Grid>
  );
};
