// pages/wxMoney/index.js
const app = getApp();
const commonUtil = require("../../../utils/commonUtil.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    balance: '0.00',
    auth: 0,
    openId: '',
    isLogin: false,
    hasUserInfo: {},
    agencyStatus: 0,
  },
  getWalletInfo: function() {
    var that = this;
    wx.request({
        url: app.globalData.ip + "/wallet/info",
        data: {
        },
        header: app.globalData.headers,
        method: "GET",
        success: function(res) {
          console.log(res)
            if(res.data.status == 200) {
              console.log(res);
              //绑定成功
              // console.log(new Number(res.data.data.balance / 100).toFixed(2));
              // console.log( commonUtil.moneyToFixed(res.data.data.balance) + '');

              that.setData({
                balance: new Number(res.data.data.balance / 100).toFixed(2),
                auth: res.data.data.auth,
                openId: res.data.data.openId,
              });
              wx.hideLoading({
                success: (res) => {},
              })
              
            } else {
              wx.showToast({
                  icon: "none",
                  title: res.data.msg,
              })
            }
        },
        fail: function(res) {
            console.log("操作失败");
        }
    })
  },
  wxLogin() {
    var that = this;
    wx.login({
        success(res) {
            if (res.code) {
                that.setData({
                    wxCode: res.code
                });

            } else {
                console.log('获取code失败！' + res.errMsg)
            }
        }
    })
},
goWithDraw(){
  wx.navigateTo({
    url: '/pages/withdraw/index',
  })

},

getUserInfoBtn: function(e) {
  console.log(e)
  let that = this;
  wx.getUserProfile({
      desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
          console.log(res)
          let encryptedData = res.encryptedData;
          let iv = res.iv;
          //发起网络请求
          wx.request({
              url: app.globalData.ip + '/decodeUserInfo',
              data: {
                encryptedData: encryptedData,
                iv: iv,
                code: that.data.wxCode,
                // type: 'dtdl',
              },
              method: "POST",
              header: app.globalData.headers,
              success: function(res) {
                  console.log(res);
                  if(res.data.status == 200) {//设置头像昵称成功，进去提现页面
                    wx.navigateTo({
                      url: '/pages/withdraw/index',
                    })
                  } else {
                    wx.showToast({
                      icon: "none",
                      title: res.data.msg,
                  })
                  }
                  
              },
              fail: function(res) {
              }
          })
        
      },
      fail: (res) => {
          console.log(res);
          wx.showToast({
            icon: "none",
            title: '获取授权失败，将影响您的提现。',
        })
      }
    })
},

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

  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.showLoading({
      title: '加载中...',
    })
    this.wxLogin();
    if (app.globalData.isLogin) {
      this.setData({
          isLogin: app.globalData.isLogin,
          hasUserInfo: app.globalData.hasUserInfo,
          agencyStatus: app.globalData.agencyStatus,
          user: app.globalData.user,
      });
      this.getWalletInfo();
    } else {
        // 由于 userLogin 是网络请求，可能会在 Page.onLoad 之后才返回  
        // 所以此处加入 callback 以防止这种情况  
        app.userLoginCallback = res => {
            this.setData({
                isLogin: app.globalData.isLogin,
                hasUserInfo: app.globalData.hasUserInfo,
                agencyStatus: app.globalData.agencyStatus,
                user: app.globalData.user,
            });
            this.getWalletInfo();
        }
    }
    wx.hideLoading()
  },

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

  },

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

  },

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

  },

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

  },

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

  }
})