import React, { useEffect } from "react";
import { getInitialValues, getValidationSchema } from "./formUtil";
import { ModalEnum } from "../../../enums/Model";
import LayoutModel from "../../../Layout/Dashboard/LayoutModel";
import { QueryStatusEnum } from "../../../enums/QueryStatus";
import ModelForm from "./ModelForm";
import { useQueryClient } from "react-query";
import { useAddLesson } from "../../../api/lesson";
import { useParams } from "react-router-dom";
import { ParamsEnum } from "../../../enums/params";
import { useModalState } from "../../../zustand/Modal";

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

  const { isOpen, setIsOpen } = useModalState((state) => state);
  const queryClient = useQueryClient();
  const { mutate, isSuccess, status } = useAddLesson();

  const { unit_id } = useParams<ParamsEnum>();
  useEffect(() => {
    if (isSuccess) {
      setIsOpen("");
      queryClient.invalidateQueries(["Lesson"]);
    }
  }, [setIsOpen, isSuccess, queryClient]);

  const handleSubmit = (values: any) => {
    mutate({ ...values, unit_id: unit_id });
  };

  return (
    <>
      <LayoutModel
        status={status as QueryStatusEnum}
        ModelEnum={ModalEnum.LESSON_ADD}
        modelTitle="lesson"
        handleSubmit={handleSubmit}
        getInitialValues={getInitialValues({})}
        getValidationSchema={getValidationSchema}
      >
        <ModelForm />
      </LayoutModel>
    </>
  );
};

export default AddModel;
