{{ define "member/reg.htm" }}
<!doctype html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="">
  <title>注册 - 会员中心 - 看丽人</title>
  {{ template "_common.htm" .}}
</head>
<body>
  {{ template "_header.htm" .}}

  <main role="main">
    <div class="album py-3 bg-light">
      <div class="container">
        <div class="row">
          <div class="col-md-12">
              <div class="alert border-bottom">会员中心 - 注册</div>
          </div>
        </div>
          <div class="py-2">
            <label for="iptUserid" class="sr-only">账号</label>
            <input type="text" id="iptUserid" class="form-control" placeholder="输入账号，英文或者数字" required="" autofocus="">
          </div>
          <div class="py-2">
            <label for="iptPassword" class="sr-only">密码</label>
            <input type="password" id="iptPassword" class="form-control" placeholder="输入密码" required="">
          </div>
          <div class="py-2">
            <label for="iptRePassword" class="sr-only">确认密码</label>
            <input type="password" id="iptRePassword" class="form-control" placeholder="再次输入密码" required="">
          </div>
          <div class="py-2">
            <img src="/assets/img/none-captcha.png" id="imgCaptcha" onclick="javascript:getCaptcha()" style="cursor:pointer">
            <label for="iptCaptcha" class="sr-only">验证码</label>
            <input type="text" id="iptCaptcha" class="form-control" placeholder="输入上图中的验证码" required="">
          </div>
          <div class="py-2">
            <label for="iptInviterID" class="sr-only">邀请人</label>
            <input type="text" id="iptInviterID" class="form-control" placeholder="邀请人ID，没有可不填">
          </div>
          <div class="py-2">
            <button class="btn btn-danger btn-block" type="button" id="btnReg">注册</button>
            <a class="btn btn-secondary btn-block" href="/login">返回登录</a>
          </div>
      </div>
    </div>

  </main>

  {{ template "_footer.htm" }}
  <script type="text/javascript">
    var urlParams = new URLSearchParams(window.location.search);
    var userid = "";
    var captchaid = "";
    if (urlParams.get('userid') != "") {
      userid = urlParams.get('userid');
    }
    if (userid != null) userid = userid.replace(/[^0-9A-Za-z]+/g, "");

    if (userid != "") {
      $("#iptInviterID").val(userid);
    }

    function getCaptcha() {
      var num =   new Date().getTime();
      var rand = Math.round(Math.random() * 10000);
      num = num + rand;
      $.ajax({
        url: '/captcha?'+num,
        type: 'GET',
        dataType: 'json',
      })
      .done(function(data) {
        if (data.code == 0) {
          $('#imgCaptcha').attr({
            src: data.data,
          });
          captchaid = data.captchaId;
        }
      })
      .fail(function() {
        ShowMsg("验证码获取失败");
        return;
      });
    }

    $(document).ready(function() {
      getCaptcha();
    });

    $('#btnReg').click(function(event) {
      if ($("#iptUserid").val()=="" || $("#iptPassword").val() == "" || $("#iptRePassword").val() == "") {
        ShowMsg("请输入用户名和密码");
        return;
      }
      if ($("#iptPassword").val() != $("#iptRePassword").val()) {
		    ShowMsg("两次密码不同，请确保一致");
        return;
      }
      if ($("#iptCaptcha").val() == "") {
        ShowMsg("请填写验证码");
        return;
      }
      $.ajax({
        type: "POST",
        dataType: 'json',
        contentType:"application/json;",
        url: "/api/v1/reg",
        data: JSON.stringify({
          userid: $("#iptUserid").val(),
          password: $("#iptPassword").val(),
          re_password: $("#iptRePassword").val(),
          inviter_id: $("#iptInviterID").val(),
          captchaid: captchaid,
          captcha: $("#iptCaptcha").val(),
        }),
        success: function(data) {
          if (data.code == 0) {
            var footer = "<p><a href='/login' class='btn btn-primary'>立即登录</a></p>";
            ShowMsg("注册成功，请前往登录界面进行登录", {
              footer:footer,
              noClose:false,
            });
          } else if (data.code == 3004){
            ShowMsg("验证码错误，请填写正确的验证码");
          } else {
            ShowMsg("注册失败，用户名或者密码可能错误");
          }
        },
        error: function() {
          ShowMsg("注册失败");
        }
      });
    });

  </script>
  {{ template "_analytics.htm" }}
</body>

</html>
{{ end }}