import { routerRedux } from 'dva/router';
import * as baseService from '../../services/FM/base';

export default {

  namespace: 'FMBase',
  state: {
    userGroupDatasource:[],
    roleDatasource:[],
    roleGroupDatasource:[],
  },

  subscriptions: {
    setup({ dispatch, history }) {  // eslint-disable-line

    },
  },

  effects: {
    *getUserGroupDatasource ({ payload }, { call, put }) {
      const ret = yield call(baseService.getUserGroupDatasource, payload);
      const dataSource = [];
      if(ret) {
        ret.map(item => {
          dataSource.push({
            key: item.ID.toString().toUpperCase(),
            title: item.NAME,
          });
        });
      }
      yield put({ type: 'save', payload: { userGroupDatasource: dataSource } });
    },
    *getRoleDatasource({ payload }, { call, put }) {
      const ret = yield call(baseService.getRoleDatasource, payload);
      const dataSource = [];
      if(ret) {
        ret.map(item => {
          dataSource.push({
            key: item.ID.toString().toUpperCase(),
            title: item.NAME,
          });
        });
      }
      yield put({ type: 'save', payload: { roleDatasource: dataSource } });
    },
    *getRoleGroupDatasource ({ payload }, { call, put}) {
      const ret = yield call(baseService.getRoleGroupDatasource, payload);
      const dataSource = [];
      if(ret) {
        ret.map(item => {
          dataSource.push({
            key: item.ID.toString().toUpperCase(),
            title: item.NAME,
          });
        });
      }
      yield put({ type: 'save', payload: { roleGroupDatasource: dataSource } });
    },
  },

  reducers: {
    save(state, action) {
      return { ...state, ...action.payload };
    },
  },

};
