/*
 * @Description: 这里输入文件功能
 * @Author: zhoupeng
 * @Date: 2020-06-12 16:16:07
 */

import Config from '../../../../utils/config';
import { birthcertificateDetail, transfer } from '../../services/index';
import { Toast } from 'antd-mobile';
import { goIndex } from '../../common/route';
import router from 'umi/router';
import { getQueryVariableBy } from '../../../../utils/helper';
export default {
  state: {},
  reducers: {
    updateState(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    clean(state, { payload }) {
      return {};
    },
  },
  effects: {
    *detail({ payload }, { call, put, select }) {
      const { birthCerRegCode, identificationFlag } = yield select(state => state.transfer);
      Toast.loading('查询中...', 0, null, true);
      const { rc, msg, data } = yield call(birthcertificateDetail, {
        birthCerRegCode,
        identificationFlag,
        ...payload,
      });
      Toast.hide();
      if (rc === Config.constant.codeSuccess) {
        yield put({
          type: 'updateState',
          payload: {
            ...data,
          },
        });
      } else {
        if (rc === 1002) {
        } else {
          Toast.fail(msg);
        }
      }
    },
    *transfer({ payload }, { call, put, select }) {
      const { birthCerRegCode, identificationFlag, dogEnName, snCode } = yield select(
        state => state.transfer,
      );
      if (!birthCerRegCode || !identificationFlag || !dogEnName) {
        Toast.info('请先输入正确的登记卡编号和芯片号码', 3);
        return;
      }
      Toast.loading('登记中', 0, null, true);
      const { rc, msg, data } = yield call(transfer, {
        birthCerRegCode,
        identificationFlag,
        snCode,
        ...payload,
      });
      Toast.hide();
      if (rc === Config.constant.codeSuccess) {
        var page = getQueryVariableBy(window.location.href, 'fromPage');
        Toast.success('登记成功', 2, () => {
          if (page === 'scanning') {
            router.replace(`/ckuh5/index`);
          } else {
            goIndex();
          }
        });
      } else {
        Toast.fail(msg);
      }
    },
  },
  subscriptions: {
    // setupHistory({ dispatch, history }) {
    //   history.listen(location => {});
    // },
  },
};
