import { Button, TextField, Typography } from "@mui/material";
import { useContext, useState } from "react";
import { useDispatch } from "react-redux";
import { AppContext } from "../../../../contexts/AppContext";
import { Grid } from "../../../../components/grid/Grid";
import { SPACING } from "../../../../data/spacing";
import { DRAWER } from "../../../../lib/redux/slices/appSlice";
import { provinceCheckChainAllocationService } from "../../services/province-check-chain-allocation";

export const ProvinceCheckChainAllocation = ({ item, fetchApiData }) => {
  const dispatch = useDispatch();
  const [openNotif] = useContext(AppContext);
  const [value, setValue] = useState("");

  const handleChange = (event) => {
    setValue(event.target.value);
  };

  return (
    <Grid container direction="column">
      <Grid>
        <Typography variant="h6" gutterBottom>
          اطلاعات مرغدار
        </Typography>
        <Typography variant="subtitle1" gutterBottom>
          نام: {item?.poultryHatching?.poultry?.unitName} (
          {item?.poultryHatching?.poultry?.user?.mobile})
        </Typography>
        <Typography variant="subtitle1" gutterBottom>
          شرکت زنجیره:{" "}
          {`${item?.chainCompany?.name} (${item?.chainCompany?.user.mobile})`}
        </Typography>
        <Typography variant="subtitle1" gutterBottom>
          نوع تخصیص: {item?.outProvince ? "خارج استان" : "داخل استان"}
        </Typography>
        <Typography variant="subtitle1" gutterBottom>
          خریدار:{" "}
          {item?.killHouse?.killHouseOperator?.user.fullname
            ? `${item?.killHouse?.killHouseOperator?.user.fullname} (${item?.killHouse?.killHouseOperator?.user.mobile})`
            : `${item?.buyerName} (${item?.buyerMobile})`}
        </Typography>
      </Grid>

      <Grid container gap={SPACING.TINY} mt={SPACING.SMALL}>
        <TextField
          label="یادداشت"
          variant="outlined"
          fullWidth
          multiline
          rows={4}
          value={value}
          onChange={handleChange}
        />
        <Button
          style={{ flex: 0.5 }}
          variant="contained"
          color="success"
          onClick={() => {
            dispatch(
              provinceCheckChainAllocationService({
                chain_allcation_key: item?.key,
                check_allocation: true,
                state: "accepted",
              })
            ).then((r) => {
              fetchApiData(1);
              if (r.payload.error) {
                openNotif({
                  vertical: "top",
                  horizontal: "center",
                  msg: r.payload.error,
                  severity: "error",
                });
              } else {
                dispatch(
                  DRAWER({ right: false, bottom: false, content: null })
                );
                openNotif({
                  vertical: "top",
                  horizontal: "center",
                  msg: "عملیات با موفقیت انجام شد.",
                  severity: "success",
                });
              }
            });
          }}
        >
          تایید
        </Button>
        <Button
          style={{ flex: 0.5 }}
          variant="contained"
          color="error"
          onClick={() => {
            dispatch(
              provinceCheckChainAllocationService({
                chain_allcation_key: item?.key,
                check_allocation: true,
                state: "rejected",
              })
            ).then((r) => {
              fetchApiData(1);
              if (r.payload.error) {
                openNotif({
                  vertical: "top",
                  horizontal: "center",
                  msg: r.payload.error,
                  severity: "error",
                });
              } else {
                dispatch(
                  DRAWER({ right: false, bottom: false, content: null })
                );
                openNotif({
                  vertical: "top",
                  horizontal: "center",
                  msg: "عملیات با موفقیت انجام شد.",
                  severity: "success",
                });
              }
            });
          }}
        >
          رد
        </Button>
      </Grid>
    </Grid>
  );
};
