<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link href="./static/bootstrap.min.css" rel="stylesheet">
    <title>GUNIFLASK - Login</title>
</head>
<body style="margin:1em">
<nav aria-label="breadcrumb">
    <ol class="breadcrumb">
        <li class="breadcrumb-item"><a href="./">Home</a></li>
        <li class="breadcrumb-item active" aria-current="page">Login</li>
    </ol>
</nav>
<h2>Login</h2>
<form>
    <div class="form-group">
        <label for="usernameInput">Username</label>
        <input type="text" class="form-control" id="usernameInput" placeholder="Please input your username">
    </div>
    <div class="form-group">
        <label for="passwordInput">Password</label>
        <input type="password" class="form-control" id="passwordInput" placeholder="Please input your password">
    </div>
    <div class="form-group">
        <button type="button" class="btn btn-primary" id="loginBtn">Login</button>
    </div>
</form>
<p>Username: root, Password: 123456</p>
</body>
<script src="./static/jquery.min.js"></script>
<script>
    'use strict';
    localStorage.removeItem('user');
    $('#loginBtn').click(() => {
        let data = {
            username: $('#usernameInput').val(),
            password: $('#passwordInput').val()
        };
        $.ajax({
            url: './login',
            data: JSON.stringify(data),
            type: 'post',
            contentType: 'application/json',
            success: (res) => {
                localStorage.setItem('user', JSON.stringify(res));
                alert('登录成功');
                location.href = './';
            }
        }).fail((res) => {
            alert('登录失败');
        });
    });
</script>
</html>
