/**
 * Created by zhouhuan on 2017/10/30.
 */
import { getData } from '../../utils/DataTemplate';

export default {
  namespace: 'obligeeInfo',
  state: {
    sourceData: {},
    detailData: {},
    isDetail: false,
  },
  effects: {
    *getObligeeInfo({ payload }, { put, call, select }) {
      if (payload === 'clear') {
        yield put({ type: 'clearList', payload: {} });
      }
      const { sourceData } = yield select(state => state.obligeeInfo);
      const size = sourceData.length;
      const coordinate = {
        containerType: 'module',
        containerName: 'query-DSRInfo',
        datasourceName: 'QueryDSRInfoTable',
      };
      const result = yield call(getData, coordinate, { pst: size, psz: 10, params: { pID: payload } });
      const singularData = result[0];
      yield put({ type: 'setDSRList', payload: { ...singularData } });
    },
    *getObligeeDetail({ payload }, { put, call, select }) {
      const coordinate = {
        containerType: 'module',
        containerName: 'query-DSRInfo',
        datasourceName: 'QueryDSRInfoDetails',
      };
      const result = yield call(getData, coordinate, { params: { pID: payload } });
      console.log(result);
      yield put({ type: 'getDSRDetail', payload: { ...result[0] } });
      yield put({ type: 'setDetail', payload: true });
    },
  },
  reducers: {
    getDSRDetail(state, { payload }) {
      return {
        ...state,
        detailData: payload,
      };
    },
    setDSRList(state, { payload }) {
      return {
        ...state,
        sourceData: payload,
      };
    },
    setDetail(state, { payload }) {
      return {
        ...state,
        isDetail: payload,
      };
    },
  },
};

