// component/selectCoupon/selectCoupon.js
const wxService = require('../../utils/wxService')

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    show: { // 显示标识
      type: Boolean,
      value: false
    },
    couponList: {
      type: Array,
      value: [],
      observer: '_couponList'
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    checkedColor: 'rgb(203, 60, 60)',
    isSelect:false
  },

  /**
   * 组件的方法列表
   */
  methods: {
    _couponList(newV, oldV) {
      console.log('newV, oldV', newV)
      if (newV !== oldV) {
       newV.map(item => {
          item.couponSettingDTO.notice = JSON.parse(item.couponSettingDTO.notice)
          for (let i in item.couponSettingDTO.notice) {
            if (item.couponSettingDTO.notice[i].type == 1) {
              item.couponSettingDTO.notice[i].value = item.couponSettingDTO.startTime.substring(0, 10) + ' 至 ' + item.couponSettingDTO.endTime.substring(0, 10)
            }
          }
        })

        this._updateData({
          couponList: newV
        })
      }
    },
    _updateData(data) {
      if (data) {
        this.setData(data)
      }
    },
    selectCoupon(e) {
      // 1 抵用券  2 折扣券 
      const { id,index,type } = e.currentTarget.dataset
      const { couponList } = this.data

      couponList.forEach(item=>{
        item.checked = false
      })
      couponList[index].checked = true

      let params = {
        type,
        id,
        faceAmount: ''
      }
      if(type == 1){
        params.faceAmount = couponList[index].couponSettingDTO.faceAmount
      } else if(type == 2){
        params.faceAmount = couponList[index].couponSettingDTO.faceAmount
      }

      wxService.nextTick(() => {
        this.triggerEvent('updateCoupon', params)
      })

      // wxService.nextTick(() => {
      //   this.triggerEvent('updateCoupon', couponList[index].couponDiscount)
      // })

      this.setData({
        couponList
      },()=>{
        this.hidePopup()})
    },
    hidePopup: function () { // 隐藏弹出框
      this.setData({
        show: false
      })
    },
  }
})
