var app = getApp()
var that=null
var sopenFrame=null
var wait=120
Page( {
  data: {
    loadingHidden:false,
    AccountName:"", 
    BankName:"",
    BankCard:"", 
    BankDepos:"",
    BankNames:[],
    AliAccount:"",
    AliName:"",
    aliimage:"../../images/add.png",
    weixinimage:"../../images/add.png",
    alipic:"",
    weixinpic:"",
    phone:"",   
    getcode:"获取验证码",
    savebutton:"保存",
    nav:0,
    appbus:0,
    index:-1,
    umoney:0,
    umoney0:0
  }, 

  onLoad: function(options) {
      that = this
      if(options.t){
        that.setData({
          nav:options.t
        }); 
      }
      if(options.appbus){
        that.setData({
          appbus:options.appbus
        }); 
      }
      that.rdata()
    }, // 下拉刷新
    onPullDownRefresh: function () {
       //在当前页面显示导航条加载动画
     wx.showNavigationBarLoading(); 
     //显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
     wx.showLoading({
         title: '刷新中...',
     })
        that.rdata();//第一次加载数据
    },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,
        umoney:userInfo.xjye,
        umoney0:userInfo.zzjfye  
      })
      if(userInfo.xjye>0||userInfo.zzjfye>0){
        wx.setNavigationBarTitle({
          title: "绑定收款账号"
        })
      }
    
      wx.request({
        url: app.globalData.apiurl + "/xapiajax.ashx", 
        data: { 
          action:"getuserinfo",
           userId:(!userId?"":userId),
           LoginId:(!LoginId?"":LoginId)
        },
        method:"GET",
        dataType:"json", 
        success (res) { 
          if(res.data.status==1){
              var accountname=res.data.user.accountname
              var bankname=res.data.user.bankname
              var bankcard=res.data.user.bankcard
              var bankaddress=res.data.user.bankaddress
              var webbank=res.data.webbank
              
              var webbanks=webbank.split('|')
              var banks=""
              var index=0
              
              for(var i=0;i<webbanks.length;i++){
                    if(banks!=""){
                       banks+=",{\"key\":\""+webbanks[i]+"\"}"
                    }else{
                       banks+="{\"key\":\""+webbanks[i]+"\"}"
                    }
                    if(webbanks[i]==bankname){ 
                        index=i;
                    }
              }
                that.setData({
                    index: index,
                    AccountName:accountname,
                    BankName:bankname,
                    BankCard:bankcard,
                    BankDepos:bankaddress,
                    BankNames:JSON.parse("["+banks+"]"),
                    AliAccount:res.data.user.AliAccount,
                    AliName:res.data.user.AliName,
                    aliimage:res.data.user.aliimage,
                    weixinimage:res.data.user.weixinimage,
                    loadingHidden:true
               })
               
          }else{
            wx.showToast({
              title: res.data.msg,
              icon: "none",
              duration: 3000
             })
          }
          }
      })
    }
            
    },onnav:function(e) {
      var index = e.currentTarget.dataset.id
      that.setData({
          nav:index
      })
    },onaliimage:function(e) {
      var yimage=that.data.aliimage
      if(that.data.aliimage=="../../images/ajaxLoader.gif"){
        wx.showToast({
          title: "正在上传中...",
          icon: "none",
          duration: 3000
         })
      }else{
      wx.chooseImage({
        count: 1,
        sizeType: ['original'],
        sourceType: ['album', 'camera'],
        success (res) {
              const tempFilePaths = res.tempFilePaths;
              
              that.setData({
                aliimage:"../../images/ajaxLoader.gif"
              })
              var userId = wx.getStorageSync("userId")
      var LoginId = wx.getStorageSync("LoginId")
              wx.uploadFile({
                url: app.globalData.apiurl + "/xapiajax.ashx", //仅为示例，非真实的接口地址
                filePath: tempFilePaths[0],
                name: 'image',
                formData: {
                  action:"uploadImage",
                  userId:(!userId?"":userId),
                  LoginId:(!LoginId?"":LoginId)
                },
                fail: function(err) {
                  wx.showToast({
                    title: "上传出错了",
                    icon: "none",
                    duration: 2000
                })
                that.setData({
                  aliimage:yimage
                })
                },
                success (res0){
                    var data=JSON.parse(res0.data);
                    
                   
                    if(data.status==1){
                      wx.showToast({
                        title: data.msg,
                        icon: "success",
                        duration: 1000
                      })
                         that.setData({
                          aliimage:data.url,
                          alipic:data.pic
                        })
                    }else{
                      wx.showToast({
                        title: data.msg,
                        icon: "none",
                        duration: 2000
                    })
                      that.setData({
                        aliimage:yimage
                      })
                    }
                  
                }
              });
        }
      })
    }
    },onweixinimage:function(e) {
      var yimage=that.data.weixinimage
      if(that.data.weixinimage=="../../images/ajaxLoader.gif"){
        wx.showToast({
          title: "正在上传中...",
          icon: "none",
          duration: 3000
         })
      }else{
      wx.chooseImage({
        count: 1,
        sizeType: ['original'],
        sourceType: ['album', 'camera'],
        success (res) {
              const tempFilePaths = res.tempFilePaths;
              var userId = wx.getStorageSync("userId")
      var LoginId = wx.getStorageSync("LoginId")
              that.setData({
                weixinimage:"../../images/ajaxLoader.gif"
              })
              wx.uploadFile({
                url: app.globalData.apiurl + "/xapiajax.ashx", //仅为示例，非真实的接口地址
                filePath: tempFilePaths[0],
                name: 'image',
                formData: {
                  action:"uploadImage",
                  userId:(!userId?"":userId),
                  LoginId:(!LoginId?"":LoginId)
                },
                fail: function(err) {
                  wx.showToast({
                    title: "上传出错了",
                    icon: "none",
                    duration: 2000
                })
                that.setData({
                  weixinimage:yimage
                })
                },
                success (res0){
                    var data=JSON.parse(res0.data);
                    
                   
                    if(data.status==1){
                      wx.showToast({
                        title: data.msg,
                        icon: "success",
                        duration: 1000
                      })
                         that.setData({
                          weixinimage:data.url,
                          weixinpic:data.pic
                        })
                    }else{
                      wx.showToast({
                        title: data.msg,
                        icon: "none",
                        duration: 2000
                    })
                      that.setData({
                        weixinimage:yimage
                      })
                    }
                  
                }
              });
        }
      })
    }
    },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)
        }

    },BankChange:function(e) {
      var index=e.detail.value;
      var key=that.data.BankNames[index].key
      that.setData({
        index: index
      })
     
    },saveaddress:function(e) {
      var userId = wx.getStorageSync("userId")
      var LoginId = wx.getStorageSync("LoginId")
      var pwd =  e.detail.value.txpwd
      var b=true

      var AccountName = ""
      var BankName=""
      var BankCard=""
      var BankDepos=""
      var AliAccount=""
      var AliName=""
      var aliimage=""
      var weixinimage=""
      if(that.data.nav==0){
      AccountName = e.detail.value.AccountName
      BankName=(that.data.index==-1?"":that.data.BankNames[that.data.index].key)
      BankCard=e.detail.value.BankCard
      BankDepos=e.detail.value.BankDepos
       if(b){
      if(AccountName==null || AccountName==""){
          b=false
          wx.showToast({
            title: "开户姓名不能为空！",
            icon: "none",
            duration: 3000
           })
      }
      } 
      if(b){
      if(BankName==null || BankName==""){
        b=false
        wx.showToast({
          title: "银行名称必须选择！",
          icon: "none",
          duration: 3000
         })
      }
    }
    if(b){
      if(BankCard==null || BankCard==""){
          b=false
          wx.showToast({
            title: "开户姓名不能为空！",
            icon: "none",
            duration: 3000
           })
      }
      }
    }else if(that.data.nav==1){
        AliAccount = e.detail.value.AliAccount
        AliName = e.detail.value.AliName
        if((AliAccount=="" || AliName=="") && that.data.aliipic==""){
          b=false
          wx.showToast({
           title: "支付宝账号姓名和收款码必选一！",
           icon: "none",
           duration: 3000
          })
        }else if(that.data.aliimage=="../../images/ajaxLoader.gif"){
            b=false
            wx.showToast({
             title: "支付宝收款码正在上传中！",
             icon: "none",
             duration: 3000
            })
        }else{
             aliimage=that.data.alipic
        }
    }else if(that.data.nav==2){
      if(that.data.weixinpic==""){
        b=false
        wx.showToast({
         title: "微信收款码未上传！",
         icon: "none",
         duration: 3000
        })
      }else if(that.data.weixinimage=="../../images/ajaxLoader.gif"){
          b=false
          wx.showToast({
           title: "微信收款码正在上传中！",
           icon: "none",
           duration: 3000
          })
      }else{
        weixinimage=that.data.weixinpic
      }
  }
      if(b){
        if(pwd==null || pwd==""){
           b=false
           wx.showToast({
              title: "安全密码不能为空！",
              icon: "none",
              duration: 3000
           })
         }
      }
      if(b){
        that.setData({
          savebutton:"请稍候..."
        })
        var datas={}
        if(that.data.nav==0){
          datas={
            action:"updateBankInfo",
            userId:(!userId?"":userId),
            LoginId:(!LoginId?"":LoginId),
            AccountName:AccountName,
            BankName:BankName,
            BankCard:BankCard,
            BankDepos:BankDepos,
            pwd:pwd
          }
        }else if(that.data.nav==1){
          datas={
            action:"updatealipay",
            userId:(!userId?"":userId),
            LoginId:(!LoginId?"":LoginId),
            AliAccount:AliAccount,
            AliName:AliName,
            aliimage:aliimage,
            pwd:pwd
          }
        }else if(that.data.nav==2){
          datas={
            action:"updateweixinpay",
            userId:(!userId?"":userId),
            LoginId:(!LoginId?"":LoginId),
            weixinimage:weixinimage,
            pwd:pwd
          }
        }
      wx.request({
        url: app.globalData.apiurl + "/xapiajax.ashx", 
        data: datas,
        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)) 
            wx.showModal({
              title: "操作提示",
              content: res.data.msg,
              showCancel:false,
              success (res0) {
                if (res0.confirm) {
                  that.rdata();
                }
              }
            })
          
          }else{
             wx.showToast({
              title: res.data.msg,
              icon: "none",
              duration: 3000
             })
          }
          that.setData({
            savebutton:"保存"
        })
          }
      })
      }
    }
})
