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

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

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.business_id) {
      app.globalData.business_id = options.business_id;
    }
    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
    var business_id = app.globalData.business_id;
    wx.request({
      url: hostname + '/Pre/pre',
      data: {
         business_id,
         openid:wx.getStorageSync("openId")
       },
      method:"post",
      header: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;' },
      success: function (res) {
        console.log(res,"活动列表")
        var couponList = [];
        var couponListData = res.data.data;
        if (couponListData != undefined ){
          for (var i = 0; i < couponListData.length;i++){
            couponList.push(couponListData[i])
          }
          that.setData({
            couponList: couponList
          })
        }else{
          this.setData({
            couponList:[]
          })
        }
        // 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
    var business_id = app.globalData.business_id;
    var page = this.data.page
    wx.request({
      url: hostname + '/Pre/pre_list',
      method: "post",
      header: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;' },
      data: {
        business_id,
        openid: wx.getStorageSync("openId"),
        page
      },
      success: function (res) {
        console.log(res,"优惠券列表")
        var activityList = [];
        var activityListData = res.data.data;
        if (activityListData != undefined) {
          
          for (var i = 0; i < activityListData.length; i++) {
            if (activityListData[i].status == 1){
              activityList.push(activityListData[i])
            }
            
          }
          that.setData({
            activityList: activityList
          })
          console.log(activityList,"我的优惠列表")
        } else {
          that.setData({
            activityList: []
          })
        }
        // 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){
    
    var that = this
    var pre_id = e.currentTarget.dataset.pre_id
    var business_id = app.globalData.business_id;
    console.log(pre_id)
    
    wx.request({
      url: hostname + 'Pre/get_pre',
      method: "post",
      header: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;' },
      data: {
        openid: wx.getStorageSync('openId'),
        pre_id,
        business_id
      },
      success: function(res) {
        console.log(res,"领取优惠券")
        if (res.data.status == -1) {
          wx.showToast({
            title: res.data.msg,
            image: '/images/sea2.png',
            duration: 1500
          })
        } else if (res.data.status == 1){
          wx.showToast({
            title: res.data.msg,
            // image: '/images/sea2/png',
            duration: 1500
          })
        }
      },
      fail: function(res) {},
      complete: function(res) {},
    })
  }
})