const app = getApp();
Page({
  data: {
    canClick:true,
    codeBtnTxt:"发送验证码"
  },
  onLoad: function (options) {
    this.getDeptList();
    var userInfo = wx.getStorageSync('userInfo')
    this.setData({
      loginName:userInfo.user.phonenumber
    })
  },
  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 parentDeptId = that.data.parentDeptId
    if (!parentDeptId) {
      app.showToptip(that, "worning", "请选择上级所属单位")
      return;
    }
    return false;//提交功能APP还没开发，故在此屏蔽提交
    wx.showLoading({
      mask: true
    })
    app.requestP({
      url : "/register", 
      needToken:false,
      contentType:"application/json",
      data: JSON.stringify({
        phoneNumber,
        smsCode,
        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)
    });

  },
  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)
    })
  },
  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
       })
    })
  }
})