<% this.title='登录' %>
<%- include('head') %>
<div class="ui error message" id="error" hidden></div>
<div class="ui middle aligned center aligned grid">
  <div class="row">
	<div class="column" style="max-width: 450px">
	  <h2 class="ui image header">
		<div class="content" style="margin-bottom: 10px; ">
		  管理员登录
		</div>
	  </h2>
	  <form class="ui large form" action="<%= web_util.makeUrl(['loginPage', 'login']) %>?nex=<%= req.query.nex %>" method="post" id="loginForm">
		<div class="ui existing segment">
		  <div class="field">
			<div class="ui left icon input">
			  <i class="user icon"></i>
			  <input name="username" placeholder="用户名" type="text" id="username" onkeydown="key_login(event)">
			</div>
		  </div>
		  <div class="field">
			<div class="ui left icon input">
			  <i class="lock icon"></i>
			  <input name="password" placeholder="密码" type="password" id="password" onkeydown="key_login(event)">
			</div>
		  </div>
		  <div class="ui fluid large submit button" id="login">登录</div>
		</div>

	  </form>
	</div>
  </div>
</div>
<script type="text/javascript">
function key_login(event) {
	if (event.keyCode == 13) {
		login();
	}
}
function show_error(error) {
swal({
	title: "登录失败。",
	icon: "error",
	text: error
  });
}
function success(pwd) {
  $("#loginForm").submit();
}
function login() {
	$("#login").addClass("loading");
	$.ajax({
		url: "/sport/loginPage/check",
		type: 'POST',
		data: {
			"username": $("#username").val(),
			"password": $("#password").val()
		},
		async: true,
		success: function(data) {
			let error_code = data.error_code;
			switch (error_code) {
				case 2001:
					show_error("用户名只允许包含大小写英文字母、数字。");
					break;
				case 2002:
					show_error("查询数据库时出现问题。");
					break;
				case 2003:
					show_error("用户名或密码不正确。");
					break;
				case 1:
					success(data.encryption);
					return;
				default:
					show_error(error_code);
					break;
			}
			$("#login").text("登录");
			$("#login").removeClass("loading");
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			show_error(XMLHttpRequest.responseText);
			$("#login").text("登录");
			$("#login").removeClass("loading");

		}
	});
}
$(document).ready(function() {
	$("#login").click(function() {
		login();
	});
});
</script>
<%- include('foot') %>