<!doctype html>
[#escape x as (x)!?html]
<html lang="zh-CN">
<head>
  <meta charset="utf-8">
  <title>密码重置 - ${site.title}</title>
  <meta name="_csrf" content="${_csrf.token}"/>
  <meta name="_csrf_header" content="${_csrf.headerName}"/>
  [#include 'includes_meta.html'/]
  [#include 'includes_css.html'/]
  [#include 'includes_js.html'/]
  [#include 'includes_js_validation.html'/]
  <script src="${files}/vendor/crypto-js/crypto-js.min.js"></script>
  <script src="${files}/vendor/sm-crypto/dist/sm2.js"></script>
</head>
<body class="bg-light">
<div class="container my-3 my-md-4 my-lg-5">
  <div class="row justify-content-center">
    <div class="col col-md-6 col-lg-5 col-xl-5 reg-box">
      <h3 class="reg-logo text-center">
        <a href="/"><b>UJCMS</b></a> <span>重置密码</span>
      </h3>
      <div class="reg-box-body rounded p-4 mt-4" style="background-color:#fff;border:1px solid #ccc;">
        <ul class="nav nav-pills small">
          <li class="nav-item">
            <a id="mobileTab" class="nav-link active" href="javascript:"
               onclick="$('#mobileTab').addClass('active');$('#emailTab').removeClass('active');$('.mobileField').removeClass('d-none');$('.emailField').addClass('d-none');">手机号码</a>
          </li>
          <li class="nav-item">
            <a id="emailTab" class="nav-link" href="javascript:"
               onclick="$('#emailTab').addClass('active');$('#mobileTab').removeClass('active');$('.emailField').removeClass('d-none');$('.mobileField').addClass('d-none');">邮箱地址</a>
          </li>
        </ul>
        <form class="my-2" id="validForm" action="${api}/reset-password?_method=put" method="post">
          <div class="form-group mobileField">
            <label class="sr-only" for="mobile">手机号码</label>
            <input type="text" class="form-control" id="mobile" name="mobile" placeholder="手机号码" autocomplete="off" required title="请填写手机号码"
                   data-rule-pattern="^1\d{10}$" data-msg-pattern="请填写有效的手机号码" data-rule-remote="${api}/user/mobile-exist" data-msg-remote="手机号码不存在">
          </div>
          <div class="form-group mobileField">
            <label class="sr-only" for="mobileMessageValue">短信验证码</label>
            <div class="input-group">
              <input type="text" class="form-control" id="mobileMessageValue" name="mobileMessageValue" placeholder="短信验证码" autocomplete="off" required title="请填写短信验证码"
                     data-msg-remote="短信验证码不正确">
              <input type="hidden" id="mobileMessageId" name="mobileMessageId">
              <span class="input-group-append"><button class="btn btn-secondary" id="mobileMessageButton" type="button">获取短信验证码</button></span>
            </div>
          </div>

          <div class="form-group d-none emailField">
            <label class="sr-only" for="email">邮箱地址</label>
            <input type="text" class="form-control" id="email" name="email" placeholder="邮箱地址" autocomplete="off" required title="请填写邮箱地址"
                   data-rule-email="true" data-msg-email="请填写有效的邮箱地址" data-rule-remote="${api}/user/email-exist" data-msg-remote="邮箱地址不存在">
          </div>
          <div class="form-group d-none emailField">
            <label class="sr-only" for="emailMessageValue">邮件验证码</label>
            <div class="input-group">
              <input type="text" class="form-control" id="emailMessageValue" name="emailMessageValue" placeholder="邮件验证码" autocomplete="off" required title="请填写邮件验证码"
                     data-msg-remote="邮件验证码不正确">
              <input type="hidden" id="emailMessageId" name="emailMessageId">
              <span class="input-group-append"><button class="btn btn-secondary" id="emailMessageButton" type="button">获取邮件验证码</button></span>
            </div>
          </div>

          <div class="form-group">
            <label class="sr-only" for="password">新密码</label>
            [#if config.security.passwordStrength == 1]
              [#assign dataMsgPattern='密码中必须包含字母、数字']
            [#elseif config.security.passwordStrength == 2]
              [#assign dataMsgPattern='密码中必须包含大写字母、小写字母、数字']
            [#elseif config.security.passwordStrength == 3]
              [#assign dataMsgPattern='密码中必须包含字母、数字、特殊字符']
            [#elseif config.security.passwordStrength == 4]
              [#assign dataMsgPattern='密码中必须包含大写字母、小写字母、数字、特殊字符']
            [#else]
              [#assign dataMsgPattern='密码可以是任意字符']
            [/#if]
            <input type="password" class="form-control" id="password" name="password" placeholder="新密码" autocomplete="off" title="请输入新密码" required
                   maxlength="${config.security.passwordMaxLength}"
                   data-rule-minlength="${config.security.passwordMinLength}" data-msg-minlength="密码不能少于 ${config.security.passwordMinLength} 个字符"
                   data-rule-pattern="${config.security.passwordPattern}" data-msg-pattern="${dataMsgPattern}">
          </div>
          <div class="form-group">
            <label class="sr-only" for="confirmPassword">确认密码</label>
            <input type="password" class="form-control" id="confirmPassword" placeholder="确认密码" autocomplete="off" title="请确认密码" required
                   data-rule-equalTo="#password" data-msg-equalTo="两次输入不相同">
          </div>

          <button type="submit" class="btn btn-primary btn-block">提交</button>
        </form>
      </div>
    </div>
  </div>
</div>

[#include 'includes_message_box.html'/]
[#include 'includes_short_message.html'/]

<script>
  var publicKey;
  axios.get('${api}/env/client-public-key').then(function (response) {
    publicKey = response.data;
  });

  function sm2Encrypt(message) {
    return CryptoJS.enc.Base64.stringify(CryptoJS.enc.Hex.parse('04' + sm2.doEncrypt(message, publicKey)));
  }

  $(function () {
    $('#mobile').focus().select();
    var validator = $('#validForm').validate({
      rules: {
        emailMessageValue: {
          remote: {
            url: '${api}/sms/email/try', data: {
              emailMessageId: function () {
                return $('#emailMessageId').val();
              },
              email: function () {
                return $('#email').val();
              }
            }
          }
        },
        mobileMessageValue: {
          remote: {
            url: '${api}/sms/mobile/try', data: {
              mobileMessageId: function () {
                return $('#mobileMessageId').val();
              },
              mobile: function () {
                return $('#mobile').val();
              }
            }
          }
        }
      },
      submitHandler: function (form) {
        var body = $(form).serializeJSON();
        if (body.password) {
          body.password = sm2Encrypt(body.password);
        }
        request.post(form.action, body).then(function (response) {
          showSuccess();
          location.href = '${dy}/login';
        });
      }
    });
    $('#emailMessageButton').click(function () {
      sendEmailMessage(validator, $('#email'), $('#emailMessageId'), $(this), 6);
    });
    $('#mobileMessageButton').click(function () {
      sendMobileMessage(validator, $('#mobile'), $('#mobileMessageId'), $(this), 5);
    });
  });

</script>
</body>
</html>
[/#escape]
