/* eslint-disable prettier/prettier */
import { PaginationProps } from "@pureadmin/table";
import { reactive } from "vue";
import { PageContentProps } from "../types";
import { useAppStoreHook } from "/@/store/modules/app";

export function usePagination(props: PageContentProps, { onSearch }: any) {
  const { pageSize, contentConfig } = props;

  const { searchParams } = useAppStoreHook();

  if (contentConfig.notPagination) return {};

  //分页参数
  const pagination = reactive<PaginationProps>(
    searchParams.pagination
      ? searchParams.pagination
      : {
        total: 0,
        pageSize,
        currentPage: 1,
        background: true,
        pageSizes: [15, 50, 100]
      }
  );

  /**
   * 设置当前页
   */
  async function onCurrentChange(currentPage: number) {
    pagination.currentPage = currentPage;
    await onSearch();
  }

  /**
   * 设置分页器
   */
  async function onSizeChange(pageSize: number) {
    pagination.pageSize = pageSize;
    pagination.currentPage = 1;
    await onSearch();
  }

  function getPagination() {
    return pagination;
  }

  function changePagination(key: string, newVal) {
    pagination[key] = newVal;
  }

  return {
    onSizeChange,
    onCurrentChange,
    pagination,
    getPagination,
    changePagination
  };
}
