import * as appService from '../services/app'
import { getEnums } from '../utils/common'
import * as roleEditService from "../services/FM/roleEdit"
import { routerRedux } from 'dva/router'
import config from '../config'
export default {
  namespace: 'app',
  state: {
    orgId: '',
    enums: {},
    alarmId: '', // 显示报警面板
    currVersion: null,
    addedTab: null, // 用于在其他页面添加 tab
    currActivatedTab: null, // 用于面包屑导航，同步于 model login
    currActivatedMenu: null // 当前选中的菜单项，同步于 model login
  },
  subscriptions: {
    setup ({ dispatch, history }) {  // eslint-disable-line
      history.listen(({ pathname }, routeType) => {
        if (pathname === '/' && !routeType) {
          if (window.navigator.userAgent.indexOf("Windows") < 1)
          {
            window.location.replace(config.guideH5Host)
          }
          else
          {
            dispatch(routerRedux.push('/home'))
          }
        }
      })
    }
  },
  effects: {
    *getFormId ({ payload, onComplete }, { call, put }) {
      const ret = yield call(appService.getFormId, payload)
      if (typeof onComplete === 'function') {
        onComplete(ret)
      }
      return ret
    },
    *getFormByRedis ({ payload, onComplete }, { call, put }) {
      const ret = yield call(appService.getFormByRedis, payload)
      if (typeof onComplete === 'function') {
        onComplete(ret)
      }
      return ret
    },
    *getEntities ({ payload, onComplete }, { call, put }) {
      const ret = yield call(appService.getEntities, payload)
      if (typeof onComplete === 'function') {
        onComplete(ret)
      }
      return ret
    },
    *getTableEntities ({ payload, onOver }, { call, put }) {
      const ret = yield call(appService.getTableEntities, payload)
      if (typeof onOver === 'function') {
        onOver(ret)
      }
      return ret
    },
    *getTreeCloum ({ payload, onComplete }, { call, put }) {
      const ret = yield call(appService.getTreeCloum, payload)
      if (typeof onComplete === 'function') {
        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
    },
    *getDataByPost ({ payload, url, onComplete, onlyData = true, ignoreThrottle = false, isIgnoreTenant = false }, { call, put }) {
      const ret = yield call(appService.getDataByPost, url, payload, onlyData, ignoreThrottle, isIgnoreTenant)
      if (typeof onComplete === 'function') {
        onComplete(ret)
      }
      return ret
    },
    *getDataByGet ({ payload, url, onComplete, onlyData = true }, { call, put }) {
      const ret = yield call(appService.getDataByGet, url, payload, onlyData)
      if (typeof onComplete === 'function') {
        onComplete(ret)
      }
      return ret
    },
    *deleteItem ({ payload, url, onComplete }, { call }) {
      const ret = yield call(appService.deleteItem, url, payload)
      if (typeof onComplete === 'function') {
        onComplete(ret)
      }
      return ret
    },
    *deleteItemBatch ({ payload, url, onComplete }, { call }) {
      const ret = yield call(appService.deleteItemBatch, url, payload)
      if (typeof onComplete === 'function') {
        onComplete(ret)
      }
      return ret
    },
    *genSystemCode ({ payload, onComplete }, { call, put }) {
      const ret = yield call(appService.genSystemCode, payload)
      if (typeof onComplete === 'function') {
        onComplete(ret)
      }
      return ret
    },
    *releasePreUseSerial ({ payload, onComplete }, { call, put }) {
      const ret = yield call(appService.releasePreUseSerial, payload)
      if (typeof onComplete === 'function') {
        onComplete(ret)
      }
      return ret
    },
    *getEnums ({ payload, onComplete }, { call, put }) {
      const ret = yield call(appService.getEnums, payload)
      if (typeof onComplete === 'function') {
        onComplete(getEnums(ret))
      }
      return ret
    },
    *getAllEnum ({ payload }, { call, put }) {
      const ret = yield call(appService.getAllEnum, payload)
      const enums = {}
      if (ret && ret.length) {
        ret.forEach((n, i) => {
          const tmp = { options: n.options, enums: {} }
          if (n.options && n.options.length) {
            n.options.forEach((n1, i1) => {
              tmp.enums[n1.value] = n1.label
            })
          }
          enums[n.EnumName] = tmp
        })
      }
      yield put({ type: 'save', payload: { enums: enums } })
      return ret
    },
    *addLoginOut ({ payload, onComplete }, { call, put }) {
      const ret = yield call(appService.addLoginOut, payload)
      if (typeof onComplete === 'function') {
        onComplete(ret)
      }
      return ret
    },
    *addAddLog ({ payload, onComplete }, { call, put }) {
      const ret = yield call(appService.addAddLog, payload)
      if (typeof onComplete === 'function') {
        onComplete(ret)
      }
      return ret
    },
    *addEditLog ({ payload, onComplete }, { call, put }) {
      const ret = yield call(appService.addEditLog, payload)
      if (typeof onComplete === 'function') {
        onComplete(ret)
      }
      return ret
    },
    *addDeleteLog ({ payload, onComplete }, { call, put }) {
      const ret = yield call(appService.addDeleteLog, payload)
      if (typeof onComplete === 'function') {
        onComplete(ret)
      }
      return ret
    },
    *addOtherLog ({ payload, onComplete }, { call, put }) {
      const ret = yield call(appService.addOtherLog, payload)
      if (typeof onComplete === 'function') {
        onComplete(ret)
      }
      return ret
    },
    *operateLog ({ payload, onComplete }, { call, put }) {
      const ret = yield call(appService.operateLog, payload)
      if (typeof onComplete === 'function') {
        onComplete(ret)
      }
      return ret
    },
    *getBtnList ({ payload, onComplete }, { call, put }) {
      const ret = yield call(roleEditService.getBtnList, payload)
      if (typeof onComplete === 'function') {
        onComplete(ret)
      }
      return ret
    },
    *acceptNotification ({ payload, onComplete }, { call, put }) {
      const ret = yield call(appService.acceptNotification, payload)
      if (typeof onComplete === 'function') {
        onComplete(ret)
      }
      return ret
    },
    *getServerList ({ payload, onComplete, url }, { call, put }) {
      const ret = yield call(appService.getServerList, { url, payload })
      if (typeof onComplete === 'function') {
        onComplete(ret)
      }
      return ret
    },
    *getCurrVer ({ payload, onComplete }, { call, put, select }) {
      const { currVersion } = yield select(state => state.app)
      const { OrgId } = yield select(state => state.login)
      if (!currVersion && OrgId) {
        const ret = yield call(appService.getCurrVer, { OrgId, ...payload })
        yield put({ type: 'save', payload: { currVersion: ret } })
        if (typeof onComplete === 'function') {
          onComplete(ret)
        }
      }
    },
    *updateActivatedMenu ({ payload }, { put }) {
      yield put({ type: 'save', payload })
      yield put({ type: 'login/save', payload })
    },
    /**
     * 
     * @param {
     *  payload: {
     *    Keyword: '',
     *    OrgId: ''
     *  }
     * } param0 
     * @param {*} param1 
     * @returns 
     */
    *getRedisValue ({ payload }, { call }) {
      return yield call(appService.getRedisValue, { payload })
    },
    /**
     * 
     * @param {
     *  payload: {
     *    Keyword: '',
     *    OrgId: '',
     *    Parameter1: ''
     *  }
     * } param0 
     * @param {*} param1 
     * @returns 
     */
    *setRedisValue ({ payload }, { call }) {
      return yield call(appService.setRedisValue, { payload })
    }
  },
  reducers: {
    save (state, action) {
      return { ...state, ...action.payload }
    },
    addTab (state, action) {
      return { ...state, ...action.payload }
    }
  }
}
