/**
 * Created by zhouhuan on 2017/10/30.
 */
import { Toast } from 'antd-mobile';
import { NavigationActions } from 'react-navigation';
import { getData } from '../../utils/DataTemplate';
import { datasourceApi } from '../../services/datasource';

export default {
  namespace: 'obligeeInfo',
  state: {
    sourceData: {},
    detailData: {},
    metas: [],
    isDetail: false,
    editable: false,
    disabled: true,
  },
  effects: {
    *getObligeeInfo({ payload }, { put, call, select }) {
      if (payload.type === '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.id } });
      const singularData = result[0];
      yield put({ type: 'setDSRList', payload: { ...singularData } });
    },
    *getObligeeDetail({ payload }, { put, call }) {
      const coordinate = {
        containerType: 'module',
        containerName: 'query-DSRInfo',
        datasourceName: 'QueryDSRInfoDetails',
      };
      const result = yield call(getData, coordinate, { params: { pID: payload } });
      yield put({ type: 'getDSRDetail', payload: { ...result[0] } });
      yield put({ type: 'setDetail', payload: true });
      yield put({ type: 'setEdit', payload: false });
    },
    *updateDSRDetail({ payload }, { call, put }) {
      const coordinate = {
        containerType: 'module',
        containerName: 'query-DSRInfo',
        datasourceName: 'QueryDSRInfoDetails',
      };
      const api = datasourceApi(coordinate);
      yield call(api.update, payload.id, payload.singularData);
      Toast.success('修改成功!', 1);
      yield put(NavigationActions.navigate({
        routeName: 'ObligeeInfo',
      }));
      yield put({ type: 'setDisabled', payload: true });
      yield put({ type: 'setDetail', payload: false });
    },
    *onEBack({ payload }, { put, call }) {
      const coordinate = {
        containerType: 'module',
        containerName: 'query-DSRInfo',
        datasourceName: 'QueryDSRInfoTable',
      };
      const result = yield call(getData, coordinate, { params: { pID: payload } });
      const singularData = result[0];
      yield put({ type: 'setDSRList', payload: { ...singularData } });
      yield put(NavigationActions.navigate({
        routeName: 'ObligeeInfo',
      }));
      yield put({ type: 'setDisabled', payload: true });
      yield put({ type: 'setDetail', payload: false });
    },
    *setStatus({ payload }, { put }) {
      yield put({ type: 'setDisabled', payload: false });
      yield put({ type: 'setEdit', payload });
    },
  },
  reducers: {
    // 下拉刷新用到的，清空列表的数据
    clearList(state) {
      return { ...state, sourceData: {} };
    },
    getDSRDetail(state, { payload }) {
      return {
        ...state,
        detailData: payload,
      };
    },
    setDSRList(state, { payload }) {
      return {
        ...state,
        sourceData: payload,
      };
    },
    setMetas(state, { payload }) {
      return { ...state, metas: payload };
    },
    setDetail(state, { payload }) {
      return {
        ...state,
        isDetail: payload,
      };
    },
    setEdit(state, { payload }) {
      return {
        ...state,
        editable: payload,
      };
    },
    setDisabled(state, { payload }) {
      return {
        ...state,
        disabled: payload,
      };
    },
  },
};

