var app = getApp()
var that=null
var sopenFrame0=null
var wait=120
var updateing=0;
Page( {
  data: {
    IFDefault:0,
    username:"",
    phone:"",
    getcode0:"获取验证码",
    psd:"",
    tguid:0,
    nav:1,
    loginbutton:"会员登录",
    loginbutton2:"微信一键登录"
  }, 

  onLoad: function() {
      that = this
      that.rdata()
      var tguid = wx.getStorageSync('tguid');
      if(tguid){
        that.setData({
            tguid:tguid
        })
      }
    },onPullDownRefresh: function () {
      //在当前页面显示导航条加载动画
       wx.showNavigationBarLoading(); 
      //显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
      wx.showLoading({
          title: '刷新中...',
      })
      that.rdata()
       //隐藏loading 提示框
       wx.hideLoading();
       //隐藏导航条加载动画
       wx.hideNavigationBarLoading();
       //停止下拉刷新
       wx.stopPullDownRefresh();
  },rdata: function() {
        var LoginData = wx.getStorageSync("LoginData")
        if(LoginData){
            var lData = LoginData.split('|')
            that.setData({
                IFDefault:1,
                username:lData[0],
                psd:lData[1]
            })
        }
    },getphoneValue:function(e) {
      that.setData({
        phone:e.detail.value.replace(/\s+/g, "")
      })
       
  },getcode0:function(e) {
      var userId = wx.getStorageSync("userId")
      var LoginId = wx.getStorageSync("LoginId")
      var b=true
     
      if(b){
        if(updateing==0){
          updateing=1
        wx.request({
          url: app.globalData.apiurl + "/xapiajax.ashx", 
          data: { 
               action:"sendphonecode0",
               username:that.data.phone,
               phone:that.data.phone
          },
          method:"POST",
          dataType:"json", 
          header: { 
            "content-type": "application/x-www-form-urlencoded" // 默认值
          },
          success (res) { 
            updateing=0
            if(res.data.status==1){
              wx.showToast({
                title: res.data.msg,
                icon: "success",
                duration: 3000
               })
               that.gettime0()
            }else{
               wx.showToast({
                title: res.data.msg,
                icon: "none",
                duration: 3000
               })
            }
            }
        })
        }
      }
    },  
    gettime0:function() {
        if (wait == 0) {
            that.setData({
                getcode0:"获取验证码"
            })
            wait = 120;
            updateing=0
        } else {
            that.setData({
                 getcode0: "重新发送(" + wait + ")"
             })
            wait--;
            setTimeout(function() {
                that.gettime0()
            }, 1000)
        }

    },login:function(e) {
      var username = ""
      var password= ""
      if(that.data.nav==0){
           username = e.detail.value.txtphone.replace(/\s+/g, "");
           password=e.detail.value.txtcode.replace(/\s+/g, "");
      }else{
           username = e.detail.value.txtusername.replace(/\s+/g, "");
           password=e.detail.value.txtpsd.replace(/\s+/g, "");
      }
      var IFDefault=that.data.IFDefault
      var b=true
      
      if(username==""){
          b=false
          wx.showToast({
            title: "手机号码不能为空！",
            icon: "none",
            duration: 3000
           })
      }
      if(b){
      if(password==""){
        b=false
        wx.showToast({
          title: (that.data.nav==0?"手机验证码":"登录密码") + "不能为空！",
          icon: "none",
          duration: 3000
         })
      }
    }
    
      if(b){
        
        wx.login({
          success: function (resc) {
            
              if (resc.code) {
         var code=resc.code;
        that.setData({
            loginbutton:"请稍候..."
        })
        var tguid = wx.getStorageSync("tguid")
      wx.request({
        url: app.globalData.apiurl + "/xapiajax.ashx", 
        data: { 
           action:"login",
           username:username,
           password:password,
           t:that.data.nav,
           code:code,
           tguid:(!tguid?"":tguid),
           IFDefault:IFDefault
        }, 
        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: 2000 
             })
             wx.setStorageSync('userId', res.data.userId)
             wx.setStorageSync('LoginId', res.data.LoginId) 
             //wx.setStorageSync('latitude', res.data.latitude)
             //wx.setStorageSync('longitude', res.data.longitude) 
             wx.setStorageSync('userInfo', JSON.stringify(res.data.userInfo))
             wx.setStorageSync("openid", res.data.userInfo.openid);
             wx.setStorageSync('IFLogin', 1) 
             wx.removeStorageSync('IFYYD')
             app.globalData.IFRefreshmy=1 
             app.globalData.IFRefreshshangquan=1
             app.globalData.IFRefreshindex=1 
             app.globalData.IFRefreshshop=1 
             if(that.data.IFDefault==1){
                  wx.setStorageSync('LoginData', username+"|"+password)
             }else{
                  wx.removeStorageSync('LoginData')
             }
            
             sopenFrame0=setInterval(function (){
                   clearInterval(sopenFrame0);
                   wx.navigateBack({})
                   
             }, 500)
            
          }else{
             wx.showToast({
              title: res.data.msg,
              icon: "none",
              duration: 3000
             })
             that.setData({
              loginbutton:"会员登录"
          })
          }
          }
      }) 
    }
  }
})
    }
    },weixinlogin:function(e) {
        that.setData({
            loginbutton2:"请稍候..."
        })
        app.globalData.IFwxLogin=1
        app.getUserInfo()
        
    },wjmm:function(e) {
      wx.navigateTo({  
        url: "../forgetPassword/index"
     })
  },register:function(e) {
    wx.navigateTo({  
      url: "../register/index"
    }) 
  },changeisDefault:function(e) {
    var ifdefault = (e.detail.value==true?1:0)
    that.data.IFDefault=ifdefault
   
  },onnav:function(e) {
    var index = e.currentTarget.dataset.id
    that.setData({
        nav:index
    })
  }  
})
