// pages/forgotPassword/forgotPassword.js
const api = require('../../api/api');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    num: 60,
    sendTime: '获取短信验证码',
    smsFlag: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },


  /**
   * 
   * 发送验证码
   * 
   * @param {*} e 
   */
  getCode(e) {
    const that = this

    if (that.data.smsFlag) {
      wx.showToast({
        title: that.data.sendTime,
        icon: 'none'
      })
      return
    }

    var reg_tel = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
    if (!that.data.mobile || !reg_tel.test(that.data.mobile)) {
      wx.showToast({
        title: '请输入正确手机号码',
        icon: 'none',
        mask: true,
      })
    } else {
      api.sendMsg({
        mobile: that.data.mobile
      }).then(res => {
        if (res.code == 200) {
          that.inter()
          that.setData({
            sessionId: res.sessionId
          })
          wx.showToast({
            title: '已发送',
            icon: 'none',
          })
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none'
          })
        }
      }).catch(err => {

      })
    }
  },


  /**
   * 
   * 修改密码
   * @param {*} e 
   */
  formSubmit(e) {
    const that = this
    const data = e.detail.value
    data.sessionId = that.data.sessionId
    console.log("修改密码", data)
    if (!data.mobile) {
      wx.showToast({
        title: '请输入手机号码',
        icon: 'none',
      })
    } else if (!data.confirmCode) {
      wx.showToast({
        title: '请输入短信验证码',
        icon: 'none',
      })
    } else if (!data.password) {
      wx.showToast({
        title: '请输入新密码',
        icon: 'none',
      })
    } else if (!data.morePassword) {
      wx.showToast({
        title: '请再次输入新密码',
        icon: 'none',
      })
    } else if (data.morePassword !== data.password) {
      wx.showToast({
        title: '两次密码输入不一致',
        icon: 'none',
      })
    } else {
      wx.showLoading({
        title: '提交中',
      })
      api.checkConfirmCode(data, this.data.sessionId).then(res => {
        wx.hideLoading()
        if (res == 'ok') {
          wx.showToast({
            title: '修改成功！',
            mask: true,
          })
          setInterval(function () {
            wx.navigateBack()
          }, 1000)
        } else {
          wx.showToast({
            title: res,
            icon: 'none',
          })
        }
      }).catch(err => {

      })
    }
  },


  inter() {
    const that = this
    const inter = setInterval(function () {
      that.setData({
        smsFlag: true,
        sendTime: that.data.num + 's后重发',
        num: that.data.num - 1
      });
      if (that.data.num < 0) {
        clearInterval(inter)
        that.setData({
          smsFlag: false,
          sendTime: '点击重新获取',
          num: 60
        });
      }
    }, 1000)
  },



  /**
   * 
   * 输入手机号码
   * 
   * @param {*} e 
   */
  mobile(e) {
    this.setData({
      mobile: e.detail.value
    })
  },

})