const util = require('../../../utils/util.js');
const md5 = require('../../../utils/md5.js');
const app = getApp();
const crypto=require("../../../utils/crypto")
Page({
  data: {
    userInfo: app.globalData.userInfo,
    code: '',
    password: '',
    rPassword: '',
    time: 0
  },
  onLoad(options) {
    if (options.noLogin) {
      this.setData({
        noLogin: options.noLogin
      })
    }
  },
  onShow() {
    // 更新用户信息
    this.setData({
      userInfo: app.globalData.userInfo,
    })
  },
  sendCode() {
    let that = this;
    if (that.data.time > 0) return false;
    console.log(this.data.userInfo);
    let key=crypto.encrypted(this.data.userInfo.loginName)
    console.log(key);
    util.sendMessage({ phone: this.data.userInfo.loginName, type: "rcode", time: 120,key:key }, (res) => {
      that.setData({ time: res.text })
    })
  },
  submit() {
    let that = this;
    if (util.isEmpty(this.data.code)) {
      util.showTips("请输入验证码");
      return false;
    }
    if (this.data.code.length != 6) {
      util.showTips("请输入6位验证码");
      return false;
    }
    console.log(this.data.password, this.data.rPassword)
    if (util.isEmpty(this.data.password) || util.isEmpty(this.data.rPassword)){
      util.showTips("密码不能为空");
      return false;
    }
    if (this.data.password.length < 4) {
      util.showTips("请输入至少4位密码");
      return false;
    }
    if (this.data.password!=this.data.rPassword) {
      util.showTips("两次密码不一致");
      return false;
    }
    util.ajax({
      func: '/v2/login/forget',
      data: { 'phone': that.data.userInfo.loginName, 'rcode': that.data.code, 'password': md5.hexMD5(that.data.password)}
    }, function (res) {
      if (res.code == 0) {
        wx.showModal({
          content: '密码修改成功。',
          showCancel:false,
          success(res) {
            if (res.confirm) {
              wx.navigateBack()
            } 
          }
        })
      } else {
        util.showTips(res.reason)
      }
    })
  },
  codeInput(e) {
    this.setData({ code: e.detail.value })
  },
  passwordInput(e) {
    this.setData({ password: e.detail.value })
  },
  rPasswordInput(e) {
    this.setData({ rPassword: e.detail.value })
  }
})