// pages/receiveTicket/receiveTicket.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    yesGet: true,//是否被领取
    ticketUser:[],//优惠券信息
    titleWord:"",
    presentId:"",//接受的id
    ticketNum:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.globalData.present_id = options.shareId;
    this.data.presentId = options.shareId;
    app.globalData.number = options.number;
    this.setData({
      ticketNum: options.number
    })

    if (app.globalData.openId) {
      this.getTicketUser(options.shareId, options.number);
    } else {
      app.globalData.openidSuccessFuc = this.getTicketUser;
    }
  },

  getTicketUser: function (id,number) {//获取赠送优惠券信息
    app.wxRequest(app.globalData.httpUrl + 'couponbuy/presentvisit', { present_id: id, present_num: number }, e => {
      console.log(e)
      if (e.code == 201) {
        this.setData({
          ticketUser: e.data,
          yesGet: true
        })
      } else if (e.code == 202){
        this.setData({
          yesGet: false,
          titleWord:"此券已被领取"
        })
      } else if (e.code == 203){
        this.setData({
          yesGet: false,
          titleWord: "此券已失效"
        })
      } else if (e.code == -201) {
        this.setData({
          yesGet: false,
          titleWord: "没有相关赠送记录"
        })
      } else if (e.code == 204) {
        this.setData({
          yesGet: false,
          titleWord: "赠送优惠券不足，无法领取"
        })
      } else if (e.code == 205) {
        this.setData({
          yesGet: false,
          titleWord: "优惠券超限，无法领取"
        })
      }
    }, this)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // "p1583474312b94"
  getTicketFn:function(){//点击领取优惠券
    var id = this.data.presentId;
    app.wxRequest(app.globalData.httpUrl + 'couponbuy/receivepresent', { present_id: id }, e => {
      console.log(e)
      if (e.code == 200) {
        wx.showToast({
          title: '领取成功',
          duration:500
        })
        setTimeout(()=>{
          wx.switchTab({
            url: '/pages/index/index'
          })
        },500)
      }else{
        wx.showToast({
          title: e.message,
          duration: 500
        })
      }
    }, this,"POST")
  },
  backIndex:function(){//优惠券已被领取，点击确定返回首页
    wx.switchTab({
      url: '/pages/index/index'
    })
  },
})