import React, { useContext } from "react";
import { Button, Checkbox, FormControlLabel, FormGroup } from "@mui/material";
import { useFormik } from "formik";
import * as Yup from "yup";
import { Grid } from "../../../../components/grid/Grid";
import { AppContext } from "../../../../contexts/AppContext";
import { CLOSE_MODAL } from "../../../../lib/redux/slices/appSlice";
import { useDispatch } from "react-redux";
import { slaughterHouseEditDispenserService } from "../../services/slaughter-house-submit-dispenser-service";

const validationSchema = Yup.object({
  free_sale: Yup.boolean(),
  free_guilds: Yup.boolean(),
  free_stewards: Yup.boolean(),
});

export const SlaughterHouseDispensersAccessLevel = ({ updateTable, item }) => {
  const [openNotif] = useContext(AppContext);
  const dispatch = useDispatch();
  const formik = useFormik({
    initialValues: {
      free_sale: item?.freeSale ? item?.freeSale : false,
      free_guilds: item?.freeGuilds ? item?.freeGuilds : false,
      free_stewards: item?.freeStewards ? item?.freeStewards : false,
    },
    validationSchema: validationSchema,
    onSubmit: (values) => {
      dispatch(
        slaughterHouseEditDispenserService({
          dispenser_key: item?.key,
          type: "update-acceess-level",
          free_sale: values.free_sale,
          free_guilds: values.free_guilds,
          free_stewards: values.free_stewards,
        })
      ).then((r) => {
        if (r.payload.error) {
          openNotif({
            vertical: "top",
            horizontal: "center",
            msg: r.payload.error,
            severity: "error",
          });
        } else {
          dispatch(CLOSE_MODAL());
          updateTable();
          openNotif({
            vertical: "top",
            horizontal: "center",
            msg: "عملیات با موفقیت انجام شد.",
            severity: "success",
          });
        }
      });
    },
  });

  return (
    <Grid container xs={12} direction="column">
      <form onSubmit={formik.handleSubmit}>
        <FormGroup>
          <FormControlLabel
            control={
              <Checkbox
                id="free_sale"
                name="free_sale"
                checked={formik.values.free_sale}
                onChange={formik.handleChange}
              />
            }
            label="اجازه فروش آزاد"
          />
          <FormControlLabel
            control={
              <Checkbox
                id="free_guilds"
                name="free_guilds"
                checked={formik.values.free_guilds}
                onChange={formik.handleChange}
              />
            }
            label="فروش به اصناف آزاد"
          />
          <FormControlLabel
            control={
              <Checkbox
                id="free_stewards"
                name="free_stewards"
                checked={formik.values.free_stewards}
                onChange={formik.handleChange}
              />
            }
            label="فروش به مباشرین آزاد"
          />
        </FormGroup>
        {formik.touched.free_stewards && formik.errors.free_stewards ? (
          <div style={{ color: "red" }}>{formik.errors.free_stewards}</div>
        ) : null}
        <Button
          fullWidth
          color="primary"
          variant="contained"
          type="submit"
          sx={{ marginTop: 2 }}
        >
          ویرایش
        </Button>
      </form>
    </Grid>
  );
};
