import { routerRedux } from 'dva/router'
import pathToRegexp from 'path-to-regexp';

import { dataUtils, formUtils } from '@utils'
import * as examService from '../../../services/source/exams';

export default {

  namespace: 'examsDetails',

  state: {
    formData: {},
    list: [],
    mode: 'create',
    saveBtnFlag:false
  },

  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(location => {
        const match = pathToRegexp('/source-mgmt/bid-mgmt/exam/:id').exec(location.pathname);
        if (match) {
          dispatch({ type: 'clean' });
          const mode = dataUtils.queryStringToJSON(location.search).mode;
          dispatch({ type: 'initWithForm', payload: { mode, id: match[1] } });
          dispatch({ type: 'query', payload: {mode, id: match[1] } });
        }
      })
    },
  },

  effects: {
    * query({ payload }, { call, put, select }) {
      // 角色判断
      /*soum_bm_em_save*/
      let {  saveBtnFlag} = yield select(({ examsDetails }) => examsDetails);
      const perms = yield select(({ app }) => app.perms);
      // console.log('perms==>', perms)
      if (perms) {
        if (perms.find((x) => {
            return x.menuId === 'soum_bm_em_save';
          })) {
          saveBtnFlag = true;
        }
      }
      let data;
      if(payload && payload.mode==='view'){
        data = yield call(examService.queryAllList, payload.id);
      }else{
        data = yield call(examService.queryList, payload.id);
      }
      if (data.success) {
        yield put({
          type: 'updateState',
          payload: {
            list: data.data,
            saveBtnFlag
          },
        });
      } else {
        throw data;
      }
    },

    * initWithForm({ payload }, { call, put }) {
      const { id, mode } = payload;
      const data = yield call(examService.getSingle, id);
      const formData = data.data;
      if (data.success) {
        yield put({
          type: 'updateState',
          payload: {
            formData: formData,
            mode,
          },
        });
      } else {
        throw data;
      }
    },

    * save({ payload }, { select, call, put }) {
      const list = yield select(({ examsDetails }) => examsDetails.list);
      const data = yield call(examService.create, list);
      if (data.success) {
        yield put(routerRedux.goBack());
      } else {
        throw data;
      }
    },

    * clean({ payload }, { call, put }) {
      yield put({
        type: 'updateState',
        payload: {
          formData: {},
          list: [],
          mode: 'create',
        },
      })
    },

    * cancel({ payload }, { put }) {
      yield put(routerRedux.goBack());
    },

  },

  reducers: {

    updateState(state, { payload }) {
      return { ...state, ...payload }
    },

    updateCell(state, { payload }) {
      const { list } = state
      const newList = list.slice()
      const { key, index, value } = payload
      newList[index][key] = value
      return {
        ...state,
        list: newList,
      }
    },
  },
}
