// pages/my/account/account.js
const app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    recharge: [{
      tit: '充600元',
      tips: '赠送300元',
      value: 600
    },
    {
      tit: '充300元',
      tips: '赠送100元',
      value: 300
    },
    {
      tit: '充100元',
      tips: '赠送20元',
      value: 100
    }],
    active: 0,
    value: 0,
    balance:false,
    amount:0,
    is_online:1
  },
  amountList:[],

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      is_online: wx.getStorageSync('is_online')
    })
    this.getwallet();
  },
  showPopup() {
    console.log(this.data.recharge[0])
    this.setData({
      show: !this.data.show,
      active: 0,
      // value: this.data.recharge[0].price
      value: this.data.recharge.length>0?[0].price : 0

    });
    this.getrecharge();
  },
  showBalance(){
    this.setData({
      balance:!this.data.balance
    })
  },

  toDrtail(e) {
    var id=e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/my/account/detail/detail?id='+id,
    })
  },

  changeCurrent(e) {
    let { index, value } = e.currentTarget.dataset
    this.setData({
      active: index,
      value
    })
  },

  inputChange(e) {
    let { value } = e.detail
    console.log(value);
    this.setData({
      value: value ? value : 0,
      active: -1
    })

  },
  getwallet() {
    var that = this;
    app.api.useApi(app.globalData.baseAppUrl + "api/wallet", {
    }, "get").then(function (res) {
        if (res.code != 200) {
            wx.showLoading({
                title: res.message.msg,
            })
            setTimeout(function () {
                wx.hideLoading({
                    success: (res) => { },
                })
            }, 2000);

        } else {
            console.log(res.message.data);
            that.setData({
              amount: res.message.data.balance,
              amountList:res.message.data.wallets.data
            })
        }

    }).catch(function (err) {
        console.log(222);
    })

},
getrecharge() {
  var that = this;
  app.api.useApi(app.globalData.baseAppUrl + "api/recharge", {
  }, "get").then(function (res) {
      if (res.code != 200) {
          wx.showLoading({
              title: res.message.msg,
          })
          setTimeout(function () {
              wx.hideLoading({
                  success: (res) => { },
              })
          }, 2000);

      } else {
          console.log(res.message.data);
          that.setData({
           
            recharge:res.message.data
          })
      }

  }).catch(function (err) {
      console.log(222);
  })

},
wxPay(){
  var that=this;
  var no=that.data.value
  app.api.useApi(app.globalData.baseAppUrl + "api/recharge/charge", {
      price: no,
}, "post").then(function (res) {
    console.log(res)
    if (res.code != 200) {
        wx.showLoading({
            title: res.message.msg,
        })
        setTimeout(function () {
            wx.hideLoading({
                success: (res) => { },
            })
        }, 1000);
    } else {
      var paydata=res.message.data
      wx.requestPayment({
          timeStamp: paydata.timeStamp,
          nonceStr: paydata.nonceStr,
          package: paydata.package,
          signType: paydata.signType,
          paySign: paydata.paySign,
          success (res) { 
              console.log(res);
      if(res.errMsg=="requestPayment:ok"){
        that.getwallet();
        wx.showLoading({
          title:"支付成功。",
      })
      setTimeout(function () {
        wx.hideLoading({
            success: (res) => { },
        })
    }, 1000);
      // this. getwallet();
      }
          },
          fail (res) { 
              wx.showLoading({
                  title: "失败，请重试！",
              })
              setTimeout(function () {
                  wx.hideLoading({
                      success: (res) => { },
                  })
              }, 1000);
              console.log(res)}
        })

    }
}).catch(function (err) {console.log(err)})
 
}
})