const app = getApp();
Page({
  data: {
    canClick:false,
    isVivo:false,
    codeBtnTxt:"发送验证码"
  },
  onLoad: function (options) {
    // this.getDeptList();
    var that = this
    //获取手机型号，vivo手机安全键盘bug
    wx.getSystemInfo({
      success:function(res){
        if(res.brand=="vivo"){
         that.data.isVivo = true; 
        }
      }
    });
  },
  goReg(){
    var that = this
    var phoneNumber = that.data.loginName
    var phonereg = /^[1][0-9]{10}$/;
    if (!phonereg.test(phoneNumber)) {
      app.showToptip(that, "worning", "手机号输入有误")
      return;
    }
    var smsCode = that.data.code
    if (!smsCode) {
      app.showToptip(that, "worning", "验证码输入有误")
      return;
    }
    var password = that.data.password
    if (!password) {
      app.showToptip(that, "worning", "密码不能为空")
      return;
    }
    var repassword = that.data.password2
    if (password != repassword) {
      app.showToptip(that, "worning", "两次密码输入不一致")
      return;
    }
    var nickName = that.data.nickName
    if (!nickName) {
      app.showToptip(that, "worning", "姓名不能为空")
      return;
    }
    var identityId = that.data.cardNo && that.data.cardNo.replace(/\s/g, "")
    if (!identityId || [15,18].indexOf(identityId.length)<0) {
      app.showToptip(that, "worning", "身份证号输入有误")
      return;
    }
    var workUnit = that.data.workUnit
    if (!workUnit) {
      app.showToptip(that, "worning", "工作单位不能为空")
      return;
    }
    var parentDeptId = that.data.parentDeptId
    if (!parentDeptId) {
      app.showToptip(that, "worning", "请选择上级所属单位")
      return;
    }
    wx.showLoading({
      mask: true
    })
    app.requestP({
      url : "/register", 
      needToken:false,
      contentType:"application/json",
      data: JSON.stringify({
        phoneNumber,
        smsCode,
        password,
        repassword,
        nickName,
        identityId,
        workUnit,
        parentDeptId
      })
    }).then((res) => {
      app.showToptip(that,"success", res.data.msg)
      wx.redirectTo({
        url: '/pages/login/login?phoneNumber='+phoneNumber+"&password="+password,
      })
    }).catch((res) => {
      app.showToptip(that,"error", res.data.msg)
    });

  },
  cardNoChange(e) {//借款人身份证验证
    var cardNo = e.detail.value.replace(/[^\dXx]/g, "").replace(/([\d]{6})(?=[\dXx])/g, '$1  ')
    this.setData({
      cardNo: cardNo.substr(0, 22)
    })
  },
  setWorkUnit: function (e) {
    if (e.detail.cursor == this.data.workUnitcursor) return 
    var workUnit = e.detail.value
    this.setData({
      workUnitcursor: e.detail.cursor,
      workUnit: workUnit
    })
  } ,
  setNickName: function (e) {
    if (e.detail.cursor == this.data.nickNamecursor) return 
    var nickName = e.detail.value.replace(/[^\u4e00-\u9fa5\da-zA-Z]/g, "");
    if (nickName.length >= 25) {
      nickName = nickName.substring(0, 25)
    }
    this.setData({
      nickNamecursor: e.detail.cursor,
      nickName: nickName
    })
  } ,
  setLoginName: function (e) {//手机号输入校验
    var loginName = e.detail.value.replace(/[^\d]/g, "").substr(0,11)
    //验证手机号
    var myreg = /^[1][0-9]{10}$/;
    this.setData({
      loginName,
      canClick : myreg.test(loginName)
    })
  },
  setPwd: function (e) {//密码输入绑定
    var password = e.detail.value
    this.setData({
      password: password
    })
    if(this.data.isVivo){//vivo密码键盘bug，超过1秒没输入收起键盘
      if(this.timer)clearTimeout(this.timer)
      this.timer = setTimeout(function(){
        wx.hideKeyboard();
      },1000)
    }
  },
  setPwd2: function (e) {//密码输入绑定
    var password2 = e.detail.value
    this.setData({
      password2: password2
    })
    if(this.data.isVivo){//vivo密码键盘bug，超过1秒没输入收起键盘
      if(this.timer)clearTimeout(this.timer)
      this.timer = setTimeout(function(){
        wx.hideKeyboard();
      },1000)
    }
  },
  getValidCode:function(e){
    //验证手机号
    var myreg = /^[1][0-9]{10}$/;
    if (!myreg.test(this.data.loginName)) {
      app.showToptip(this, "worning", "手机号输入有误")
      return;
    }

    var that=this
    wx.showLoading({
      title: '发送中...',
      mask: true
    })
    app.requestP({
      url: "/mobileGetAuthCode",
      needToken:false,
      data: { 
        phoneNumber: that.data.loginName
      }
    }).then((res) => {
        app.showToptip(that, "success","发送成功")
        that.data.lastSendTime = Date.now()
        that.setData({
          canClick: false
        })
        //定时器触发倒计时，
        var times = 60
        var i = setInterval(function () {
          times--
          if (times == 0) {
            that.setData({
              canClick: true,
              codeBtnTxt: "获取验证码",
            })
            clearInterval(i)
          } else {
            that.setData({
              codeBtnTxt: "重新获取" + times + "s",
            })
          }
        }, 1000)
    }).catch(function(r){
      app.showToptip(that, "error",r.data.msg)
    })
  },
  setCode:function(e){//验证码输入绑定
    this.setData({
      code: e.detail.value.replace(/[^\d]/g, "").substr(0,6)
    })
  },
  setDept(e){
    var that = this
    that.setData({
      parentDeptId:that.data.deptList[e.detail.value].deptId,
      deptindex:e.detail.value
    })
  },
  getDeptList(){
    var that=this
    app.requestP({
      url: "/deptlist",
      method:"get",
      needToken:false,
    }).then((res) => {
       that.setData({
          deptList: res.data.data
       })
    })
  }
})