export default {
    onPullDownRefresh() {
        if (this.store.isListAtTop) {
            this.store && this.store.reload()
        }
    },
    onReachBottom() {
        if (this.store.status != 'noMore') {
            this.store.next && this.store.next()
        }
    },
    data() {
        return {
            store: {},
        }
    },
    methods: {
        DataSource(url) {
            var me = this
            return {
                data: [],
                copyData: {},
                status: 'noMore',
                empty: false,
                isListAtTop: true,
                total: 0,
                header: {
                    'Content-Type': 'application/json;charset=UTF-8',
                },
                params: {
                    pageNum: 1,
                    pageSize: 20,
                },
                timer: null,
                callBack: null,
                firstCallBack: null,
                loadedCallback: null,
                reload() {
                    if (this.status == 'loading') return
                    this.data = []
                    this.empty = false
                    this.params.pageNum = 1
                    this.next()
                },
                search() {
                    // 节流搜索
                    clearTimeout(this.timer)
                    this.timer = setTimeout(() => {
                        this.reload()
                    }, 300)
                },
                next() {
                    this.status = 'loading'
                    me.$request({
                        url: url,
                        data: this.params,
                        header: this.header,
                        method: 'POST',
                        debounce: false,
                    }).then((res) => {
                        console.warn(res)
                        let resultData = res.data?.list || res.data?.records || res.data || []
                        this.copyData = res.data
                        if (this.params.pageNum == 1) {
                            this.data = resultData
                            if (resultData.length == 0 || resultData == 0) {
                                this.empty = true
                                this.status = 'noMore'
                            } else if (resultData.length < this.params.pageSize) {
                                this.status = 'noMore'
                            } else if (res.total == resultData.length) {
                                this.status = 'noMore'
                            } else {
                                this.status = 'more'
                            }
                        } else {
                            this.data.push(...resultData)
                            if (resultData.length < this.params.pageSize) {
                                this.status = 'noMore'
                            } else {
                                this.status = 'more'
                            }
                        }
                        if (this.params.pageNum == 1) {
                            this.firstCallBack && this.firstCallBack()
                        }
                        this.loadedCallback && this.loadedCallback()
                        this.params.pageNum++
                        this.total = res.total || res.data?.total

                        this.loading = false
                        uni.stopPullDownRefresh()
                    })
                },
            }
        },
    },
}
