{{ define "member/login.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">
            <button class="btn btn-danger btn-block" type="button" id="btnLogin">登录</button>
            <a class="btn btn-secondary btn-block" href="/member/reg">注册</a>
          </div>
      </div>
    </div>

  </main>

  {{ template "_footer.htm" }}
  <script type="text/javascript">
    $('#btnLogin').click(function(event) {
      if ($("#iptUserid").val()=="" || $("#iptPassword").val() == "") {
        ShowMsg("请输入用户名和密码");
        return;
      }
      $.ajax({
        type: "POST",
        dataType: 'json',
        contentType:"application/json;",
        url: "/api/v1/login",
        data: JSON.stringify({
          userid: $("#iptUserid").val(),
          password: $("#iptPassword").val()
        }),
        success: function(data) {
          if (data.code == 200) {
            SetCookie(TokenName(), data.token, 1);
            localStorage.setItem('time_login', NowUnix);
            DirectTo('/member')
          } else {
            ShowMsg("登录失败，用户名或者密码可能错误");
          }
        },
        error: function() {
          ShowMsg("登录失败");
        }
      });
    });

    // 如果已经登录了，直接去会员中心首页
    var gokeep_token = GetCookie(TokenName())

    $.ajax({
      type: "GET",
      dataType: 'json',
      contentType:"application/json;",
      url: "/api/v1/auth/member/info",
      beforeSend: function(xhr) {
        xhr.setRequestHeader('Authorization', 'Bearer ' + gokeep_token);
      }
    }).done(function(data){
      if (data.code == 0) {
        DirectTo("/member/");
      }
    });

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

</html>
{{ end }}