import React, { useContext, useEffect, useState } from "react";
import { Grid } from "../../../../components/grid/Grid";
import { FormControlLabel, Radio, Typography } from "@mui/material";
import { useDispatch } from "react-redux";
import { AppContext } from "../../../../contexts/AppContext";
import {
  provincePolicyEditSlaughterSubmitBuyingPriceStatus,
  provincePolicyGetSLaughterSubmitBuyingPriceStatus,
} from "../../services/province-policy-get-slaughter-buying-price-status";

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

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

  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(
                  provincePolicyEditSlaughterSubmitBuyingPriceStatus({
                    allow: 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(
                  provincePolicyEditSlaughterSubmitBuyingPriceStatus({
                    allow: 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>
  );
};
