// import { formUtils } from '@utils';
// import { routerRedux } from 'dva/router';
import { networkUtils } from '@utils';

async function queryFormData() {
  // return networkUtils.request({
  //   url: '/edp/bpm/form/nodes',
  //   method: 'get',
  //   data: { defId },
  // });
}


async function queryFormCfg(params) {
  return networkUtils.request({
    url: '/edp/bpm/form/config',
    method: 'get',
    data: params,
  });
}


export default {
  namespace: 'dev_form',

  state: {
    formData: null,
    tableData: [{
      id: 1,
      supplier: { id: 1, code: '2222', text: 'props' },
      warnLevelCode: '2133',
      warnDays: 12,
    }],
    remoteCfg: null,
  },

  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(location => {
        if (location.pathname === '/dev/form') {
          dispatch({ type: 'query' });
        }
      });
    },
  },

  effects: {
    * query({ payload }, { call, put, select, take }) {
      // yield take('app/updateInfo');
      const { user } = yield select(({ app }) => app.user);
      // console.log(user);

      const formdata = yield call(queryFormData, '');

      const params = {
        defId: 'SuppliperItem:1:87323',
        nodeId: 'purchasingManager',
        mode: 'create',
      };

      const cfgData = yield call(queryFormCfg, params);

      if (cfgData.success) {
        yield put({
          type: 'querySuccess',
          payload: {
            remoteCfg: cfgData.data,
          },
        });
      } else {
        throw cfgData;
      }
    },

    * querySingle({ payload }, { call, put }) {

    },

    * getSingle({ payload }, { put }) {

    },

  },

  reducers: {
    clean(state, { payload }) {
      return {
        ...state,
        formData: {},
      };
    },

    querySuccess(state, action) {
      const { formData, remoteCfg } = action.payload;
      return {
        ...state,
        formData,
        remoteCfg,
      };
    },

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

  },

};
