import React, { useEffect } from "react";
import { Modal, Spin } from "antd";
import { useModalState } from "../../../../zustand/Modal";
import FormikForm from "../../../../Layout/Dashboard/FormikFormModel";
import ModelBody from "./SendNotificationEduClass";
import {
  getInitialValues,
  getValidationSchema,
} from "../../show/model/formUtil";
import { ModalEnum } from "../../../../enums/Model";
import { useParams } from "react-router-dom";
import { useAddNote } from "../../../../api/note";
import { useObjectToEdit } from "../../../../zustand/ObjectToEditState";
import { useTranslation } from "react-i18next";

const ModalForm: React.FC = () => {
  const { isOpen, setIsOpen } = useModalState((state) => state);
  const { edu_class_id } = useParams();
  const { mutate, isSuccess, isLoading } = useAddNote();
  const { set_object_to_edit } = useObjectToEdit();

  useEffect(() => {
    if (isSuccess) {
      setIsOpen("");
      set_object_to_edit({});
    }
  }, [setIsOpen, isSuccess, set_object_to_edit]);

  const handleSubmit = (values: any) => {
    //   console.log(values,"values");
    const Data_To_Send = { ...values };
    Data_To_Send["date"] = values.date?.format("YYYY-MM-DD");

    mutate({
      ...values,
      edu_class_ids: [edu_class_id],
      attachments: [values?.attachments],
    });
  };

  const handleCancel = () => {
    setIsOpen("");
    set_object_to_edit({});
  };
  const [t] = useTranslation();

  return (
    <>
      <Modal
        className="ModalForm"
        centered
        width={"60vw"}
        footer={null}
        open={isOpen === ModalEnum?.EDUClASS_SENDNOTIFICATION}
        onCancel={handleCancel}
      >
        <FormikForm
          handleSubmit={handleSubmit}
          initialValues={getInitialValues([])}
          validationSchema={getValidationSchema}
        >
          <header>
            {t("practical.send_notification_to_education_class")}{" "}
          </header>
          <main className="main_modal w-100">
            <ModelBody />
            <div className="buttons">
              <div onClick={handleCancel}>{t("practical.back")}</div>
              <button disabled={isLoading} type="submit">
                {t("practical.add")}
                {isLoading && (
                  <span className="Spinier_Div">
                    <Spin />
                  </span>
                )}
              </button>
            </div>
          </main>
        </FormikForm>
      </Modal>
    </>
  );
};

export default ModalForm;
