<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Simple Login</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 20px;
            background-color: #121212;
            color: #fff;
        }
        .container {
            max-width: 400px;
            margin: 0 auto;
            padding: 20px;
            background-color: #1e1e1e;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
        }
        h2 {
            text-align: center;
            color: #fc7712;
        }
        form {
            display: flex;
            flex-direction: column;
        }
        input[type=text], input[type=email], input[type=password] {
            margin-bottom: 10px;
            padding: 10px;
            border-radius: 4px;
            border: 1px solid #333;
            background-color: #2a2a2a;
            color: #fff;
        }
        .action {
            margin-top: 20px;
        }
        button {
            padding: 10px;
            border: none;
            border-radius: 4px;
            background-color: #fc7712;
            color: #fff;
            cursor: pointer;
        }
        button:hover {
            background-color: #e06900;
        }
    </style>


</head>
<body>

<div class="container">
    <h2>Login</h2>
    <form id="loginForm">
        <input type="text" id="loginUsername" placeholder="Username" required>
        <input type="password" id="loginPassword" placeholder="Password" required>
        <div class="action">
            <button type="submit">Login</button>
            <button type="button" onclick="location.href='/'">Register</button>
        </div>
    </form>
</div>

<script>
    document.getElementById('loginForm').addEventListener('submit', function(event) {
        event.preventDefault();
        const formData = {
            username: document.getElementById('loginUsername').value,
            password: document.getElementById('loginPassword').value
        }

        fetch('/login/auth', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify(formData)
        })
            .then(response => {
                if (!response.ok) {
                    throw new Error('Login failed');
                }
                return response.json();
            })
            .then(data => {
                localStorage.setItem('authToken', data.token);

                window.location.href = '/reminders';
            })
            .catch((error) => {
                console.error('Error:', error);
            });
    });
</script>

</body>
</html>

