<!DOCTYPE html>
<html>
  <head>
    <title>Admin Login</title>
    <!-- Bootstrap -->
    <link href="<?= base_url().'/'; ?>theme/bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
    <link href="<?= base_url().'/'; ?>theme/bootstrap/css/bootstrap-responsive.min.css" rel="stylesheet" media="screen">
    <link href="<?= base_url().'/'; ?>theme/assets/styles.css" rel="stylesheet" media="screen">
    <script src="<?= base_url().'/'; ?>theme/vendors/jquery-1.9.1.min.js"></script>
    <script src="<?= base_url().'/'; ?>theme/bootstrap/js/bootstrap.min.js"></script>
    <script src="<?= base_url().'/'; ?>js/md5.min.js"></script>
    <script src="<?= base_url().'/'; ?>js/jquery.cookie.js"></script>
  </head>
  <body id="login">
    <div class="container">

      <form class="form-signin">
        <h2 class="form-signin-heading">请登录...</h2>
        <input type="text" class="input-block-level" placeholder="用户名" id="username">
        <input type="password" class="input-block-level" placeholder="密码" id="password">
        <button class="btn btn-large btn-primary" type="button" onclick="login()">登 录</button>
        <a href="<?= base_url().'/'; ?>sign/registerPage" style="margin:12px;float:right;">没有账号，去注册?</a>
        <a href="<?= base_url().'/'; ?>" style="margin:12px;float:right;">首页</a>
      </form>

    </div>

<script>
/* *************************************
 *       登录
************************************** */
function login(){
    var username = $("#username").val();
    var password = $("#password").val();
    password = md5(password);

    if(password == "" || username == ""){
        alert("用户名或密码不能为空！");
    }else{
        var host = window.location.host;
        var url = "http://" + host + "/signCtr/login"
        var postData = {};
        postData["username"] = username;
        postData["password"] = password;
        $.ajax({
            url: url,
            data: JSON.stringify(postData),
            type: "POST",
            dataType: "json",
            contentType:"application/json",
            success: function(data) {
                if(data.status == "200"){
                    $.cookie('token', data.user.token, { expires: 7, path: '/' });
                    $.cookie('username', data.user.username, { expires: 7, path: '/' });
                    window.location.href = "http://" + host + "?username=" + data.user.username + "&token=" + data.user.token ;
                }else{
                    alert(data.message);
                }
            }
        });
    }
}
</script>
  </body>
</html>