import { ModalEnum } from "../../../enums/Model";
import useModalHandler from "../../../utils/useModalHandler";
import { FaPlus } from "react-icons/fa";
import { useTranslation } from "react-i18next";
import useSetPageTitle from "../../../Hooks/useSetPageTitle";
import { canAddSubject } from "../../../utils/hasAbilityFn";
import { useDeleteSubject } from "../../../api/subject";
import { lazy } from "react";
import { useGetAllCurriculum } from "../../../api/curriculum";
import { useParams } from "react-router-dom";
import { ParamsEnum } from "../../../enums/params";
import { useGetAllGrade } from "../../../api/grade";
const Table = lazy(() => import("./TablePage"));
const AddModalForm = lazy(() => import("../Model/AddModel"));
const EditModalForm = lazy(() => import("../Model/EditModel"));
const DeleteModalForm = lazy(
  () => import("../../../Layout/Dashboard/DeleteModels"),
);

const TableWithHeader = () => {
  const { handel_open_model } = useModalHandler();
  const [t] = useTranslation();
  const deleteMutation = useDeleteSubject();

  const { grade_id } = useParams<ParamsEnum>();

  const { data: grade } = useGetAllGrade({
    show: grade_id,
  });

  const gradeName = grade?.data?.name ?? "";
  useSetPageTitle(
    t(`page_header.grade`) +
      "/" +
      `${gradeName}` +
      "/" +
      t(`PageTitle.subject`),
  );

  return (
    <div className="TableWithHeader">
      <header>
        <h4 className="Bold Primary ">{t("models.subject")}</h4>
        {canAddSubject && (
          <button
            onClick={() => handel_open_model(ModalEnum?.SUBJECT_ADD)}
            className="add_button"
          >
            {t("practical.add")} {t("models.subject")} <FaPlus />
          </button>
        )}
      </header>
      <Table />

      <AddModalForm />
      <EditModalForm />
      <DeleteModalForm
        deleteMutation={deleteMutation}
        ModelEnum={ModalEnum?.SUBJECT_DELETE}
      />
    </div>
  );
};

export default TableWithHeader;
