coupon-select.js 2.28 KB
Component({
  properties: {
    show: {
      type: Boolean,
      value: false,
      observer: '_show'
    },
    coupons: {
      type: Array,
      value: [],
    },
    selectedCode: {
      type: String,
      value: '',
    },
    chosenIdx: {
      type: String,
      value: ''
    },
  },
  data: {
    // chosenIdx: ''
  },
  methods: {
    confirm: function () {
      let item = this.data.coupons[this.data.chosenIdx];
      let codeType = item && item.coupon_type|| '';
      let code = item && item.coupon_code || '';
      let amount = item && item.coupon_value_str || '';
      this.triggerEvent('confirmselect', { code, amount, codeType})
    },
    cancel: function () {
      this.triggerEvent('cancel')
    },
    check: function (e) {
      let idx = e.currentTarget.dataset.idx;
      let that = this;
      this.data.coupons.forEach((item, index) => {
        let listItem = item;
        if (index !== idx && item.isChosen) {
          this.setData({
            [`coupons[${index}].isChosen`]: false,
            [`coupons[${idx}].selected`]: 'N'
          });
        }
        if (index === idx) {
          this.setData({
            [`coupons[${idx}].isChosen`]: true,
            [`coupons[${idx}].selected`]: 'Y'
          });
        }
      });
      this.data.chosenIdx = idx;
    },
    uncheck: function (e) {
      let idx = e.currentTarget.dataset.idx;

      this.setData({
        [`coupons[${idx}].isChosen`]: false,
        [`coupons[${idx}].selected`]: 'N'
      });
      this.data.chosenIdx = '';
    },
    _show: function (show) {
      if (show) {
        if (this.data.selectedCode) {
          this.data.coupons.forEach((item, index) => {
            if (item.coupon_code === this.data.selectedCode) {
              this.setData({
                [`coupons[${index}].isChosen`]: true
              });
              this.data.chosenIdx = index;
            } else {
              this.setData({
                [`coupons[${index}].isChosen`]: false
              });
            }
          });
        } else {
          this.data.coupons.forEach((item, index) => {
              this.setData({
                [`coupons[${index}].isChosen`]: false
              });
          });
          this.data.chosenIdx = '';
        }
      }
      console.log(this.data.coupons)
    }
  }
});