export default {
    data() {
        return {
            empty: false,
            loading: false,
            finished: false,
            page: 0,
            maxSize: -1
        };
    },
    methods: {
        getData(isFirst = false) {
            console.log('调动了');
            if (isFirst) {
                this.page = 0;
                this.list = [];
                this.$root.$el.scrollTop = 0;
            }

            this.loading = true;
            this.finished = false;
            this.empty = false;

            let size = 20;

            if (this.maxSize !== -1) {
                size = this.maxSize - this.list.length;
            }
            if (size > 20) {
                size = 20;
            }

            let data = { page: this.page, size: size, sort: 'createdAt,desc' };
            if (this.beforeData) {
                data = {
                    ...data,
                    ...this.beforeData()
                };
            }

            data.query = {
                ...data.query,
                projectId: this.$store.state.projectId
            };

            if (this.httpType === 'get') {
                this.$http.get(this.url, data, { body: 'json' }).then(res => {
                    if (res.first) {
                        this.list = [];
                    }
                    this.list = [...this.list, ...res.content];
                    this.empty = res.empty;
                    this.loading = false;
                    this.finished = res.last;
                    if (this.maxSize !== -1) {
                        if (!this.finished && this.list.length >= this.maxSize) {
                            this.finished = true;
                        }
                    }
                    if (!this.finished) {
                        this.page = this.page + 1;
                    }
                });
            } else {
                this.$http.post(this.url, data, { body: 'json' }).then(res => {
                    if (res.first) {
                        this.list = [];
                    }
                    this.list = [...this.list, ...res.content];
                    this.empty = res.empty;
                    this.loading = false;
                    this.finished = res.last;
                    if (this.maxSize !== -1) {
                        if (!this.finished && this.list.length >= this.maxSize) {
                            this.finished = true;
                        }
                    }
                    if (!this.finished) {
                        this.page = this.page + 1;
                    }
                });
            }
        }
    }
};
