export const TableMixin = {
  data() {
    return {
      tableData: [],
      pageable: {
        pageNum: 1,
        pageSize: 10,
        total: 0
      },
      formParams: {},
      loading: false,
      isAuto: true
    }
  },
  methods: {
    async getList(params) {
      if (params) {
        const { limit, page } = params
        this.pageable = {
          ...this.pageable,
          pageNum: page,
          pageSize: limit
        }
      }

      this.loading = true
      try {
        let params2 = {
          ...this.pageable,
          ...this.formParams
        }
        if (this.paramsCallback) {
          params2 = this.paramsCallback()
        }
        const { data } = await this.apiMethod(params2)
        this.loading = false
        const { list = [], page } = data
        this.tableData = list || []
        this.pageable.total = Number(page.total)
      } catch (err) {
        console.error('err=', err)
      } finally {
        this.loading = false
      }
    },
    // 搜索
    onSearch() {
      this.pageable.pageNum = 1
      this.getList()
    },
    init() {
      if (!this.isAuto) {
        return
      }
      this.getList()
    }
  },
  mounted() {
    this.init()
  }
}
