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

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

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.id)
    this.data.id = options.id
    this.data.sellId = options.id;
    if (app.globalData.openId) {
      this.getTicketUser()
    } else {
      app.globalData.openidSuccessFuc = this.getTicketUser;
    }

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

  getShareId:function(){
    wx.showLoading({
      title: '加载中',
    })
    app.wxRequest(app.globalData.httpUrl + 'couponsell/presentid', { coupon_sell_id: this.data.sellId}, e => {
      console.log(e)
      if (e.code == 200) {
        wx.hideLoading();
        this.setData({
          shareId: e.present_id,
          showMask:true,
        })
      }
    }, this)
  },
  shareBtn:function(){
    console.log(this.data.buyNumber)
    if (!this.data.buyNumber){
      wx.showToast({
        title: '请先输入赠送好友优惠券的数量',
        icon:"none",
        duration:2000
      })
      return;
    } else if (this.data.buyNumber==0){
      wx.showToast({
        title: '赠送好友优惠券的数量最小为1',
        icon: "none",
        duration: 2000
      })
      return;
    }
    if (!this.data.yesShare) {
      this.data.yesShare = true;
      this.getShareId();
    }
  },
  hideMask:function(){
    this.data.yesShare = false;
    this.setData({
      showMask: false,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

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

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

  },

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

  },

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

  },

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

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (options) {
    this.setData({
      showMask: false,
      yesShare:false,
    })
    var shareUrl = "";//分享指定路径
    var titleWord = "";//title
    var shareImg = "";//分享图
    if (options.from === "button"){
      shareUrl = '/pages/receiveTicket/receiveTicket?shareId=' + this.data.shareId + "&number=" + this.data.buyNumber
      titleWord = "您的好友分享给您一张老门框爆肚涮肉的代金券，请您查收"
      shareImg = "../images/shareImg.png";
    }else{
      shareUrl = "";
      titleWord = "老门框爆肚会员"
      shareImg = "";
    }
    return{
      title: titleWord,
      imageUrl: shareImg,
      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 == 0) {
      //   wx.showToast({
      //     title: "赠送数量最小为1",
      //     icon:"none",
      //     duration: 2000
      //   })
      //   this.setData({
      //     buyNumber: 1,
      //     ticketNum: 1
      //   })
      // }else{
      //   this.setData({
      //     buyNumber: e.detail.value,
      //     ticketNum: e.detail.value
      //   })
      // }
      this.setData({
        buyNumber: e.detail.value,
        ticketNum: e.detail.value
      })
    } else{
      wx.showToast({
        title: "您赠送数量不能大于您已有数量",
        icon: "none",
        duration:2000
      })
      setTimeout(() => {
        this.setData({
          buyNumber: 1,
          ticketNum: 1
        })
      },1000)
    }
  },
})