<!DOCTYPE html>
<html>
<head>
    <title>Login</title>
    <style>
        body { font-family: Arial, sans-serif; }
        .login-container { max-width: 300px; margin: 0 auto; }
        .input { margin: 10px 0; padding: 10px; width: 100%; }
        .button { padding: 10px; background: #007BFF; color: white; border: none; width: 100%; cursor: pointer; }
        .message { margin-top: 10px; }
    </style>
</head>
<body>
    <h1>Bank Of Europe - Login</h1>
    <div class="login-container">
        <input id="username" class="input" placeholder="Username" />
        <input id="password" type="password" class="input" placeholder="Password" />
        <button class="button" onclick="login()">Login</button>
    </div>
    <div id="message"></div>

    <script>

        window.onload = function() {
            const token = localStorage.getItem('token');
            if (token) {
                window.location.href = 'bank-account.html';
            }
        };


        async function login() {
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value; 

            const response = await fetch('http://localhost:3000/login', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({ username, password })
            });

            if (response.ok) {
                const data = await response.json();
                localStorage.setItem('token', data.token);
                document.getElementById('message').innerText = 'Logged in successfully! Redirecting...';
                setTimeout(() => {
                    window.location.href = 'bank-account.html';
                }, 2000);
            } else {
                document.getElementById('message').innerText = 'Login failed.';
            }
        }
    </script>
</body>
</html>
