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

if (AdminAuth::isLoggedIn()) {
    header('Location: /admin/index.php');
    exit;
}

$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $password = $_POST['password'] ?? '';
    if (AdminAuth::attempt($password)) {
        header('Location: /admin/index.php');
        exit;
    }
    $error = AdminAuth::isLockedOut($_SERVER['REMOTE_ADDR'] ?? 'unknown')
        ? 'Too many failed attempts. Try again in 15 minutes.'
        : 'Incorrect password';
}

$pageTitle = 'Login - ChuckCo Time Keeper';
require_once __DIR__ . '/../includes/header.php';
?>
<div class="container" style="max-width:400px; padding-top: 4rem;">
    <div class="card">
        <h2 class="text-center">ChuckCo Time Keeper</h2>
        <?php if ($error): ?>
            <div class="alert alert-error"><?= e($error) ?></div>
        <?php endif; ?>
        <form method="POST">
            <div class="form-group">
                <label class="form-label">Admin Password</label>
                <input type="password" name="password" class="form-input" autofocus required>
            </div>
            <button type="submit" class="btn btn-primary btn-block">Log In</button>
        </form>
    </div>
</div>
<?php require_once __DIR__ . '/../includes/footer.php'; ?>
