// pages/buyTicket/buyTicket.js

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    buyNumber:1,//购买数量
    num : 1,
    ticketUser: [],//优惠券信息
    orderUser:[],//下单信息
    sign_num:"",//确认支付参数
    id:"",
    yesClick:false,//防止购买按钮多次点击
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.data.id = options.index;
    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.id, from: 1}, e => {
      console.log(e)
      if (e.code == 200) {
        this.setData({
          ticketUser:e.data
        })
      }
    }, this)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

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

  },

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

  },

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

  },

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

  },

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

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: "老门框爆肚会员",
      success: res => {
        console.log(res, "转发成功")
      },
    }
  },
  addFn:function(){
    // if (this.data.num<5){
      this.data.num++;
      this.setData({
        buyNumber: this.data.num
      })
    // }
  },
  subtractFn:function(){
    if (this.data.num > 1) {
      this.data.num--;
      this.setData({
        buyNumber: this.data.num
      })
    }
  },
  buySuccess:function(){//购买下单
    var state = wx.getStorageSync('state');
    if (state == 1) {//已注册
      
    } else if (state == 0) {//未注册
      wx.navigateTo({
        url: '/pages/register/register',
      })
      return;
    } else if (state == -1) {
      app.globalData.userInfoBackFn = res => {//等待结果
        console.log(res)
        if (res) {
          
        } else {
          wx.navigateTo({
            url: '/pages/register/register',
          })
          return;
        }
      }
    }

    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;
    }
    console.log(this.data.yesClick)
    if (this.data.yesClick){
      return;
    }
    this.data.yesClick = true;
    app.wxRequest(app.globalData.httpUrl + 'order/set', { coupon_sell_id: this.data.ticketUser.coupon_sell_id, buy_num: this.data.buyNumber}, e => {
      console.log(e)
      if (e.code == 200) {
        this.setData({
          orderUser: e.data.submit,
          sign_num: e.data.sign_num
        })
        wx.requestPayment({
          timeStamp: this.data.orderUser.timeStamp,
          nonceStr: this.data.orderUser.nonceStr,
          package: this.data.orderUser.package,
          signType: this.data.orderUser.signType,
          paySign: this.data.orderUser.paySign,
          success: res => {
            console.log(res)
            this.orderPay()
          },
          fail: res => {
            this.data.yesClick = false;
          },
          complete: res => {
            this.data.yesClick = false;
          }
        })
      } else if (e.code == -3003) {
        this.data.yesClick = false;
        wx.showToast({
          title: e.message,
          icon:"none",
          duration:2000
        })
      }
    }, this,"POST")
  },
  orderPay:function(){
    app.wxRequest(app.globalData.httpUrl + 'order/paid', { sign_num: this.data.sign_num}, e => {
      console.log(e)
      if (e.code == 200) {
        wx.switchTab({
          url: '/pages/personalCenter/personalCenter'
        })
      }
    }, this,"POST")
  },
  getInputVal:function(e){
    if (e.detail.value > 50) {
      wx.showToast({
        title: "最多购买50张",
        icon:"none",
        duration: 2000
      })
      this.setData({
        buyNumber: 1,
        ticketNum: 1
      })
    } else {
      this.setData({
        buyNumber: e.detail.value,
        num: e.detail.value
      })
    }
    console.log(this.data.buyNumber)
  },
})