import { useContext, useEffect, useState } from "react";
import { useDispatch } from "react-redux";
import { AppContext } from "../../../../contexts/AppContext";
import {
  provincePolicyGEditUploadImageService,
  provincePolicyGetUploadImageService,
} from "../../services/province-policy-upload-image";
import { Grid } from "../../../../components/grid/Grid";
import { FormControlLabel, Radio, Typography } from "@mui/material";

export const ProvincePolicyUploadImageStewardOutProvince = () => {
  const [submitStatus, SetSubmitStatus] = useState(false);
  const dispatch = useDispatch();
  const [openNotif] = useContext(AppContext);

  const fetchData = () => {
    dispatch(provincePolicyGetUploadImageService()).then((r) => {
      SetSubmitStatus(r.payload.data.steward_free_sale);
    });
  };

  useEffect(() => {
    fetchData();
  }, []);

  return (
    <Grid
      container
      alignItems="center"
      gap={2}
      justifyContent="space-between"
      isPolicy
      sx={{
        width: {
          xs: "100%",
          sm: "48%",
          lg: "24%",
          nxl: "19%",
        },
      }}
    >
      <Grid>
        <Typography variant="body1">سند توزیع خارج استان مباشر</Typography>
      </Grid>
      <Grid container xs={12} justifyContent="space-between">
        <FormControlLabel
          control={
            <Radio
              name="item6"
              checked={submitStatus}
              onClick={() => {
                SetSubmitStatus(true);
                dispatch(
                  provincePolicyGEditUploadImageService({
                    steward_free_sale: true,
                  })
                ).then((r) => {
                  if (r.payload.error) {
                    openNotif({
                      vertical: "top",
                      horizontal: "center",
                      msg: r.payload.error,
                      severity: "error",
                    });
                  } else {
                    fetchData();
                    openNotif({
                      vertical: "top",
                      horizontal: "center",
                      msg: "عملیات با موفقیت انجام شد.",
                      severity: "success",
                    });
                  }
                });
              }}
            />
          }
          label="اجباری"
        />
        <FormControlLabel
          control={
            <Radio
              checked={!submitStatus}
              name="item6"
              onClick={() => {
                SetSubmitStatus(false);
                dispatch(
                  provincePolicyGEditUploadImageService({
                    steward_free_sale: false,
                  })
                ).then((r) => {
                  if (r.payload.error) {
                    openNotif({
                      vertical: "top",
                      horizontal: "center",
                      msg: r.payload.error,
                      severity: "error",
                    });
                  } else {
                    fetchData();
                    openNotif({
                      vertical: "top",
                      horizontal: "center",
                      msg: "عملیات با موفقیت انجام شد.",
                      severity: "success",
                    });
                  }
                });
              }}
            />
          }
          label="اختیاری"
        />
      </Grid>
    </Grid>
  );
};
