import React, { useEffect, useState } from "react";
import { Input, Modal, Spin } from "antd";
import { useModalState } from "../../../../../../zustand/Modal";
import { ModalEnum } from "../../../../../../enums/Model";
import { useObjectToEdit } from "../../../../../../zustand/ObjectToEditState";

import { useDeleteNote } from "../../../../../../api/note";
import { useTranslation } from "react-i18next";

const ModalForm: React.FC = () => {
  const { isOpen, setIsOpen } = useModalState((state) => state);
  const [inputValue, setInputValue] = useState("");

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

  // console.log(object_to_edit,"object_to_edit");
  const [t] = useTranslation();
  useEffect(() => {
    if (isSuccess) {
      setIsOpen("");
      setInputValue("");
    }
  }, [isSuccess, setIsOpen]);

  const handleSubmit = () => {
    mutate({
      id: Number(object_to_edit?.id),
    });
  };

  const handleCancel = () => {
    setInputValue("");
    setIsOpen("");
    set_object_to_edit({});
  };
  console.log(object_to_edit, "object_to_edit");

  const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
    // Step 2: Handle changes to the input field
    setInputValue(e.target.value);
  };

  return (
    <>
      <Modal
        className="ModalForm"
        centered
        width={"40vw"}
        footer={null}
        open={isOpen === ModalEnum?.NOTES_DELETE}
        onCancel={handleCancel}
      >
        <header>
          {t("practical.delete")} (
          {object_to_edit?.registration_record?.fullName}){" "}
        </header>
        <main className="main_modal">
          <div className="ValidationField w-100 mb-5">
            <label className="text ">
              {t("practical.to_confirm_deletion_please_re_enter")}{" "}
              {t("header.student_name")}
            </label>

            <Input
              size="large"
              type="text"
              placeholder={t("header.student_name")}
              value={inputValue}
              onChange={handleChange}
            />
          </div>

          <div className="buttons">
            <div onClick={handleCancel}>{t("practical.cancel")}</div>
            <button
              className={
                object_to_edit?.registration_record?.fullName !== inputValue
                  ? "disabled_button"
                  : ""
              }
              disabled={
                object_to_edit?.registration_record?.fullName !== inputValue ||
                isLoading
              }
              onClick={handleSubmit}
            >
              {t("practical.delete")}
              {isLoading && (
                <span className="Spinier_Div">
                  <Spin />
                </span>
              )}
            </button>
          </div>
        </main>
      </Modal>
    </>
  );
};

export default ModalForm;
