var app = getApp()
var that=null
var sopenFrame=null
var wait=120
Page( {
  data: {
    savebutton:"保存",
    phone:"",
    getcode:"获取验证码",
    IFDefault:0
  },

  onLoad: function() {
      that = this
      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
           })
      }
    },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)
        }

    },bsubmit:function(e) {
      var code =  e.detail.value.code
      var newpwd=e.detail.value.newpwd
      var newconfpwd=e.detail.value.newconfpwd
      var b=true
     
      if(b){
        if(code==null || code==""){
           b=false
           wx.showToast({
              title: "手机验证码不能为空！",
              icon: "none",
              duration: 3000
           })
         }
      }
     
      if(b){
      if(newpwd==null || newpwd==""){
        b=false
        wx.showToast({
          title: "新密码不能为空！",
          icon: "none",
          duration: 3000
         })
      }
    }
    if(b){
      var patrn =/^(?=.{6,16}$)[0-9a-zA-Z_]+$/;
       if(!patrn.exec(newpwd)) {
        b=false
        wx.showToast({
          title: "新密码由6-16位数字、字母、下划线组成！",
          icon: "none",
          duration: 3000
         })
       }
}
    if(b){
      if(newconfpwd==null || newconfpwd==""){
        b=false
        wx.showToast({
          title: "确认新密码不能为空！",
          icon: "none",
          duration: 3000
         })
      }
    }
    if(b){
       if(newconfpwd!=newpwd){
        b=false
        wx.showToast({
          title: "两次输入新密码不一致！",
          icon: "none",
          duration: 3000
         })
       }
    }
      if(b){
        that.setData({
          savebutton:"请稍候..."
      })
      var userId = wx.getStorageSync("userId")
      var LoginId = wx.getStorageSync("LoginId")
      wx.request({
        url: app.globalData.apiurl + "/xapiajax.ashx", 
        data: { 
           action:"editpwd",
           userId:(!userId?"":userId),
           LoginId:(!LoginId?"":LoginId),
           code:code,
           newpwd:newpwd
        },
        method:"POST",
        dataType:"json", 
        header: { 
          "content-type": "application/x-www-form-urlencoded" // 默认值
        },
        success (res) { 
          if(res.data.status==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:"保存"
        })
          }
      })
    }
    }
})
