/**
 * Created by zhouhuan on 2017/10/30.
 */
import { Toast } from 'antd-mobile';
import { unionBy } from 'lodash';
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);
      const size = sourceData.length;
      const coordinate = {
        containerType: 'module',
        containerName: 'query-RKInfo',
        datasourceName: 'QueryRKTable',
      };
      try {
        const result = yield call(getData, coordinate, { pst: size, psz: 10, params: { pxingMing } });
        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) {
      }
    },
    *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 } });
      yield put({ type: 'getPeopleDetailInfo', payload: { ...result[0] } });
      yield put({ type: 'setEdit', payload: false });
    },
    *savePeopleInfo({ payload }, { put, call }) {
      const coordinate = {
        containerType: 'module',
        containerName: 'query-RKInfo',
        datasourceName: 'QueryRKDetails',
      };
      const api = datasourceApi(coordinate);
      const result = yield call(api.create, payload);
      if (result === null) {
        Toast.fail('信息重复!', 2);
      } else {
        Toast.success('录入成功!', 1);
      }
      yield put(NavigationActions.navigate({
        routeName: 'peopleInfoList',
      }));
    },
    *addPeopleInfo(ignore, { put }) {
      yield put({ type: 'getPeopleDetailInfo', payload: {} });
      yield put(NavigationActions.navigate({
        routeName: 'addPeoppleInfo',
      }));
    },
    *GoToPeopleDetailEditScreen({ payload: id }, { put }) {
      yield put({ type: 'setPeopPleID', payload: id });
      yield put(NavigationActions.navigate({
        routeName: 'peopleInfoDetailEdit',
      }));
    },
    *updateDSRDetail({ payload }, { call, put }) {
      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 });
    },
    *DuKa(ignored, { call, put }) {
      try {
        Toast.loading('读卡中...', 0);
        const result = yield call(scan);
        if (result) {
          yield put({ type: 'setdetailData', payload: result });
        }
        Toast.loading('读卡完成...', 2);
      } catch (e) {
        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, sex, addr } }) {
      const detailData = {
        xingMing: peopleName,
        identityCard: idcard,
        birthday: birthDay,
        minZu: people,
        xingBie: sex,
        huJiDiZhi: addr,
      };
      return { ...state, detailData };
    },
    setPeopleList(state, { payload: { arrayData } }) {
      const newSourceData = unionBy(state.sourceData, arrayData, 'id');
      return { ...state, sourceData: newSourceData };
    },
    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,
      };
    },
  },
};

