// pages/welfareMall/activityInfo/activityInfo.js
import WelfareMall from '../../../api/welfareMall';
import Activity from '../../../api/activity';
const util = require('../../../utils/util.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
     detail:[],
     activityId:'',
     statusShowText:'',
     buttonText:'',
     userMobile: '', //用户手机号
     orderInfo: [], //下单信息
     hideWindowValue:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    let  userMobile = await Activity.getMobileCache();
    if (userMobile.length !== 0) {
      this.setData({
        userMobile: userMobile,
      })
    }
    console.log(userMobile);
    if(options.activityId){
    this.getActivityDetail(options.activityId)
    }
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },
  getActivityDetail: async function(activityId) {

     let res  = await WelfareMall.getActivityDetail(activityId)

     let statusShowText = '';
     if(res.data.stock <=0){
      statusShowText == '已售罄'
     }
     if(res.data.status ==3){
      statusShowText == '已结束'
     }

     let buttonText = res.data.isPay == 1 ? '立即抢购' : '立即领取';
     this.setData({
       detail:res.data,
       statusShowText:statusShowText,
       buttonText:buttonText
     })
  },
  /**
   * 获取手机号
   * @param {*} e 
   */
  getPhoneNumber(e) {
    // this.setData({
    //   hideWindowValue:true
    // })
    // return;
    let _self = this;
    var encryptedData = e.detail.encryptedData;
    var iv = e.detail.iv;
    if (!encryptedData || encryptedData.length == 0 || !iv || iv.length == 0) {
      return;
    }
    //获取手机号
    app.doDecodePhone(encryptedData, iv, function () {
      let userMobile = Activity.getMobileCache();
      if (userMobile.length !== 0) {
        _self.setData({
          userMobile: userMobile,
        })
        _self.nowBuy();
      }
    
    });
  },

  /**
   * 下单
   */
  async nowBuy() {
    let _self = this;
    try {
      let res = await WelfareMall.createOrder(_self.data.userMobile, _self.data.detail.activityId,1);
      _self.setData({
        orderInfo: res.data,
      })
      if (this.data.detail.isPay == 1 && _self.data.orderInfo.orderSn) {
        _self.goPay();
      } else {
        _self.setData({
          hideWindowValue:true
        })
      }

    } catch (err) {
      app.showToast(err.msg);
    }

  },
   /**
   * 获取订单参数
   */
  async goPay() {
    console.log(wx.getStorageSync('loginInfo').openId)
    var _self = this;
      let result = await WelfareMall.getOrderParams(_self.data.orderInfo.orderSn,wx.getStorageSync('loginInfo').openId);
      console.log(result);
      let res = result.data
      wx.requestPayment({
        timeStamp: res.wxPrePayVo.timeStamp,
        package: res.wxPrePayVo.pack,
        nonceStr: res.wxPrePayVo.nonceStr,
        signType: res.wxPrePayVo.signType,
        paySign: res.wxPrePayVo.paySign,
        success: function (res) {
          //支付成功，跳转领取成功页面
          _self.setData({
            hideWindowValue:true
          })
          // wx.reLaunch({
          //   url: '/pages/couponSuccess/couponSuccess'
          // })
        },
        fail: function (res) {
         
        }
      })
  },
  goToOrderList(){
       wx.redirectTo({
            url: '/pages/welfareMall/historical/historical'
        })
  },

hideWindow(){
  this.setData({
    hideWindowValue:false
  })
}
})