export default {
  data() {
    return {
      listLoading: false,
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      totalRecords: 1,
      listConfig: {},
      operations: []
    }
  },
  created() {
    this.$nextTick(() => {
      if (!this.noCreated) this.fetchData()
    })
  },
  methods: {
    searchHandle(obj) {
      this.currentPage = 1
      this.searchForm = obj
      this.fetchData()
    },
    sizeChange (size) {
      this.pageSize = size
      this.currentPage = 1
      this.fetchData()
    },
    pageHandle(page) {
      this.currentPage = page
      this.fetchData()
    },
    sortChange(column) {
      this.currentPage = 1
      const obj = { ...this.searchForm }
      if (column.order === 'ascending') {
        obj.sort = JSON.stringify({ [column.prop]: 'asc' })
      } else {
        obj.sort = JSON.stringify({ [column.prop]: 'desc' })
      }
      this.searchForm = {...obj}
      this.fetchData(obj)
    },
    fetchData(curData) {
      if (!this.apiStr) return
      let params = {
        page: this.currentPage,
        page_size: this.pageSize
      }
      if (curData) {
        params = {...params, ...curData}
      } else {
        if (this.searchForm) {
          // params = Object.assign({
          //   ...this.searchForm
          // }, params)
          params = {...params, ...this.searchForm}
        }
      }
      if (this.getDataBefore) {
        this.getDataBefore(params)
      }
      this.listLoading = true
      const apiArr = this.apiStr.split('.')
      this.$api[apiArr[0]][apiArr[1]](params).then(data => {
        if (this.currentPage === 1) this.pageSize = data.page_size || 1
        this.totalRecords = data.total ? Number(data.total) : 0
        this.tableData = data.list || []
        this.listLoading = false
        if (this.getMoreData) {
          this.getMoreData(data, params)
        }
        return data
      }).catch(err => {
        this.listLoading = false
      })
    }
  }
}
