import { queryDictPageApi } from '@/api/dict';
import { onMounted, ref } from 'vue';
import { DEFAULT_PAGE_SIZE } from '@/constant/pagination';

export const useDataSource = () => {
  const dataSource = ref<Record<string, any>[]>([]);
  const currentPage = ref<number>(1);
  const pageSize = ref<number>(DEFAULT_PAGE_SIZE);
  const totalRow = ref<number>(0);
  const loading = ref<boolean>(false);

  const getDataSource = (param?: { currentPage?: number; pageSize?: number }) => {
    loading.value = true;
    if (param?.currentPage) {
      currentPage.value = param.currentPage;
    }
    if (param?.pageSize) {
      pageSize.value = param.pageSize;
    }
    queryDictPageApi({ pageNumber: currentPage.value, pageSize: pageSize.value })
      .then((res) => {
        dataSource.value = res.records;
        totalRow.value = res.totalRow;
        loading.value = false;
      })
      .finally(() => {
        loading.value = false;
      });
  };

  return { dataSource, currentPage, pageSize, totalRow, loading, getDataSource };
};
