export default {
    data() {
        return {
            empty: false,
            loading: false,
            finished: false,
            page: 0,
            pageSize: 20,
            totalPages: 0,
            totalElements: 0
        };
    },
    methods: {
        getData(isFirst = false) {
            if (isFirst) {
                this.page = 0;
                this.list = [];
                this.$root.$el.scrollTop = 0;
            }

            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()
                };
            }

            this.$http.post(this.url, data, { body: 'json' }).then(res => {
                if (this.setList) {
                    this.setList(res.content);
                }
                this.totalPages = res.totalPages;
                this.totalElements = res.totalElements;
                // 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;
                // }
            });
        },
        onSearch() {
            this.$router.push({
                query: {
                    ...this.$route.query,
                    search: this.search
                }
            });
            this.getData(true);
        },
        onSortChange(e) {
            this.sort = e;
            this.getData();
        },
        onSizeChange(e) {
            localStorage.setItem('pageSize', e);
            this.page = 1;
            this.pageSize = e;
            this.getData();
        },
        onCurrentChange(e) {
            this.$router.push({
                query: {
                    ...this.$route.query,
                    page: e
                }
            });
            this.page = e;
            this.getData();
        }
    }
};
