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 { useGetAllUnit } from "../../api/unit";
import useSetPageTitle from "../../Hooks/useSetPageTitle";
import { useGetAllLesson } from "../../api/lesson";
import { useDeleteQuestion } from "../../api/Question";
import DeleteModels from "../../Layout/Dashboard/DeleteModels";
import { ModalEnum } from "../../enums/Model";
import { useGetAllSubject } from "../../api/subject";
import { useGetAllGrade } from "../../api/grade";
import { useGetAllCurriculum } from "../../api/curriculum";
const Table = lazy(() => import("./Table"));

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

  const deleteMutation = useDeleteQuestion();

  const { unit_id, curriculum_id, grade_id, subject_id, lesson_id } =
    useParams<ParamsEnum>();
  const { data: unit } = useGetAllUnit({ show: unit_id });

  const { data: Subject } = useGetAllSubject({
    show: subject_id,
  });
  const { data: grade } = useGetAllGrade({
    show: grade_id,
  });
  const { data: Curriculum } = useGetAllCurriculum({
    show: curriculum_id,
  });
  const { data: Lesson } = useGetAllLesson({
    show: lesson_id,
  });

  const gradeName = grade?.data?.name ?? "";
  const SubjectName = Subject?.data?.name ?? "";
  const CurriculumName = Curriculum?.data?.name ?? "";
  const unitName = unit?.data?.name ?? "";
  const LessonName = Lesson?.data?.name ?? "";

  useSetPageTitle(
    t(`page_header.grade`) +
      "/" +
      gradeName +
      "/" +
      t(`PageTitle.subject`) +
      "/" +
      SubjectName +
      "/" +
      t("PageTitle.curriculum") +
      "/" +
      CurriculumName +
      "/" +
      t("PageTitle.unit") +
      "/" +
      unitName +
      "/" +
      t("PageTitle.lesson") +
      "/" +
      LessonName +
      "/" +
      t("PageTitle.question"),
  );

  return (
    <div className="TableWithHeader">
      <Suspense fallback={<Spin />}>
        <header>
          <h6>{t("models.Question")}</h6>
        </header>
        <Table />
      </Suspense>
      <DeleteModels
        deleteMutation={deleteMutation}
        ModelEnum={ModalEnum?.QUESTION_DELETE}
      />
    </div>
  );
};

export default TableHeader;
