var app = getApp()
var that=null
var sopenFrame=null
var wait=120
Page( {
  data: {
    loadingHidden:false,
    umoney:0.00,
    title:"您的余额",
    txzh:"",
    txmsg:"",
    phone:"",
    savedisable:"",
    savebutton:"确认",
    getcode:"获取验证码",
    mybalance:[],
    Start: 0,
    t:0
  },

  onLoad: function(options) {
      that = this 
      if(options.t){
        that.setData({
          t:options.t
        }); 
      }
      if(that.data.t==0){
        wx.setNavigationBarTitle({
           title: "激活码互转"
        })
      }
      else if(that.data.t==1){
        wx.setNavigationBarTitle({
           title: "余额互转"
        })
      }else if(that.data.t==4){
         wx.setNavigationBarTitle({
            title: "佣金互转"
         })
         that.setData({
          title:"您的佣金"
        });
      } 
     
      that.rdata()
      that.clearCache();//清本页缓存
      that.getlist(0);
    },rdata: function() {
      var userId = wx.getStorageSync("userId")
      var LoginId = wx.getStorageSync("LoginId")
      var userInfodata = wx.getStorageSync("userInfo")
      if(userId&&LoginId&&userInfodata){
           var userInfo = JSON.parse(userInfodata)
          that.setData({
            phone:userInfo.Phone
        })
          var umoney = userInfo.jhmye
          if(that.data.t==1){
             umoney = userInfo.xjjye
          }
          if(that.data.t==4){
            umoney = userInfo.xjye
          }
          var TXEnable = userInfo.ZZEnable
          if(that.data.t==4){
            TXEnable = userInfo.ZZEnable0
          }
          if(TXEnable=="1"){
            that.setData({
              savebutton:"暂时不能转账",
              savedisable:"disabled"
             })
          }
          that.setData({
            umoney:umoney,
            loadingHidden:true
           })
         }  
    }, // 下拉刷新
    onPullDownRefresh: function () {
       //在当前页面显示导航条加载动画
     wx.showNavigationBarLoading(); 
     //显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
     wx.showLoading({
         title: '刷新中...',
     })
        that.clearCache();
        that.getlist(1);//第一次加载数据
    },

     // 页面上拉触底事件（上拉加载更多）
    onReachBottom: function () {
       that.setData({
        loadingHidden:false
        }); 
        that.getlist(2);//后台获取新数据并追加渲染
    }, 

 // 清缓存
 clearCache:function(){
    that.setData({
      loadingHidden:false,
      Start: 0,
      mybalance:[]
    }); 
},getlist: function(gp) {
      var userId = wx.getStorageSync("userId")
      var LoginId = wx.getStorageSync("LoginId")
      wx.request({
        url: app.globalData.apiurl + "/xapiajax.ashx", 
        data: { 
          action:"getzhuanzhanglist",
           Start:that.data.Start,
           t:that.data.t,
           userId:(userId==null?"":userId),
           LoginId:(LoginId==null?"":LoginId)
        },
        method:"GET",
        dataType:"json", 
        header: { 
          "content-type": "application/json" // 默认值
        },
        success (res) { 
         
            if(res.data.status==1){
              var tmpArr = null;
              tmpArr = that.data.mybalance;
              if(res.data.data.length>0){
                  tmpArr.push.apply(tmpArr,res.data.data);
              }
              that.setData({
                  mybalance: tmpArr,
                  Start: that.data.Start+res.data.data.length,
                  loadingHidden:true
              })
             
        }else{
          wx.showToast({
            title: res.data.msg,
            icon: "none",
            duration: 3000
           })
        }
        if(gp==1){
          //隐藏loading 提示框
          wx.hideLoading();
          //隐藏导航条加载动画
          wx.hideNavigationBarLoading();
          //停止下拉刷新
          wx.stopPullDownRefresh();
      }
          }
      })
    }
,getnameValue:function(e) {
      var username=e.detail.value.replace(/\s+/g, "");
      wx.request({
        url: app.globalData.apiurl + "/xapiajax.ashx", 
        data: { 
             action:"GetUserName2",
             name:username,
             t:3
        },
        method:"POST",
        dataType:"json", 
        header: { 
          "content-type": "application/x-www-form-urlencoded" // 默认值
        },
        success (res) { 
          if(res.data.status==1){
            that.setData({
                xmname:res.data.name
            })
          }else{
             /*wx.showToast({
              title: res.data.msg,
              icon: "none",
              duration: 3000
             })*/
          }
          }
      })
  },getcode:function(e) {
      var userId = wx.getStorageSync("userId")
      var LoginId = wx.getStorageSync("LoginId")
      var b=true
     
      if(b){
        wx.request({
          url: app.globalData.apiurl + "/xapiajax.ashx", 
          data: { 
               action:"sendphonecode1",
               userId:(!userId?"":userId),
               LoginId:(!LoginId?"":LoginId)
          },
          method:"POST",
          dataType:"json", 
          header: { 
            "content-type": "application/x-www-form-urlencoded" // 默认值
          },
          success (res) { 
            if(res.data.status==1){
              wx.showToast({
                title: res.data.msg,
                icon: "success",
                duration: 3000
               })
               that.gettime()
            }else{
               wx.showToast({
                title: res.data.msg,
                icon: "none",
                duration: 3000
               })
            }
            }
        })
      }
    },  
    gettime:function() {
        if (wait == 0) {
            that.setData({
                getcode:"获取验证码"
            })
            wait = 120;
        } else {
            that.setData({
                 getcode: "重新发送(" + wait + ")"
             })
            wait--;
            setTimeout(function() {
                that.gettime()
            }, 1000)
        }

    },withdrawal:function(e) {
      var userId = wx.getStorageSync("userId")
      var LoginId = wx.getStorageSync("LoginId")
      var userInfodata = wx.getStorageSync("userInfo")
      if(userId&&LoginId&&userInfodata){
           var userInfo = JSON.parse(userInfodata)
      var TXEnable = userInfo.ZZEnable
      if(TXEnable=="0"){
          var username = e.detail.value.username.replace(/\s+/g, "")
          var txje = e.detail.value.txje.replace(/\s+/g, "")
          var pwd =  e.detail.value.txpwd.replace(/\s+/g, "")
          var b=true
          if(b){
            if(username==null || username==""){
               b=false
               wx.showToast({
                  title: "对方账号不能为空！",
                  icon: "none",
                  duration: 3000
               })
             }
         }
          if(b){
             if(txje==null || txje==""){
                b=false
                wx.showToast({
                   title: "转账金额不能为空！",
                   icon: "none",
                   duration: 3000
                })
              }
          }
        
          
            if(b){
              if(pwd==null || pwd==""){
                 b=false
                 wx.showToast({
                    title: "安全密码不能为空！",
                    icon: "none",
                    duration: 3000
                 })
               }
            }
        
            if(b){
              if(that.data.savebutton=="请稍候..."){
                 b=false
                 wx.showToast({
                    title: "请稍候...",
                    icon: "none",
                    duration: 3000
                 })
               }
            }
          
          if(b){
               that.setData({
                    savebutton:"请稍候..."
               })
             
               wx.request({
                    url: app.globalData.apiurl + "/xapiajax.ashx", 
                    data: { 
                        action:"zhuanzhang",
                        userId:(!userId?"":userId),
                        LoginId:(!LoginId?"":LoginId),
                        ZZType:that.data.t,
                        UserName:username,
                        pwd:pwd,
                        je:txje
                    },
                    method:"POST",
                    dataType:"json", 
                    header: { 
                         "content-type": "application/x-www-form-urlencoded" // 默认值
                    },
                    success (res) { 
                         if(res.data.status==1){
                          wx.setStorageSync('userInfo', JSON.stringify(res.data.userInfo))
                          app.globalData.IFRefreshindex=1;
                          app.globalData.IFRefreshmy=1;
                          app.globalData.IFRefreshmybalance=1;
                          wx.showModal({
                            title: "操作提示",
                            content: res.data.msg,
                            showCancel:false,
                            success (res0) {
                              if (res0.confirm) {
                                
                                 wx.navigateBack({})
                            
                            }
                          }
                        })
                         }else{
                             wx.showToast({
                                 title: res.data.msg,
                                 icon: "none",
                                 duration: 3000
                             })
                         }
                         that.setData({
                             savebutton:"确认"
                         })
                    }
               })
          }
      }
    }
  }
})
