import { FaPlus } from "react-icons/fa";
import useModalHandler from "../../utils/useModalHandler";
import { ModalEnum } from "../../enums/Model";
import { useTranslation } from "react-i18next";
import { lazy, Suspense } from "react";
import { Spin } from "antd";
import { canAddPackage } from "../../utils/hasAbilityFn";
import useSetPageTitle from "../../Hooks/useSetPageTitle";
import { useDeletePackage } from "../../api/package";
import { useNavigate } from "react-router-dom";
import { ABILITIES_ENUM } from "../../enums/abilities";
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 SearchField = lazy(
  () => import("../../Components/DataTable/SearchField"),
);

const TableHeader = () => {
  const { handel_open_model } = useModalHandler();
  const [t] = useTranslation();
  const navigate = useNavigate();
  useSetPageTitle(t(`page_header.package`));
  const deleteMutation = useDeletePackage();

  return (
    <div className="TableWithHeader">
      <Suspense fallback={<Spin />}>
        <header className="d-flex justify-content-between">
          <SearchField
            searchBy="name"
            placeholder={t("practical.search_here")}
          />

          {canAddPackage && (
            <div className="Selects">
              <button
                onClick={() => navigate(`/${ABILITIES_ENUM?.Package}/add`)}
                className="add_button"
              >
                {t("models.add_package")} <FaPlus />
              </button>
            </div>
          )}
        </header>

        <Table />
        <DeleteModalForm
          deleteMutation={deleteMutation}
          ModelEnum={ModalEnum?.PACKAGE_DELETE}
        />
      </Suspense>
    </div>
  );
};

export default TableHeader;
