import { isFunction } from '../../utils/isFunction';
import { connect } from 'herculex';
import history from '../../utils/history';
import { isActive } from '../../utils';
Component(connect({
  mapStateToProps: {
    userInfo: state => state.$global.userInfo || {},
    memberLevel: state => state.$global.memberLevel || ''
  }
})({
  props: {
    componentData: {},
    goodsItem: {},
    defaultNum: 0,
    onItemChange: () => null,
    targetId: '',
    // 是否展示库存
    showStock: true,
    isGoToDetail: false,
    // 是否展示预约字段
    showBooking: false,
    // 展示自动发货
    showAuto: false
  },
  data: {},

  didMount() {},

  didUpdate() {},

  methods: {
    // 外层点击事件
    handleClick() {
      const {
        isGoToDetail,
        goodsItem,
        componentData
      } = this.props;

      if (isGoToDetail && goodsItem.skuId) {
        // 跳转商品详情
        history.push({
          title: '商品详情',
          pageType: 'goods-detail',
          query: {
            skuId: goodsItem.skuId
          },
          header: 'show',
          componentData: { ...componentData
          }
        });
      }
    },

    // 点击stepper回调 @params { number: value ,type: ADD | SUBTRACT }
    changeNum(e) {
      const {
        type
      } = e;
      const {
        onItemChange,
        goodsItem
      } = this.props;

      if (!isActive(goodsItem) && type === 'ADD') {
        return;
      }

      if (isFunction(onItemChange)) {
        onItemChange({
          skuId: goodsItem.skuId,
          number: 1,
          type
        });
      }
    },

    // 点击加号显示 计数器
    handelPlusClick() {
      const {
        onItemChange,
        goodsItem
      } = this.props; // 不在售

      if (!isActive(goodsItem)) {
        return;
      }

      if (isFunction(onItemChange)) {
        onItemChange({
          skuId: goodsItem.skuId,
          number: 1,
          type: 'ADD'
        });
      }
    }

  }
}));