/**
 * 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';
import { scan } from '../../services/idcard';

export default {
  namespace: 'peopleInfo',
  state: {
    sourceData: [],
    metas: [],
    detailData: {},
    editable: false,
    disabled: true,
    types: '',
    peopleID: '',
    searchValue: '',
  },
  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 }) {
      const { clear, pxingMing } = payload;
      if (clear === 'clear') {
        yield put({ type: 'clearList', payload: {} });
      }
      const { sourceData } = yield select(state => state.peopleInfo);
      console.info(sourceData);
      const size = sourceData.length;
      const coordinate = {
        containerType: 'module',
        containerName: 'query-RKInfo',
        datasourceName: 'QueryRKTable',
      };
      console.info(`pst${size}params${pxingMing}`);
      try {
        const result = yield call(getData, coordinate, { pst: size, psz: 10, params: { pxingMing } });
        console.info(sourceData);
        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');
        }
      } catch (err) {
        console.info(err);
      }
    },
    *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 });
    },
    *DuKa(ignored, { call, put }) {
      try {
        Toast.loading('读卡中...', 0);
        const result = yield call(scan);
        console.info(result);
        if (result) {
          yield put({ type: 'setdetailData', payload: result });
        }
        Toast.loading('读卡完成', 5);
      } finally {
        Toast.loading('读卡失败', 10);
      }
      //
    },
  },
  reducers: {
    setPeopPleID(state, { payload }) {
      return {
        ...state,
        peopleID: payload,
      };
    },
    // 下拉刷新用到的，清空列表的数据
    clearList(state) {
      return { ...state, sourceData: [] };
    },
    getPeopleDetailInfo(state, { payload }) {
      return {
        ...state,
        detailData: payload,
      };
    },
    setdetailData(state, { payload: { peopleName, idcard, birthDay, people } }) {
      const detailData = {
        xingMing: peopleName,
        identityCard: idcard,
        birthday: birthDay,
        minZu: people,
      };
      return { ...state, detailData };
    },
    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,
      };
    },
    setSearchValue(state, { payload }) {
      return {
        ...state,
        searchValue: payload,
      };
    },
  },
};

