// import tips from 'utils/tips'
const filterList = (params = {}) => ({
  data() {
    return {
      isLoading: false,
      items: [],
      pagination: {
        page: 1,
        total: 0,
        pageSize: 30
      },
      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 = {
        'data': {
          ...this.filter,
          ...this.internalFilterObj
        },
        'start': this.pagination.page,
        'limit': this.pagination.pageSize
      }
      const {
        success, data, msg
      } = await this.apiList(inParams)
      if (success) {
        const { items, total } = data
        if (items.length === 0 && this.pagination.page > 1) {
          this.pageChange(1)
        } else {
          this.items = items
          this.$set(this.pagination, 'total', total)
        }
        this.loadCallBack(data)
      } else {
        this.$message.error(msg)
      }
    },

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