import * as appService from '../services/listPage'

export default {
  namespace: 'listPage',
  state: {
    btnEntities: []
  },
  effects: {
    *getBtnEntities ({ payload, onOver }, { call, put }) {
      const ret = yield call(appService.getBtnEntities, payload)
      if (typeof onOver === 'function') {
        onOver(ret)
      }
      yield put({ type: 'save', payload: { btnEntities: ret } })
      return ret
    },
    *getTableListData ({ payload, onComplete }, { call, put }) { // 表单列表页数据
      const ret = yield call(appService.getTableListData, payload)
      if (ret !== null) {
        onComplete(ret)
      }
      return ret
    },
    *getTableData ({ payload, url, onComplete }, { call, put }) {
      const ret = yield call(appService.getTableData, { payload, url })
      if (typeof onComplete === 'function') {
        onComplete(ret)
      }
      return ret
    },
    *getTablePageConfigInfo ({ payload, onComplete }, { call, put }) {
      const ret = yield call(appService.getTablePageConfigInfo, payload)
      if (typeof onComplete === 'function') {
        onComplete(ret)
      }
      return ret
    },
    *getUserCustomConfigInfo ({ payload, onComplete }, { call, put }) {
      const ret = yield call(appService.getUserCustomConfigInfo, payload)
      if (typeof onComplete === 'function') {
        onComplete(ret)
      }
      return ret
    },
    *saveCustomConfig ({ payload, onComplete }, { call, put }) {
      const ret = yield call(appService.saveCustomConfig, payload)
      if (typeof onComplete === 'function') {
        onComplete(ret)
      }
      return ret
    },
    *getTablePageTables ({ payload }, { call }) {
      return yield call(appService.getTablePageTables, payload)
    },
    *saveUserCustomConfigInfoForPageSize ({ payload, onComplete }, { call, put }) {
      const ret = yield call(appService.saveUserCustomConfigInfoForPageSize, payload)
      if (typeof onComplete === 'function') {
        onComplete(ret)
      }
      return ret
    },
    *listRowDragChange ({ payload, onComplete }, { call }) {
      const ret = yield call(appService.listRowDragChange, payload)
      if (typeof onComplete === 'function') {
        onComplete(ret)
      }
      return ret
    }
  },
  reducers: {
    save (state, action) {
      return { ...state, ...action.payload }
    }
  }
}
