import { connect } from 'herculex';
Component(connect({
  mapStateToProps: {
    memberLevel: state => state.$global.memberLevel || 'NONE'
  }
})({
  props: {
    componentData: {},
    orderItem: {},
    refundDetailStatus: '',
    isMember: false,
    memberPrice: 0,
    isDetail: true
  },
  data: {},

  didMount() {
    const {
      orderItem,
      isDetail = true
    } = this.props;
    const {
      memberPrices
    } = orderItem;
    const {
      memberLevel
    } = this.data;

    if (!isDetail && memberPrices && memberLevel !== 'NONE') {
      this.setData({
        isMember: true,
        memberPrice: this.getMemberPrice(memberPrices, memberLevel)
      });
    }

    if (isDetail) {
      this.setData({
        isMember: !!orderItem.memberPriceStatus,
        memberPrice: orderItem.price
      });
    }
  },

  methods: {
    getMemberPrice(priceSetting, level) {
      if (priceSetting.length === 1) {
        // 统一会员价
        return priceSetting[0].membershipPrice;
      }

      if (level === 'NONE') {
        return priceSetting[0] ? priceSetting[0].membershipPrice : 0;
      }

      const item = priceSetting.filter(v => v.level === level);
      return item[0] ? item[0].membershipPrice : 0;
    }

  }
}));