import React, { useContext } from "react";
import { useFormik } from "formik";
import * as Yup from "yup";
import { Grid, TextField, Button } from "@mui/material";
import { SPACING } from "../../../../data/spacing";
import { AppContext } from "../../../../contexts/AppContext";
import { useDispatch } from "react-redux";
import { DRAWER } from "../../../../lib/redux/slices/appSlice";
import { provinceJahadSubmitShareService } from "../../services/province-jahad-submit-share";

export const ProvinceJahadEditShare = ({ updateTable, item }) => {
  const [openNotif] = useContext(AppContext);
  const dispatch = useDispatch();
  const validationSchema = Yup.object().shape({
    price: Yup.string().required("این فیلد الزامی است"),
    shipping_price: Yup.string().required("این فیلد الزامی است"),
    union_price: Yup.string().required("این فیلد الزامی است"),
    cooperative_price: Yup.string().required("این فیلد الزامی است"),
    company_price: Yup.string().required("این فیلد الزامی است"),
  });

  const formik = useFormik({
    initialValues: {
      price: item?.price || "",
      shipping_price: item?.shippingPrice || "",
      union_price: item?.unionPrice || "",
      company_price: item?.companyPrice || "",
      cooperative_price: item?.cooperativePrice || "",
    },
    validationSchema,
    enableReinitialize: true,
    onSubmit: (values) => {
      dispatch(
        provinceJahadSubmitShareService({
          share_key: item?.key,
          shipping_price: values.shipping_price,
          union_price: values.union_price,
          company_price: values.company_price,
          price: values.price,
          cooperative_price: values.cooperative_price,
        })
      ).then((r) => {
        if (r.payload.error) {
          openNotif({
            vertical: "top",
            horizontal: "center",
            msg: r.payload.error,
            severity: "error",
          });
        } else {
          openNotif({
            vertical: "top",
            horizontal: "center",
            msg: "عملیات با موفقیت انجام شد.",
            severity: "success",
          });
          dispatch(DRAWER({ right: false, bottom: false, content: null }));

          updateTable();
        }
      });
    },
  });

  return (
    <Grid container direction="column" gap={SPACING.SMALL}>
      <form onSubmit={formik.handleSubmit}>
        <Grid container direction="column" gap={SPACING.TINY}>
          <TextField
            name="price"
            label="قیمت پایه "
            fullWidth
            value={formik.values.price}
            onChange={formik.handleChange}
            onBlur={formik.handleBlur}
            error={formik.touched.price && Boolean(formik.errors.price)}
            helperText={formik.touched.price && formik.errors.price}
          />

          <TextField
            name="shipping_price"
            label="هزینه حمل و نقل"
            fullWidth
            value={formik.values.shipping_price}
            onChange={formik.handleChange}
            onBlur={formik.handleBlur}
            error={
              formik.touched.shipping_price &&
              Boolean(formik.errors.shipping_price)
            }
            helperText={
              formik.touched.shipping_price && formik.errors.shipping_price
            }
          />

          <TextField
            name="union_price"
            label="سهم اتحادیه"
            fullWidth
            value={formik.values.union_price}
            onChange={formik.handleChange}
            onBlur={formik.handleBlur}
            error={
              formik.touched.union_price && Boolean(formik.errors.union_price)
            }
            helperText={formik.touched.union_price && formik.errors.union_price}
          />

          <TextField
            name="cooperative_price"
            label="سهم تعاونی"
            fullWidth
            value={formik.values.cooperative_price}
            onChange={formik.handleChange}
            onBlur={formik.handleBlur}
            error={
              formik.touched.cooperative_price &&
              Boolean(formik.errors.cooperative_price)
            }
            helperText={
              formik.touched.cooperative_price &&
              formik.errors.cooperative_price
            }
          />
          <TextField
            name="company_price"
            label="سهم شرکت"
            fullWidth
            value={formik.values.company_price}
            onChange={formik.handleChange}
            onBlur={formik.handleBlur}
            error={
              formik.touched.company_price &&
              Boolean(formik.errors.company_price)
            }
            helperText={
              formik.touched.company_price && formik.errors.company_price
            }
          />

          <Button type="submit" variant="contained" color="primary">
            ثبت اطلاعات
          </Button>
        </Grid>
      </form>
    </Grid>
  );
};
