// pages/loginModule/forgetPwd/forgetPwd.js
const { baseUrl } = require('../../../request');
const { validate } = require('../../../utils/validate');

Page({
  data: {
    phone: '',
    code: '',
    newPwd: '',
    confirmPwd: '',
    countdown: 0,
    codeButtonText: '发送验证码',
    _timer: null,
    role: '',
  },

  onLoad(options) {
    console.log(options);
    this.setData({
      role: options.role
    })
  },

  // 手机号输入
  onPhoneInput(e) {
    this.setData({ phone: e.detail.value });
  },
  // 验证码输入
  onCodeInput(e) {
    this.setData({ code: e.detail.value });
  },
  // 新密码输入
  onNewPwdInput(e) {
    this.setData({ newPwd: e.detail.value });
  },
  // 确认密码输入
  onConfirmPwdInput(e) {
    this.setData({ confirmPwd: e.detail.value });
  },

  // 发送验证码
  getSmsCode() {
    const { phone, role } = this.data;
    // 1. 非空
    if (!validate(this.data, { phone: '请输入手机号' })) return;
    // 2. 格式
    if (!/^1\d{10}$/.test(phone)) {
      return wx.showToast({ title: '手机号格式不正确', icon: 'none' });
    }
    // 3. 请求验证码
    wx.request({
      url: baseUrl + '/userInfo/code/pwd',
      method: 'POST',
      data: { phoneNumber: phone, userRole: role },
      success: (res) => {
        if (res.data.code === 1) {
          wx.showToast({ title: '验证码已发送', icon: 'none' });
          this._startCountdown(60);
        } else {
          wx.showToast({ title: res.data.message, icon: 'none' });
        }
      },
      fail: () => {
        wx.showToast({ title: '发送失败，请重试', icon: 'none' });
      }
    });
  },

  // 开始倒计时
  _startCountdown(seconds) {
    this.setData({
      countdown: seconds,
      codeButtonText: `${seconds}s后重试`
    });
    if (this.data._timer) return;  // 已在倒计时中
    this.data._timer = setInterval(() => {
      let cd = this.data.countdown - 1;
      if (cd <= 0) {
        this._clearTimer();
        this.setData({
          countdown: 0,
          codeButtonText: '发送验证码'
        });
      } else {
        this.setData({
          countdown: cd,
          codeButtonText: `${cd}s后重试`
        });
      }
    }, 1000);
  },

  // 清除定时器
  _clearTimer() {
    if (this.data._timer) {
      clearInterval(this.data._timer);
      this.data._timer = null;
    }
  },

  // 重置密码
  resetPassword() {
    const { phone, code, newPwd, confirmPwd, role } = this.data;
    // 1. 非空校验
    if (!validate(this.data, {
      phone: '请输入手机号',
      code: '请输入验证码',
      newPwd: '请输入新密码',
      confirmPwd: '请再次输入新密码'
    })) return;
    // 3. 格式校验手机号
    if (!/^1\d{10}$/.test(phone)) {
      return wx.showToast({ title: '手机号格式不正确', icon: 'none' });
    } 
    // 2. 密码一致
    if (newPwd !== confirmPwd) {
      return wx.showToast({ title: '两次密码不一致', icon: 'none' });
    }
    // 4. 发起重置请求
    wx.request({
      url: baseUrl + '/userInfo/mini/out/reset/pwd',
      method: 'POST',
      data: {
        phoneNumber: phone,
        verificationCode: code,
        userPassword: newPwd,
        userConfirmPassword: confirmPwd,
        userRole: role
      },
      success: res => {
        if (res.data.code === 1) {
          wx.showToast({ title: '重置成功', icon: 'success' });
          // 清理并跳登录
          this._clearTimer();
          setTimeout(() => {
            wx.navigateTo({ url: '/pages/login/login' });
          }, 800);
        } else {
          wx.showToast({ title: res.data.message||'重置失败', icon: 'none' });
        }
      },
      fail: () => {
        wx.showToast({ title: '网络错误，请重试', icon: 'none' });
      }
    });
  },

  // 点击“登录账号”回登录页
  gotoLogin() {
    wx.navigateBack();
  },

  onUnload() {
    this._clearTimer();
  }
});
