import React, { useEffect } from "react";
import { Modal, Spin } from "antd";
import { useModalState } from "../../../zustand/Modal";
import FormikForm from "../../../Layout/Dashboard/FormikFormModel";
import ModelBody from "./Edit";
import { getInitialValues, getValidationSchema } from "./formUtil";
import { ModalEnum } from "../../../enums/Model";
import { useObjectToEdit } from "../../../zustand/ObjectToEditState";
import { useUpdateTag } from "../../../api/tags";
import { useTranslation } from "react-i18next";
import ModelButtons from "../../../Components/models/ModelButtons";

const ModalForm: React.FC = () => {
  const { isOpen, setIsOpen } = useModalState((state) => state);
  const { objectToEdit, setObjectToEdit } = useObjectToEdit((state) => state);
  const { mutate, isSuccess, isLoading } = useUpdateTag();
  // console.log(objectToEdit,"objectToEdit");
  const handleSubmit = (values: any) => {
    // const contactInformationJson = JSON.stringify({
    //     phone_number: values?.number
    // });
    mutate({
      ...values,
    });
  };
  const handleCancel = () => {
    setIsOpen("");
    setObjectToEdit({});
  };

  useEffect(() => {
    if (isSuccess) {
      setIsOpen("");
    }
  }, [setIsOpen, isSuccess]);
  const [t] = useTranslation();
  return (
    <Modal
      className="ModalForm"
      centered
      width={"60vw"}
      footer={null}
      open={isOpen === ModalEnum?.TAGS_EDIT}
      onCancel={handleCancel}
    >
      {objectToEdit && (
        <FormikForm
          handleSubmit={handleSubmit}
          initialValues={getInitialValues(objectToEdit)}
          validationSchema={getValidationSchema}
        >
          <header>
            {" "}
            {t("practical.edit")} {t("practical.details")} {t("models.tags")}{" "}
          </header>
          <main className="main_modal w-100">
            <ModelBody />
            <ModelButtons isLoading={isLoading} />
          </main>
        </FormikForm>
      )}
    </Modal>
  );
};

export default ModalForm;
