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";
import PageHeader from "../../../Layout/Dashboard/PageHeader";
import { ABILITIES_ENUM } from "../../../enums/abilities";
import { canAddQuestion } from "../../../utils/hasAbilityFn";
import FilterLayout from "../../../Layout/Dashboard/FilterLayout";
import FilterForm from "./FilterForm";
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 />}>
      <PageHeader 
            pageTitle="question"
            ModelAbility={ModalEnum?.QUESTION_ACCEPT} 
            canAdd={canAddQuestion}
            locationToNavigate={`${ABILITIES_ENUM?.QUESTION}/add`}
            openModel={false}/>
          <FilterLayout
           sub_children={<FilterForm/>}
           filterTitle="sidebar.question"/>
        <Table />
      </Suspense>
      <DeleteModels
        deleteMutation={deleteMutation}
        ModelEnum={ModalEnum?.QUESTION_DELETE}
      />
    </div>
  );
};

export default TableHeader;
