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,
  getEditValidationSchema as getValidationSchema,
} from "./formUtil";
import { ModalEnum } from "../../../enums/Model";
import { useObjectToEdit } from "../../../zustand/ObjectToEditState";
import { useGetAllAdmin, useUpdateAdmin } from "../../../api/admin";
import { useTranslation } from "react-i18next";

const ModalForm: React.FC = () => {
  const { isOpen, setIsOpen } = useModalState((state) => state);
  const { object_to_edit, set_object_to_edit } = useObjectToEdit(
    (state) => state,
  );
  const { mutate, isSuccess, isLoading } = useUpdateAdmin();
  // console.log(object_to_edit,"object_to_edit");

  const { data } = useGetAllAdmin(
    { show: object_to_edit?.id },
    { enabled: !!object_to_edit?.id },
  );

  const handleSubmit = (values: any) => {
    console.table(values);
    set_object_to_edit(values);
    const branches = values?.selects
      ?.map((item: any) => {
        return {
          branch_id: item?.branch,
          role_id: item?.role,
        };
      })
      .filter((item: any) => item.branch_id !== null && item.role_id !== null);

    const dataToSend = { ...values };
    if (values?.status === true) {
      dataToSend["status"] = "active";
    }
    if (values?.status === false) {
      dataToSend["status"] = "suspend";
    }
    if (branches?.length > 0) {
      dataToSend["branches"] = branches;
    }

    console.table(dataToSend);
    mutate({
      ...dataToSend,
    });
  };

  const handleCancel = () => {
    setIsOpen("");
    set_object_to_edit({});
  };

  useEffect(() => {
    if (isSuccess) {
      setIsOpen("");
    }
  }, [setIsOpen, isSuccess]);
  const [t] = useTranslation();

  return (
    <Modal
      className="ModalForm"
      centered
      width={"60vw"}
      footer={null}
      open={isOpen === ModalEnum?.ADMIN_EDIT}
      onCancel={handleCancel}
    >
      {object_to_edit && (
        <FormikForm
          handleSubmit={handleSubmit}
          initialValues={getInitialValues(data)}
          validationSchema={getValidationSchema}
        >
          <header>
            {" "}
            {t("practical.edit")} {t("practical.details")} {t("models.admin")}{" "}
          </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.edit")}
                {isLoading && (
                  <span className="Spinier_Div">
                    <Spin />
                  </span>
                )}
              </button>
            </div>
          </main>
        </FormikForm>
      )}
    </Modal>
  );
};

export default ModalForm;
