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 {
  provincePolicyEditValidatePoultryWithSmsStatusService,
  provincePolicyValidatePoultryWithSmsStatusService,
} from "../../services/province-policy-validate-poultry-wth-sms-service";

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

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

  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(
                  provincePolicyEditValidatePoultryWithSmsStatusService({
                    poultry_status: 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(
                  provincePolicyEditValidatePoultryWithSmsStatusService({
                    poultry_status: 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>
  );
};
