import React, { useEffect }  from "react";
import { Modal, Spin } from "antd";
import FormikForm from "../../../Layout/Dashboard/FormikFormModel";
import ModelBody from "./Add";
import { getInitialValues, getValidationSchema } from "./formUtil";
import { useAddQuestion } from "../../../api/Question";
import { useQueryClient } from "react-query";
import { useTranslation } from "react-i18next";
import { useNavigate, useParams } from "react-router-dom";
import { ParamsEnum } from "../../../enums/params";
import { useObjectToEdit } from "../../../zustand/ObjectToEditState";

const ModalForm: React.FC = () => {

  const queryClient = useQueryClient();

  const { mutate, isSuccess, isLoading } = useAddQuestion();
  const {object_to_edit} = useObjectToEdit()

  const {subject_id} = useParams<ParamsEnum>()


  const handleSubmit = (values: any) => {
    console.log(values,"values");
    
    mutate({ ...values,   subject_id:subject_id });
  };
  const navigate = useNavigate()
  useEffect(() => {
      if(isSuccess){
        navigate(-1)

      }
  }, [isSuccess])
  
  const handleCancel = () => {
    navigate(-1)
  };

  const [t] = useTranslation();

  return (
    <div className="exercise_add">

        <FormikForm
          handleSubmit={handleSubmit}
          initialValues={getInitialValues(object_to_edit)}
          validationSchema={getValidationSchema}
        >
        
          <main className="w-100 exercise_add_main">
          <header className="exercise_add_header mb-4">
            {" "}
            {t("practical.add")} {t("models.exercise")}{" "}
          </header>
            <ModelBody />
            <div className="exercise_add_buttons">
              <div onClick={handleCancel}>{t("practical.back")}</div>
              <button disabled={isLoading} className="relative" type="submit">
                {t("practical.add")}

                {isLoading && (
                  <span className="Spinier_Div">
                    <Spin />
                  </span>
                )}
              </button>
            </div>
          </main>
        </FormikForm>

    </div>
  );
};

export default ModalForm;
