import React from "react";
import styles from "@/app//(Admin)/dashboard/(Posts Zone)/categories/categories.module.css";
import Link from "next/link";
import { BsArrowLeft } from "react-icons/bs";
import { getCategories } from "@/models/term";
import CategoriesData from "@/components/administrator/CategoriesData/categories.data";

const CategoriesPage = async ({ searchParams }) => {
  const query = await searchParams;
  const sort = query?.sort ? JSON.parse(query.sort) : { name: 1 };
  let page = parseInt(query?.page) || 1;
  const search = query?.search || null;
  const limit = 50;

  if (search) page = 1;

  const categories = await getCategories({ page, limit }, sort, search);

  return (
    <div className={styles.container}>
      <div className={styles.header}>
        <Link
          className={styles.back}
          href="/dashboard">
          <BsArrowLeft /> back
        </Link>
      </div>
      <div className={styles.body}>
        <CategoriesData
          initialData={categories}
          limit={limit}
          sort={sort}
        />
      </div>
    </div>
  );
};

export default CategoriesPage;
