export default {
    data() {
        return {
            empty: false,
            loading: false,
            finished: false,
            page: 0,
            totalElements: 0
        };
    },
    methods: {
        getData(isFirst = false, scrollTop = 0) {
            if (isFirst) {
                this.page = 0;
                this.list = [];
                this.$root.$el.scrollTop = scrollTop;
            }
            this.loading = true;
            this.finished = false;
            this.empty = false;
            let data = { page: this.page, size: 20, sort: 'createdAt,desc' };
            if (this.beforeData) {
                data = {
                    ...data,
                    ...this.beforeData()
                };
            }

            if (this.httpType === 'get') {
                return 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.finished) {
                        this.page = this.page + 1;
                    }
                    this.totalElements = Number(res.totalElements);
                });
            } else {
                return 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.finished) {
                        this.page = this.page + 1;
                    }
                    this.totalElements = Number(res.totalElements);
                });
            }
        }
    }
};
