const app = getApp();
const util = require('../../../utils/utils.js')
Page({
  data: {
    money: '', //选择的金额
    inputValue: null, //输入框内金额（10~1000）
    currentNow: 0, //当前选择金额
    moneyList: [], //可选择的金额列表
    id: '',
    Index: 0,
    title:app.globalData.title
  },
  //选择充值金额
  choice: function (e) {
    this.setData({
      inputValue: null
    })
    let idx = e.currentTarget.dataset.idx
    this.setData({
      currentNow: idx,
      money: this.data.moneyList[idx].recharge_money,
      id: this.data.moneyList[idx].id
    })
  },
  //使用输入框时去掉选择
  enter() {
    this.setData({
      currentNow: null,
      money: ''
    })
  },
  //验证输入金额
  ckInp(e) {
    var self = this
    // let reg = /^(\+?\d{2,4}|\+?1000)$/;
    this.setData({
      inputValue: e.detail.value,
      id: 0
    })
  },
  //立即支付
  pay: util.throttle(function (e) {
    var reg = new RegExp("^([1-9]|[1-9]\\d|1000)$");
    var self = this;
    console.log(self.data.money)
    console.log(self.data.inputValue)
    if (self.data.money == '' && self.data.inputValue == null) {
      wx.showToast({
        title: '请选择或者输入充值金额',
        icon: 'none',
        duration: 2000
      })
    } else if (!reg.test(self.data.inputValue) && self.data.money == '') {
      wx.showToast({
        title: '请输入1-1000的整数',
        icon: 'none',
        duration: 2000,
      })
      self.setData({
        inputValue: null
      })
    } else {
      // wx.navigateTo({
      //   url: '/pages/personal/rechSuce/rechSuce',
      // })
      //充值接口
      self.setData({
        tap: false
      })
      if (app.globalData.req) {
        let data = {
          money: self.data.inputValue || self.data.money,
          area_id: wx.getStorageSync('home').id,
          config_id: self.data.id
        }
        app.request('/rechange/pay', data, 'POST', app.globalData.req).then(res => {
          if (res.statusCode == 200) {
            let timeStamp = res.data.timeStamp.toString();
            wx.requestPayment({
              timeStamp: timeStamp,
              nonceStr: res.data.nonceStr,
              package: res.data.package,
              signType: res.data.signType,
              paySign: res.data.paySign,
              success(res) {
                wx.navigateTo({
                  url: '/pages/personal/rechSuce/rechSuce',
                })
              },
              fail: function () {
                wx.showToast({
                  title: '支付失败',
                  icon: 'none'
                })
              }
            })
          }
        })
      } else {
        wx.showToast({
          title: '您的操作过于频繁，请稍后再试~',
          icon: 'none'
        })
      }

    }
  }, 1000),
  agreement: util.throttle(function (e) {
    //充值条约。
    wx.navigateTo({
      url: '/pages/agreement/agreement',
    })
  }, 1000),
  onLoad: function (options) {
    var that = this;
    var data = {
      'area_id': wx.getStorageSync('home').id
    }
    wx.showLoading({
      title: '加载中...',
      mask:true
    })
    app.request('/pages/recharge', data, 'Get').then(res => {
      if (res.statusCode == 200) {
        console.log(res)
        wx.hideLoading({
          complete: (res) => {},
        })
        if (res.data.data != '') {
          that.setData({
            moneyList: res.data.data,
            money: res.data.data[0].recharge_money,
            id: res.data.data[0].id
          })
        }
      }
    })
  },
  onReady: function () {

  },
  onShow: function () {

  },
  onHide: function () {

  },
  onUnload: function () {

  },
  onPullDownRefresh: function () {

  },
  onReachBottom: function () {

  },
  onShareAppMessage: function () {
    
  }
})