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

export default {
  namespace: 'peopleInfo',
  state: {
    sourceData: [],
    metas: [],
    detailData: {},
    editable: false,
    disabled: true,
    types: '',
    peopleID: '',
  },
  effects: {
    *jumpPage({ payload: id }, { put }) {
      yield put({ type: 'setPeopPleID', payload: id });
      yield put(NavigationActions.navigate({
        routeName: 'peopleInfoDetail',
        params: { id },
      }));
    },
    *getPeopleInfo({ payload }, { put, call, select, take }) {
      console.log(payload);
      if (payload === 'clear') {
        yield put({ type: 'clearList', payload: {} });
      }
      const { sourceData } = yield select(state => state.peopleInfo);
      const size = sourceData.length;
      const coordinate = {
        containerType: 'module',
        containerName: 'query-RKInfo',
        datasourceName: 'QueryRKTable',
      };
      const result = yield call(getData, coordinate, { pst: size, psz: 10 });
      const meta = yield call(getMeta, coordinate);
      yield put({ type: 'setMetas', payload: meta });
      for (const row of result) {
        yield put({ type: 'addPeople', payload: row });
        yield take('addPeople/@@end');
      }
    },
    *addPeople({ payload }, { put }) {
      yield put({ type: 'setPeopleList', payload: { arrayData: [payload] } });
    },
    *getPeopleDetail({ payload }, { put, call }) {
      const coordinate = {
        containerType: 'module',
        containerName: 'query-RKInfo',
        datasourceName: 'QueryRKDetails',
      };
      const result = yield call(getData, coordinate, { params: { pID: payload } });
      console.log(result);
      yield put({ type: 'getPeopleDetailInfo', payload: { ...result[0] } });
      yield put({ type: 'setEdit', payload: false });
    },
    *savePeopleInfo({ payload }, { put, call }) {
      console.log(payload);
      const coordinate = {
        containerType: 'module',
        containerName: 'query-RKInfo',
        datasourceName: 'QueryRKDetails',
      };
      const api = datasourceApi(coordinate);
      yield call(api.create, payload);
      Toast.success('新增成功!', 0.5);
      yield put(NavigationActions.navigate({
        routeName: 'peopleInfoList',
      }));
    },
    *addPeopleInfo(ignore, { put }) {
      yield put(NavigationActions.navigate({
        routeName: 'addPeoppleInfo',
      }));
    },
    *updateDSRDetail({ payload }, { call, put }) {
      console.log('update');
      const coordinate = {
        containerType: 'module',
        containerName: 'query-RKInfo',
        datasourceName: 'QueryRKDetails',
      };
      const api = datasourceApi(coordinate);
      yield call(api.update, payload.id, payload.singularData);
      Toast.success('修改成功!', 0.5);
      yield put(NavigationActions.navigate({
        routeName: 'peopleInfoList',
      }));
      yield put({ type: 'getPeopleInfo', payload: 'clear' });
      yield put({ type: 'setDisabled', payload: true });
    },
    *onEBack({ payload }, { put, call, select, take }) {
      if (payload === 'clear') {
        yield put({ type: 'clearList', payload: {} });
      }
      const coordinate = {
        containerType: 'module',
        containerName: 'query-RKInfo',
        datasourceName: 'QueryRKTable',
      };
      const { sourceData } = yield select(state => state.peopleInfo);
      const size = sourceData.length;
      const result = yield call(getData, coordinate, { pst: size, psz: 10 });
      const meta = yield call(getMeta, coordinate);
      yield put({ type: 'setMetas', payload: meta });
      for (const row of result) {
        yield put({ type: 'addPeople', payload: row });
        yield take('addPeople/@@end');
      }
      yield put(NavigationActions.navigate({
        routeName: 'peopleInfoList',
      }));
      yield put({ type: 'setDisabled', payload: true });
    },
    *setStatus({ payload }, { put }) {
      yield put({ type: 'setDisabled', payload: false });
      yield put({ type: 'setEdit', payload });
    },
  },
  reducers: {
    setPeopPleID(state, { payload }) {
      return {
        ...state,
        peopleID: payload,
      };
    },
    // 下拉刷新用到的，清空列表的数据
    clearList(state) {
      return { ...state, sourceData: {} };
    },
    getPeopleDetailInfo(state, { payload }) {
      return {
        ...state,
        detailData: payload,
      };
    },
    setPeopleList(state, { payload: { arrayData } }) {
      return { ...state, sourceData: [...state.sourceData, ...arrayData] };
    },
    setMetas(state, { payload }) {
      return { ...state, metas: payload };
    },
    setDSRList(state, { payload }) {
      return {
        ...state,
        sourceData: payload,
      };
    },
    setEdit(state, { payload }) {
      return {
        ...state,
        editable: payload,
      };
    },
    setDisabled(state, { payload }) {
      return {
        ...state,
        disabled: payload,
      };
    },
    setTypes(state, { payload }) {
      return {
        ...state,
        types: payload,
      };
    },
  },
};

