// pages/balance/balance.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mailLanguage: {},
    language: '',
    balance: 0,
    //是否开启充值
    recharge: 0,
    transfer: 0,
    withdraw: 0,
    //充值是否开放微信充值
    recharge_wx: 0,
    //充值是否开放支付宝充值
    recharge_alipay: 0,
    //提现到微信1-开 0-关
    withdrawToWechat: 0,
    withdrawToAlipay: 0,
    //余额
    member_credit2: 0.00,
    balanceLang: '余额',
    ios_virtual_pay: false,
    dataInfo: {} //保存信息
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getBalance();
    try {
      var mailLanguage = JSON.parse(wx.getStorageSync('mailLanguage'));
      if (mailLanguage) {
        this.setData({
          mailLanguage: mailLanguage
        });
      }
    } catch (e) {
      console.log(e);
    }
    try {
      let value = wx.getStorageSync('ios_virtual_pay');
      if (value) {
        this.setData({
          ios_virtual_pay: value
        });
      }
    } catch (e) { console.log(e); }
    try {
      let value = wx.getStorageSync('yz_basic_info');
      if (value) {
        value = value.balance || "余额";
        this.setData({
          balanceLang: value
        });
        wx.setNavigationBarTitle({
          title: value
        });
      }
    } catch (e) {
      // Do something when catch error
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let language = wx.getStorageSync('langIndex');
    this.setData({
      'language': language.en
    });
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },
  getBalance() {
    let that = this;
    let urlStr = app.getNetAddresss("finance.balance.balance");
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            dataInfo: res.data,
            member_credit2: res.data.credit2,
            transfer: res.data.transfer,
            recharge: res.data.recharge,
            withdraw: res.data.withdraw,
            withdrawToWechat: res.data.withdrawToWechat,
            withdrawToAlipay: res.data.withdrawToAlipay
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  godetailed() {
    wx.navigateTo({
      url: '/packageA/member/balance/detailed/detailed'
    });
  },
  //充值的响应方法
  chargeHandel() {
    if (this.data.ios_virtual_pay) {
      wx.showModal({
        title: '提示',
        content: '十分抱歉，由于相关规定，你暂时无法在这里充值！',
        confirmText: '知道了',
        showCancel: false,
        success(res) {
          if (res.confirm) { console.log(res);}
        }
      });
      return false;
    }

    if (this.data.recharge == 1) {
      wx.navigateTo({
        url: '/packageA/member/balance/balance_recharge/balance_recharge'
      });
    } else {
      wx.showToast({
        icon: 'none',
        title: '您的充值功能未开启',
        duration: 1500
      });
    }
  },
  //转账的响应方法
  transferHandel() {
    if (this.data.transfer == 1) {
      wx.navigateTo({
        url: '/packageA/member/balance/balance_transfer/balance_transfer?balance=' + this.data.member_credit2
      });
    } else {
      wx.showToast({
        icon: 'none',
        title: '您的转账功能未开启',
        duration: 1500
      });
    }
  },
  //提现的响应方法
  withdrawHandel() {
    if (this.data.withdraw == 1) {
      wx.navigateTo({
        url: '/packageA/member/balance/balance_withdrawals/balance_withdrawals'
      });
    } else {
      wx.showToast({
        icon: 'none',
        title: '您的提现功能未开启',
        duration: 1500
      });
    }
  },
  //转化爱心值
  transformingLoveV() {
    wx.navigateTo({
      url: '/packageA/member/balance/transformLoveV/transformLoveV'
    });
  },
});
