import { Message } from 'element-ui';
// import tips from 'utils/tips'
const filterList = (params = {}) => ({
  data() {
    return {
      isLoading: false,
      items: [],
      pagination: {
        page: 1,
        total: 0,
        pageSize: 10
      },
      filter: {}, // 每次填写的过滤条件
      internalFilterObj: {}, // 必定的内置过滤条件
      fetchList: () => {}
    };
  },

  created() {
    Object.assign(this, params);
  },

  methods: {
    loadCallBack() {},

    apiList(data) {
      return this.fetchList(data);
    },

    reload() {
      this.pageChange(this.pagination.page);
    },

    async pageChange(page) {
      this.pagination.page = page;
      const inParams = {
        ...this.filter,
        ...this.internalFilterObj,
        page: this.pagination.page,
        size: this.pagination.pageSize
      };

      const { data, msg } = await this.apiList(inParams, {
        limit: this.pagination.pageSize,
        start: this.pagination.page
      });
      if ('data' in data) {
        const items = data.data;
        if (items.length === 0 && this.pagination.page > 1) {
          this.pageChange(1);
        } else {
          this.items = items;
          // this.pagination.page += 1;
          this.pagination.total = data.total;
        }
        this.loadCallBack(data);
      }
    },

    filterData(filter) {
      this.filter = filter;
      this.pageChange(1);
    }
  }
});
export default filterList;
