import { connect } from 'herculex';
import { getIn } from 'herculex/dist/utils/manipulate';
import { createSubscribe } from 'applet-page-component';
import { myCard } from '../utils/service';
import { history, replaceStar } from '../utils/index';
Component(connect({
  mapStateToProps: {
    isLogin: state => state.$global.userInfo.isLogin,
    bookingInfo: state => getIn(state, ['$global', 'bookingInfo'], {})
  }
})(createSubscribe({
  onShow() {
    this.getMyCard();
  }

})({
  data: {
    hasCard: false,
    // 是否有就诊卡绑定，如果绑定则展示就诊卡，如果没有绑定则展示，新增或者绑定就诊卡按钮
    myCardInfo: {}
  },
  props: {
    componentData: {}
  },

  didMount() {
    // 我的证件
    this.getMyCard();
  },

  methods: {
    async getMyCard() {
      const [, res] = await myCard();

      if (res) {
        res.viewCardNum = replaceStar(res.cardNum, 3, 4);
        this.setData({
          myCardInfo: res,
          hasCard: true
        });
      } else {
        this.setData({
          hasCard: false
        });
      }
    },

    toDetail() {
      const [item] = this.props.componentData.serviceList;
      const [, pageUuid] = item.url.split(':');
      const {
        myCardInfo
      } = this.data;
      history.openPage({
        pageType: 'card-detail',
        data: {
          cardNum: myCardInfo.cardNum,
          type: 0,
          pageUuid
        }
      });
    }

  }
})));