// 自定义分页 Hook
export default function usePage<T>(
    queryFun: (pageNum: number, pageSize: number) => Promise<IResData<T>>,
    callback?: (params?: any) => void,
) {
    const paging = ref<ZPagingInstance<T> | null>(null)
    const loading = ref(true)
    const firstLoaded = ref(false)
    const dataList = ref<T[]>([])
    const totalRows = ref(0)

    async function queryList(pageNum: number, pageSize: number) {
        try {
            const res = await queryFun(pageNum, pageSize)
            console.log('接口响应', res)
            await paging.value?.complete(res?.rows || [])
            totalRows.value = res?.total
            firstLoaded.value = true
            setTimeout(() => {
                callback && callback({...res, pageNum, pageSize})
            }, 100)
        } catch (error) {
            await paging.value?.complete(false)
            setTimeout(() => {
                callback && callback({pageNum, pageSize})
            }, 100)
        } finally {
            setTimeout(() => {
                loading.value = false
            }, 100)
        }
    }

    return {
        paging,
        firstLoaded,
        loading,
        dataList,
        totalRows,
        queryList,
    }
}
