export default {
    onPullDownRefresh () {
        this.store && this.store.reload();
        this.store.loadedCallback = () => {
            uni.stopPullDownRefresh();
        };
    },
    onReachBottom () {
        if (this.store.status != "noMore") {
            this.store.next && this.store.next();
        }
    },
    data () {
        return {
            store: {},
        };
    },
    methods: {
        DataSource (url) {
            var me = this;
            return {
                data: [],
                status: "more",
                empty: false,
                total: 0,
                header: {},
                params: {
                    pages: 1,
                    step: 20,
                    entity: {},
                },
                reload () {
                    this.data = [];
                    this.status = "loading";
                    this.empty = false;
                    this.params.pages = 1;
                    this.next();
                },
                callBack: null,
                firstCallBack: null,
                loadedCallback: null,
                next () {
                    me.$request({
                        url: url,
                        data: Object.assign(
                            {
                                // page: {
                                //     current: this.params.pages,
                                //     orders: [],
                                //     size: this.params.step,
                                //     total: 0,
                                // },
                                offset: this.params.pages,
                                limit: this.params.step,
                                current: this.params.pages,
                                pageSize: this.params.step,
                            },
                            this.params
                        ),
                        header: this.header,
                        method: "POST",
                    }).then((res) => {
                        console.warn(res);
                        let resultData = res.rows || res.data?.list || res.data?.result?.records || [];
                        if (this.params.pages == 1) {
                            this.data = resultData;
                            if (resultData.length == 0 || resultData == 0) {
                                this.empty = true;
                            } else if (resultData.length < this.params.step) {
                                this.status = "noMore";
                            } else if (res.total == resultData.length) {
                                this.status = "noMore";
                            }
                        } else {
                            this.data.push(...resultData);
                            if (resultData.length < this.params.step) {
                                this.status = "noMore";
                            }
                        }

                        if (this.params.pages == 1) {
                            this.firstCallBack && this.firstCallBack();
                        }
                        this.loadedCallback && this.loadedCallback();
                        this.params.pages++;
                        this.total = res.total;
                        this.loading = false;
                    });
                },
            };
        },
    },
};
