import { useTranslation } from "react-i18next";
import { lazy, Suspense } from "react";
import { Spin } from "antd";
import { useParams } from "react-router-dom";
import { ParamsEnum } from "../../enums/params";
import { useGetAllSubject } from "../../api/subject";
import useSetPageTitle from "../../Hooks/useSetPageTitle";
import { ModalEnum } from "../../enums/Model";
import { useDeleteUnit } from "../../api/unit";
import DrapableTable from "./DrapableTable";
import { useGetAllGrade } from "../../api/grade";
import { useGetAllCurriculum } from "../../api/curriculum";

const Table = lazy(() => import("./Table"));
const AddModalForm = lazy(() => import("./Model/AddModel"));
const EditModalForm = lazy(() => import("./Model/EditModel"));
const DeleteModalForm = lazy(
  () => import("../../Layout/Dashboard/DeleteModels"),
);

const TableHeader = () => {
  const [t] = useTranslation();
  const deleteMutation = useDeleteUnit();

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

  const { data: Subject } = useGetAllSubject({
    show: subject_id,
  });

  const { data: grade } = useGetAllGrade({
    show: grade_id,
  });
  const { data: Curriculum } = useGetAllCurriculum({
    show: curriculum_id,
  });
  const gradeName = grade?.data?.name ?? "";

  const SubjectName = Subject?.data?.name ?? "";
  const CurriculumName = Curriculum?.data?.name ?? "";
  useSetPageTitle(
    t(`page_header.grade`) +
      "/" +
      gradeName +
      "/" +
      t(`PageTitle.subject`) +
      "/" +
      SubjectName +
      "/" +
      t("PageTitle.curriculum") +
      "/" +
      CurriculumName +
      "/" +
      t("PageTitle.unit"),
  );

  return (
    <div className="TableWithHeader">
      <Suspense fallback={<Spin />}>
        <header>
          <h6>{t("models.units")}</h6>
        </header>
        <Table />
        <AddModalForm />
        <EditModalForm />
        <DeleteModalForm
          deleteMutation={deleteMutation}
          ModelEnum={ModalEnum?.UNIT_DELETE}
        />
      </Suspense>
    </div>
  );
};

export default TableHeader;
