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

  /**
   * 页面的初始数据
   */
  data: {
    buyNumber: 1,
    ticketNum: 1,
    ticketUser: [],//优惠券信息
    getBackData:"",//赠送回传字段
    surplusNum:0,//剩余张数
    yesMask:false,//提示赠送弹框
    shareId:"",//获取用户标识
    sellId:"",//优惠券券码
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.id)

    if (app.globalData.openId) {
      this.getTicketUser(options.id, 2)
    } else {
      app.globalData.openidSuccessFuc = this.getTicketUser;
    }
    this.data.sellId = options.id;
  },
  getTicketUser: function (id, from) {//获取优惠券信息
    app.wxRequest(app.globalData.httpUrl + 'couponsell/detail', { coupon_sell_id: id, from: from }, e => {
      console.log(e)
      if (e.code == 200) {
        this.setData({
          ticketUser: e.data,
          surplusNum:e.data.num,
          shareId: e.data.present_id
        })
      }
    }, this)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

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

  },

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

  },

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

  },

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

  },

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

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (options) {
    var shareUrl = "";//分享指定路径
    if (options.from === "button"){
      shareUrl = '/pages/receiveTicket/receiveTicket?shareId=' + this.data.shareId + "&number=" + this.data.buyNumber
    }
    return{
      title:"赠送给好友",
      imageUrl: this.data.ticketUser.coupon_img_small,
      path: shareUrl,
      success: res=> {
        console.log(res, "转发成功")
      },
    }
  },
  addFn: function () {
    if (this.data.ticketNum < this.data.ticketUser.num) {
      this.data.ticketNum++;
      this.setData({
        buyNumber: this.data.ticketNum
      })
    }
  },
  subtractFn: function () {
    if (this.data.ticketNum > 1) {
      this.data.ticketNum--;
      this.setData({
        buyNumber: this.data.ticketNum
      })
    }
  },
  getInputVal: function (e) {
    if (e.detail.value <= this.data.surplusNum){
      console.log(this.data.buyNumber)
      if (e.detail.value < 1) {
        wx.showToast({
          title: "赠送量最小为1",
          duration: 1000
        })
        this.setData({
          buyNumber: 1,
          ticketNum: 1
        })
      }else{
        this.setData({
          buyNumber: e.detail.value,
          ticketNum: e.detail.value
        })
      }
    } else{
      wx.showToast({
        title: "您赠送数量太大",
        duration:1000
      })
      setTimeout(() => {
        this.setData({
          buyNumber: 1,
          ticketNum: 1
        })
      },1000)
    }
  },
})