/*
 * @Description: 这里输入文件功能
 * @Author: zhoupeng
 * @Date: 2020-05-14 16:49:16
 */

import Config from '../../../../utils/config';
import {
  checkBirthCode,
  getByIdentificationFlag,
  getByBirthCerRegCode,
  org,
  newdogchipSave,
  getPrice,
} from '../../services/index';
import { Toast } from 'antd-mobile';
import router from 'umi/router';

const COMMON_STATE = {
  orderId: null,
  price: 0,
  ecertFlag: '0',
  dnaSaveWay: '0',
  isDnaParentsCompare: '0',
  provinceName: null,
  hospitalCode: null,
  province: [
    { id: '10000', value: '北京市', label: '北京市' },
    { id: '10002', value: '上海市', label: '上海市' },
    { id: '10003', value: '天津市', label: '天津市' },
    { id: '10004', value: '重庆市', label: '重庆市' },
    { id: '10005', value: '山西省', label: '山西省' },
    { id: '10006', value: '内蒙古自治区', label: '内蒙古自治区' },
    { id: '10007', value: '辽宁省', label: '辽宁省' },
    { id: '10008', value: '吉林省', label: '吉林省' },
    { id: '10009', value: '黑龙江省', label: '黑龙江省' },
    { id: '10010', value: '江苏省', label: '江苏省' },
    { id: '10011', value: '浙江省', label: '浙江省' },
    { id: '10012', value: '安徽省', label: '安徽省' },
    { id: '10013', value: '福建省', label: '福建省' },
    { id: '10014', value: '江西省', label: '江西省' },
    { id: '10015', value: '山东省', label: '山东省' },
    { id: '10016', value: '河南省', label: '河南省' },
    { id: '10017', value: '河北省', label: '河北省' },
    { id: '10018', value: '湖南省', label: '湖南省' },
    { id: '10019', value: '湖北省', label: '湖北省' },
    { id: '10020', value: '广东省', label: '广东省' },
    { id: '10021', value: '广西壮族自治区', label: '广西壮族自治区' },
    { id: '10022', value: '海南省', label: '海南省' },
    { id: '10023', value: '四川省', label: '四川省' },
    { id: '10024', value: '贵州省', label: '贵州省' },
    { id: '10025', value: '云南省', label: '云南省' },
    { id: '10026', value: '西藏自治区', label: '西藏自治区' },
    { id: '10027', value: '陕西省', label: '陕西省' },
    { id: '10028', value: '甘肃省', label: '甘肃省' },
    { id: '10029', value: '青海省', label: '青海省' },
    { id: '10030', value: '新疆维吾尔族自治区', label: '新疆维吾尔族自治区' },
    { id: '10031', value: '香港特别行政区', label: '香港特别行政区' },
    { id: '10032', value: '澳门特别行政区', label: '澳门特别行政区' },
    { id: '10033', value: '台湾省', label: '台湾省' },
    { id: '10034', value: '宁夏回族自治区', label: '宁夏回族自治区' },
  ],
  orgList: [],
};

export default {
  state: COMMON_STATE,
  reducers: {
    clean(state, { payload }) {
      return COMMON_STATE;
    },
    updateState(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
  effects: {
    *getPrice({ payload }, { call, put, select }) {
      const {
        birthCerRegCode,
        dnaSaveWay,
        isDnaParentsCompare,
        memberCode,
        orderId,
      } = yield select(state => state.ckuApply);
      const { rc, msg, data } = yield call(getPrice, {
        birthCode: birthCerRegCode,
        dnaSaveWay,
        isDnaParentsCompare,
        memberCode,
        newMember: '0',
      });
      if (rc === Config.constant.codeSuccess) {
        var obj = {};
        obj.orderId = orderId || payload.orderId;
        obj.orderAmount = data.price || 0;
        obj.title = '血统证书';
        obj.detail = '新生犬血统证书';
        window.sessionStorage.setItem('payInfo', JSON.stringify(obj));
        router.push('/ckuh5/pay');
      } else {
        Toast.fail(msg);
      }
    },
    *org({ payload }, { call, put, select }) {
      const { dogBreed } = yield select(state => state.ckuApply);
      const { rc, msg, list } = yield call(org, { dogBreed, ...payload });
      if (rc === Config.constant.codeSuccess) {
        let orgList = [];
        list.forEach(item => {
          orgList.push({
            value: item.id,
            label: item.name,
          });
        });
        yield put({
          type: 'updateState',
          payload: {
            orgList,
            hospitalCode: null,
          },
        });
      } else {
        Toast.fail(msg);
      }
    },
    *checkBirthCode({ payload }, { call, put }) {
      Toast.loading('加载中', 0);
      const { rc, msg, data } = yield call(checkBirthCode, { ...payload });
      const user = JSON.parse(window.sessionStorage.getItem('access-user'));
      if (rc === Config.constant.codeSuccess) {
        let result = null;
        if (data.isNewDogBirthCertificate) {
          result = yield call(getByIdentificationFlag, {
            birthCerRegCode: payload.birthCode,
            identificationFlag: data.identificationFlag,
          });
        } else {
          result = yield call(getByBirthCerRegCode, {
            birthCerRegCode: payload.birthCode,
            snCode: data.snCode,
          });
        }
        Toast.hide();
        if (result.rc === Config.constant.codeSuccess) {
          yield put({
            type: 'updateState',
            payload: {
              ...result.data,
              memberCode: user.memberCode,
              memberName: user.memberName,
              ecertFlag: '0',
              dnaSaveWay: result.data.showDna ? '1' : '0',
              isDnaParentsCompare: '0',
            },
          });
        } else {
          Toast.fail(result.msg);
        }
      } else {
        Toast.hide();
        Toast.fail(msg);
      }
    },
    *save({ payload }, { call, put, select }) {
      const {
        birthCerRegCode,
        memberCode,
        callName,
        identificationFlag,
        ecertFlag,
        dnaSaveWay,
        isDnaParentsCompare,
        hospitalCode,
      } = yield select(state => state.ckuApply);
      Toast.loading('申请中', 0, null, true);
      try {
        const { rc, msg, data } = yield call(newdogchipSave, {
          birthCode: birthCerRegCode,
          newDogOwnerCode: memberCode,
          callName,
          identificationFlag,
          ecertFlag,
          newMemberFlag: '0',
          dnaSaveWay,
          isDnaParentsCompare,
          hospitalCode,
          ...payload,
        });
        Toast.hide();
        if (rc === Config.constant.codeSuccess) {
          yield put({
            type: 'updateState',
            payload: {
              orderId: data.id,
            },
          });
          yield put({
            type: 'getPrice',
            payload: {
              orderId: data.id,
            },
          });
        } else {
          Toast.fail(msg);
        }
      } catch (e) {
        Toast.hide();
        Toast.fail('申请失败');
      } finally {
      }
    },
  },
  subscriptions: {
    // setupHistory({ dispatch, history }) {
    //   history.listen(location => {});
    // },
  },
};
