import { connect } from 'herculex';
import { navigateTo } from '../../utils';
import { getIn } from 'herculex/dist/utils/manipulate';

const fn = () => null;
Component(connect({
  mapStateToProps: {
    // 取global中的数据
    scopes: state => state.$global.scopes,
    userInfo: state => state.$global.userInfo,
  },
})({
  props: {
    className: '', // 自定义样式
    formClass: '',
    item: {},
    style: '',
    onCallBack2: fn,
    onCallBack: fn,
    onError: fn,
    needLogin: false, // 是否展示登录形式的按钮
    needCallBack: false, // 单独处理
    needHandleError: false, // 单独处理
  },
  data: {},
  didUpdate() {
  },
  methods: {
    onSubmit(e) {
      const formId = getIn(e, ['detail', 'formId'], '');
      this.commit('$global:updateFormId', { formId });
    },
    async onClickItem(e) {
      await this.onJmp(e);
    },
    async onGetAuthorize(e) {
      await this.onJmp(e);
    },
    /* 跳转逻辑 */
    async onJmp(e) {
      let customerCheckInPage = null;
      const { needCallBack, onCallBack } = this.props;
      const item = getIn(e, ['target', 'dataset', 'item'], {});
      const currentScope = getIn(e, ['target', 'dataset', 'scope'], null);
      const { index = 'N', activeIndex = 'N', accessMode } = item;
      const isTabBar = index !== 'N' && activeIndex !== 'N';
      /* 如果是tabBar的时候，防止重复点击 */
      if (isTabBar && activeIndex === index) return;
      /* 如果是会员卡，需要去检查会员卡 */
      if (accessMode.customer && item.from !== 'webview') {
        customerCheckInPage = this.dispatch.bind(this, 'customerCheck');
      }
      const jumpPara = {
        ...item,
        currentScope,
        isToService: true,
        customerCheckInPage,
        handleTabBarChange: onCallBack,
      };
      if (needCallBack) return onCallBack(item);
      navigateTo.call(this, jumpPara);
    },
    onAuthError() {
      const { needHandleError, onError } = this.props;
      if (needHandleError) onError();
    },
  },
}));
