<?php
require_once __DIR__ . '/includes/auth.php';
require_once __DIR__ . '/includes/functions.php';
require_once __DIR__ . '/includes/config.php';

startSession();

if (isLoggedIn()) {
    redirect(isAdmin() ? '/admin/index.php' : '/dashboard.php');
}

$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = trim($_POST['username'] ?? '');
    $password = $_POST['password'] ?? '';

    if ($username === '' || $password === '') {
        $error = 'Bitte Benutzername und Passwort eingeben.';
    } elseif (attemptLogin($username, $password)) {
        redirect(isAdmin() ? '/admin/index.php' : '/dashboard.php');
    } else {
        $error = 'Ungültiger Benutzername oder Passwort.';
    }
}
?>
<!DOCTYPE html>
<html lang="de">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title><?= h(SITE_NAME) ?></title>
    <link rel="stylesheet" href="/assets/css/bootstrap.min.css">
    <link rel="stylesheet" href="/assets/css/fontawesome.min.css">
    <style>
        body {
            background: linear-gradient(135deg, #0f0c29, #302b63, #24243e);
            min-height: 100vh;
        }
        .login-card {
            border: none;
            border-radius: 1rem;
            box-shadow: 0 8px 40px rgba(0,0,0,.4);
        }
        .input-group-text { background: #f8f9fa; border-right: none; }
        .form-control { border-left: none; }
        .form-control:focus { border-color: #dee2e6; box-shadow: none; }
        .input-group:focus-within .input-group-text {
            border-color: #86b7fe;
            box-shadow: none;
        }
    </style>
</head>
<body class="d-flex align-items-center justify-content-center">
    <div class="col-11 col-sm-8 col-md-5 col-lg-4 col-xl-3 py-5">
        <div class="text-center mb-4">
            <span class="display-4 text-primary"><i class="fas fa-cloud-download-alt"></i></span>
            <h1 class="h3 fw-bold text-white mt-2"><?= h(SITE_NAME) ?></h1>
            <p class="text-white-50 mb-0">Bitte melden Sie sich an</p>
        </div>

        <div class="card login-card">
            <div class="card-body p-4 p-sm-5">
                <?php if ($error !== ''): ?>
                    <div class="alert alert-danger d-flex align-items-center gap-2 py-2">
                        <i class="fas fa-exclamation-triangle flex-shrink-0"></i>
                        <?= h($error) ?>
                    </div>
                <?php endif; ?>

                <form method="post" action="" novalidate>
                    <div class="mb-3">
                        <label for="username" class="form-label fw-semibold small">Benutzername</label>
                        <div class="input-group">
                            <span class="input-group-text text-muted"><i class="fas fa-user"></i></span>
                            <input type="text" id="username" name="username" class="form-control"
                                   value="<?= h($_POST['username'] ?? '') ?>"
                                   required autofocus autocomplete="username" placeholder="Benutzername">
                        </div>
                    </div>
                    <div class="mb-4">
                        <label for="password" class="form-label fw-semibold small">Passwort</label>
                        <div class="input-group">
                            <span class="input-group-text text-muted"><i class="fas fa-lock"></i></span>
                            <input type="password" id="password" name="password" class="form-control"
                                   required autocomplete="current-password" placeholder="Passwort">
                        </div>
                    </div>
                    <button type="submit" class="btn btn-primary w-100 fw-semibold py-2">
                        <i class="fas fa-sign-in-alt me-2"></i>Anmelden
                    </button>
                </form>
            </div>
        </div>
    </div>
    <script src="/assets/js/bootstrap.bundle.min.js"></script>
</body>
</html>
?>