import Config from '../../../../utils/config';
import { info, save, mapApi } from '../../services/index';
import { Toast } from 'antd-mobile';
import router from 'umi/router';
import { goIndex, goScannerPage } from '../../common/route';
import { idCardValidate } from '../../../../utils/helper';

const CONST_STATE = {
  address: {
    address: null,
    province: null,
    city: null,
    area: null,
    latitude: null,
    longitude: null,
  },
};

export default {
  state: {
    address: {
      address: null,
      province: null,
      city: null,
      area: null,
      latitude: null,
      longitude: null,
    },
  },
  reducers: {
    updateState(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    updateAddressState(state, { payload }) {
      let { address } = state;
      address = { ...address, ...payload };
      return { ...state, address };
    },
    clearState(state, { payload }) {
      return {
        ...CONST_STATE,
      };
    },
  },
  effects: {
    *info({ payload }, { call, put }) {
      const { rc, msg, data } = yield call(info, { ...payload });
      console.log(data, '================================');
      if (rc === Config.constant.codeSuccess) {
        if (data) {
          yield put({
            type: 'updateState',
            payload: {
              ...data,
            },
          });
          window.sessionStorage.setItem('access-user', JSON.stringify(data));
        }
      } else {
        Toast.fail(msg);
      }
    },
    *save({ payload }, { call, put, select }) {
      const authentication = yield select(state => state.authentication);
      const form = {
        ...authentication,
        ...payload,
        dogTypeList: [],
      };
      console.log(form, '================================');
      if (!form.gender || form.gender === '') {
        Toast.info('请选择性别');
        return;
      }
      if (!form.birthday || form.birthday === '') {
        Toast.info('请选择生日');
        return;
      }
      if (!form.memberName || form.memberName === '') {
        Toast.info('请填写真实姓名');
        return;
      }
      if (!form.cardNo || form.cardNo === '') {
        Toast.info('请填写身份证');
        return;
      }
      if (!idCardValidate(form.cardNo)) {
        Toast.info('请填写正确的身份证');
        return;
      }
      if (!form.address || !form.address.address) {
        Toast.info('请输入详细地址');
        return;
      }
      if (form.memberType === '20' || form.memberType === 20) {
        if (!form.kennelNameCn || form.kennelNameCn === '') {
          Toast.info('请填写犬舍中文名');
          return;
        }
        if (!form.kennelNameEn || form.kennelNameEn === '') {
          Toast.info('请填写犬舍英文名');
          return;
        }
        if (!form.kennelFciName || form.kennelFciName === '') {
          Toast.info('请填写FCI英文名');
          return;
        }
        if (!form.kennelFciNameBackup || form.kennelFciNameBackup === '') {
          Toast.info('请填写FCI备用名');
          return;
        }
      }
      if (!form.address || !form.address.city || !form.address.city) {
        Toast.info('请选择收货地址');
        return;
      }

      Toast.loading('保存中', 0, null, true);
      try {
        const { rc, msg, data } = yield call(save, { ...form });
        Toast.hide();
        if (rc === Config.constant.codeSuccess) {
          Toast.success('保存成功', 2, () => {});
          //更新用户信息
          yield put({
            type: 'authentication/info',
          });
          //goIndex();
          goScannerPage();
        } else {
          Toast.fail(msg);
        }
      } catch (e) {
        Toast.hide();
        Toast.fail('保存失败');
      } finally {
      }
    },
    *geocoder({ payload }, { call, put }) {
      const { location, poiname } = payload;
      const { status, result } = yield call(mapApi, {
        key: '457BZ-TES6V-JS7P2-UJQAQ-IJSSO-RIFBA',
        location: `${location.lat},${location.lng}`,
      });
      if (status === Config.constant.codeSuccess) {
        yield put({
          type: 'updateAddressState',
          payload: {
            address: result.address_component.street_number + poiname,
            province: result.address_component.province,
            city: result.address_component.city,
            area: result.address_component.district,
            latitude: result.location.lat,
            longitude: result.location.lng,
          },
        });
        router.goBack();
      }
    },
  },
  subscriptions: {
    // setupHistory({ dispatch, history }) {
    //   history.listen(location => {});
    // },
  },
};
