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

  /**
   * 页面的初始数据
   */
  data: {
    yesGet: true,//是否被领取
    ticketUser:[],//优惠券信息
    userPhone:"",//手机号
    titleWord:"",
    presentId:"",//接受的id
    ticketNum:0,
    moreClick:false,//防止重复点击领取
    showAll:false,
  },

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

    var state = wx.getStorageSync('state');
    if (state == 1) {//已注册
      if (app.globalData.openId) {
        this.getTicketUser();
      } else {
        app.globalData.openidSuccessFuc = this.getTicketUser;
      }
      this.setData({
        showAll:true
      })
    } else if (state == 0) {//未注册
      wx.navigateTo({
        url: '/pages/register/register'
      })
    } else if (state == -1) {
      app.globalData.userInfoBackFn = res => {//等待结果
        if (res) {
          if (app.globalData.openId) {
            this.getTicketUser();
          } else {
            app.globalData.openidSuccessFuc = this.getTicketUser;
          }
          this.setData({
            showAll: true
          })
        } else {
          wx.navigateTo({
            url: '/pages/register/register'
          })
        }
      }
    }
  },

  getTicketUser: function () {//获取赠送优惠券信息
    app.wxRequest(app.globalData.httpUrl + 'couponbuy/presentvisit', { present_id: app.globalData.present_id, present_num: app.globalData.number }, e => {
      console.log(e)
        if (e.code == 201) {
          var partPhone = e.data.user_phone.slice(0, 3) + "****" + e.data.user_phone.slice(7)
          this.setData({
            ticketUser: e.data,
            yesGet: true,
            userPhone: partPhone
          })
        } else if (e.code == 202) {
          var partPhone = e.data.user_phone.slice(0, 3) + "****" + e.data.user_phone.slice(7)
          this.setData({
            yesGet: false,
            ticketUser: e.data,
            titleWord: e.message,
            userPhone: partPhone
          })
        } else if (e.code == 203) {
          var partPhone = e.data.user_phone.slice(0, 3) + "****" + e.data.user_phone.slice(7)
          this.setData({
            yesGet: false,
            ticketUser: e.data,
            titleWord: e.message,
            userPhone: partPhone
          })
        } else if (e.code == 204) {
          var partPhone = e.data.user_phone.slice(0, 3) + "****" + e.data.user_phone.slice(7)
          this.setData({
            yesGet: false,
            ticketUser: e.data,
            titleWord: e.message,
            userPhone: partPhone
          })
        } else if (e.code == 205) {
          var partPhone = e.data.user_phone.slice(0, 3) + "****" + e.data.user_phone.slice(7)
          this.setData({
            yesGet: false,
            ticketUser: e.data,
            titleWord: e.message,
            userPhone: partPhone
          })
        }
    }, this)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

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

  },

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

  },

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

  },

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

  },

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

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: "老门框爆肚会员",
      success: res => {
        console.log(res, "转发成功")
      },
    }
  },
  // "p1583474312b94"
  getTicketFn: function () {//点击领取优惠券
    var id = this.data.presentId;
    if (this.data.moreClick){
      return;
    }
    this.data.moreClick = true;
    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 {
        this.data.moreClick = false;
        wx.showToast({
          title: e.message,
          icon:"none",
          duration: 1000
        })
      }
    }, this, "POST")
  },
  backIndex:function(){//优惠券已被领取，点击确定返回首页
    wx.switchTab({
      url: '/pages/index/index'
    })
  },
})