export default class Page {
  listMethod = new Function()
  listKey = 'data.lists'
  listTotalKey = 'data.total'
  hideFilter = false
  hidePagination = false
  hasExport = false
  showSelection = false
  listQuery = {
    page: 1,
    limit: 10
  }
  filterFields = []
  tabelFields = []
  formFields = []
  formDefaultValue = {}
  /**
   * 列表请求返回的数据处理函数
   * @param {array} data
   * @returns {array}
   */
  listManager(data) {
    return data
  }
  /**
   * 列表请求参数处理函数
   * @param {any} data
   * @returns {any}
   */
  searchManager(data) {
    return data
  }
  /**
   * 通过key从一个数组中筛选出新数组
   * @param {array} dataArr 目标数组
   * @param {array} keyArr 筛选key的数组
   * @param {string} key 筛选的key
   * @returns {array}
   */
  static findByKey(dataArr, keyArr, key) {
    return keyArr.map(keyItem => {
      const item = dataArr.find(dataItem => dataItem[key] === keyItem)
      if (!item) console.log(`没找到: ${keyItem}`)
      return { ...item }
    })
  }
  /**
   * 根据数组 prop defaultValue 字段生成对象
   * @param {array} arr
   */
  static getDefaultObj(arr = []) {
    return arr.reduce((prev, curr) => {
      prev[curr.prop] = curr.defaultValue
      return prev
    }, {})
  }

  /**
   * 格式化字段
   * @param {object} obj
   * @param {string} type
   */
  static fieldFormat(obj, type) {
    const item = { ...obj }
    const reg = new RegExp(`^${type}(.+)$`)
    Object.keys(obj).forEach((key) => {
      if (reg.test(key)) {
        const objKey = (RegExp.$1).toLocaleLowerCase()
        item[objKey] = obj[key]
      }
    })
    if (!obj[type]) obj[type] = {}
    return { ...item, ...obj[type] }
  }
  static fieldFormatByTabel(obj) {
    return this.fieldFormat(obj, 'tabel')
  }
  static fieldFormatByForm(obj) {
    return this.fieldFormat(obj, 'form')
  }
  static getTabelFields(arr) {
    return arr.map(item => this.fieldFormatByTabel(item))
  }
  static getFormFields(arr) {
    return arr.map(item => this.fieldFormatByTabel(item))
  }
  getFormDefaultValue() {
    return this.getDefaultObj(this.formFields)
  }
}
