import { Button, TextField } from "@mui/material";
import React, { useContext, useState } from "react";
import { Grid } from "../../../../components/grid/Grid";
import { useDispatch } from "react-redux";
import { AppContext } from "../../../../contexts/AppContext";
import { SPACING } from "../../../../data/spacing";
import { adminFreeBuyEditPayment } from "../../services/admin-free-buy-edit-payment";
import { CLOSE_MODAL } from "../../../../lib/redux/slices/appSlice";

export const ProvinceSettlementExtendSettlement = ({
  updateTable_data,
  item,
}) => {
  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
        type="number"
        label={"تعداد روز"}
        variant="outlined"
        fullWidth
        value={text}
        onChange={handleTextChange}
      />
      <Button
        disabled={!text}
        variant="contained"
        color="primary"
        onClick={() => {
          dispatch(
            adminFreeBuyEditPayment({
              key: item.key,
              extension_payment_deadline_days: parseInt(text),
            })
          ).then((r) => {
            if (r.error) {
              openNotif({
                vertical: "top",
                horizontal: "center",
                msg: r.error,
                severity: "error",
              });
            } else {
              updateTable_data();
              openNotif({
                vertical: "top",
                horizontal: "center",
                msg: "درخواست با موفقیت رد شد",
                severity: "success",
              });
              dispatch(CLOSE_MODAL());
            }
          });
        }}
      >
        تمدید مهلت تسویه
      </Button>
    </Grid>
  );
};
