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

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

  /**
   * 组件的方法列表
   */
  methods: {
    changeList() {
      const { couponList } = this.data
      if (couponList.length) {
        couponList.map(coupon => {
          coupon.couponDTOS.forEach(item => {
            item.checked = false
            item.title = coupon.couponSettingDTO.title
            item.type = coupon.couponSettingDTO.type
            item.faceAmount = coupon.couponSettingDTO.faceAmount
            item.takeCouponBgimg = coupon.couponSettingDetailDTO.style.takeCouponBgimg
            // notice 格式
            item.newNotice = JSON.parse(item.notice)
            for (let i in item.newNotice) {
              if (item.newNotice[i].type == 1) {
                item.newNotice[i].value = item.startTime.substring(0, 10) + ' 至 ' + item.endTime.substring(0, 10)
              }
            }
          })
        })
       
        wxService.nextTick(() => {
          this.setData({
            newConponOrderList: couponList
          })
        })
      }
    },
    _updateData(data) {
      if (data) {
        this.setData(data)
      }
    },
    selectCoupon(e) {
      // 1 抵用券  2 折扣券 
      const { id, index, type, amount } = e.currentTarget.dataset
      const { newConponOrderList } = this.data

      newConponOrderList.forEach(item => {
        item.couponDTOS.forEach(coupon => coupon.checked = coupon.id == id ? true : false )
      })

      let params = {
        type,
        id,
        faceAmount: amount
      }

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

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