/**
 * 分页组合式函数
 */
import { ref, reactive, computed } from 'vue'

export function usePagination(defaultPageSize = 20) {
  const currentPage = ref(1)
  const pageSize = ref(defaultPageSize)
  const total = ref(0)

  const pagination = reactive({
    page: currentPage,
    pageSize: pageSize,
    total: total
  })

  const totalPages = computed(() => {
    return Math.ceil(total.value / pageSize.value)
  })

  const resetPage = () => {
    currentPage.value = 1
  }

  const setTotal = (count: number) => {
    total.value = count
  }

  return {
    currentPage,
    pageSize,
    total,
    pagination,
    totalPages,
    resetPage,
    setTotal
  }
}
