const app = getApp();
Page({
  data: {
    smsCode:'',
    email:'',
    codeBtnTxt: "发送验证码",
  },
  onLoad(options){
    var userId = options.userId;
    this.setData({
      userId,
    })
  },
  submitEmail(){
    const that = this
    let email = that.data.email;
    let smsCode = that.data.smsCode;
    if(!email){
      app.showToptip(that, "worning", "邮箱输入不能为空")
      return;
    }else if(!smsCode){
      app.showToptip(that, "worning", "邮箱验证码输入不能为空")
    }
    wx.showLoading({
      mask: true
    })
    app.requestP({
      url : "/user/editEmail", 
      contentType:"application/json",
      method:'post',
      data: {
        email,smsCode,userId:that.data.userId
      }
    }).then((res) => {
      app.showToptip(that,"success", res.data.msg)
      wx.switchTab({
        url: '/pages/my/my',
      })
    }).catch((res) => {
      app.showToptip(that,"error", res.data.msg)
    });
  },
  getEmailCode(){
    var that = this;
    let email = that.data.email;
    if(!email){
      app.showToptip(that, "worning", "邮箱输入不能为空")
      return;
    }
    app.requestP({
      url:'/sms/getEmailGetAuthCode',
      method:'post',
      contentType:"application/json",
      data:{
        email,
      }
    }).then(res=>{
      app.showToptip(that, "worning", res.data.msg);
      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)
      wx.navigateTo({
        url: '/pages/my/my',
      })
    }).catch(err=>{
      app.showToptip(that, "worning", err.data.msg)
    })
  },
  setEmail(e){
    this.setData({
      email:e.detail.value
    })
  },
  setEmailCode(e){
    this.setData({
      smsCode:e.detail.value
    })
  }
})