// coupon.js
const app = getApp(),
  { hostname } = app.globalData;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabIndex: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let imgUrlBefore = wx.getStorageSync('imgUrlBefore')
    this.setData({ imgUrlBefore })
  },

  onShow: function () {
    this.getActivityList()
    this.getCouponList()
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '邀请好友成为您的伙伴',
      path: '/pages/index/index?share_id=' + wx.getStorageSync('openid'),
      success: function (res) {
        // 转发成功
        wx.showToast({
          title: '分享成功',
        })
      },
      fail: function (res) {
        // 转发失败
        wx.showToast({
          title: '分享失败',
          image: '../../../images/warning.png'
        })
      }
    }
  },

  // 切换选项卡
  switchTab(e) {
    let { index } = e.target.dataset
    if (Number(index)){
      this.getCouponList()
    }else{
      this.getActivityList()
    }
    this.setData({ tabIndex: index })
  },

  // 获取活动列表
  getActivityList() {
    let that = this
    wx.request({
      url: hostname + 'coupon_activi_list',
      success: function (res) {
        console.log(res.data.data)
        let activityList = res.data.data || []
        if (activityList.length > 0) {
          activityList.forEach((value) => {
            let d = new Date
            d.setTime(Number(value.end_time) * 1000)
            value.end_time = d.getFullYear() + '-' + ((d.getMonth() + 1).toString().length < 2 ? '0' + (d.getMonth() + 1) : (d.getMonth() + 1)) + '-' + (d.getDate().toString().length < 2 ? '0' + d.getDate() : d.getDate())
            value.coupon_text = value.coupon_text.split('+')
          })
        }
        that.setData({ activityList })
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  // 获取优惠券列表
  getCouponList() {
    let that = this
    wx.request({
      url: hostname + 'coupon_list',
      data: {
        user_id: wx.getStorageSync('openid'),
      },
      success: function (res) {
        console.log(res.data.data)
        let couponList = res.data.data || []
        if (couponList.length > 0) {
          couponList.forEach((value) => {
            let d = new Date
            d.setTime(Number(value.endtime) * 1000)
            value.endtime = d.getFullYear() + '-' + ((d.getMonth() + 1).toString().length < 2 ? '0' + (d.getMonth() + 1) : (d.getMonth() + 1)) + '-' + (d.getDate().toString().length < 2 ? '0' + d.getDate() : d.getDate())
            
            d.setTime(Number(value.addtime) * 1000)
            value.addtime = d.getFullYear() + '-' + ((d.getMonth() + 1).toString().length < 2 ? '0' + (d.getMonth() + 1) : (d.getMonth() + 1)) + '-' + (d.getDate().toString().length < 2 ? '0' + d.getDate() : d.getDate())
          })
        }
        that.setData({ couponList })
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  // 领取优惠券
  getCoupon(e){
    let {index} = e.currentTarget.dataset,
    {activityList} = this.data,
    that = this
    console.log(activityList[index].id)
    wx.request({
      url: hostname + 'get_coupon',
      data: {
        user_id: wx.getStorageSync('openid'),
        activi_id: activityList[index].id
      },
      success: function(res) {
        if (res.data == 20000) {
          wx.showToast({
            title: '您已领取过了',
            image: '/images/warning.png',
            duration: 1500
          })
        }else if(res.data == 10000){
          wx.showToast({
            title: '领取成功',
            duration: 1500
          })
        }
      },
      fail: function(res) {},
      complete: function(res) {},
    })
  }
})