<?php
/**
 * TOPBD — Admin Login
 */

require_once __DIR__ . '/../includes/db.php';
require_once __DIR__ . '/../includes/functions.php';
require_once __DIR__ . '/../includes/auth.php';

if (is_admin_logged_in()) {
    header('Location: index.php');
    exit;
}

$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $token = $_POST['csrf_token'] ?? '';
    if (!verify_csrf($token)) {
        $error = 'Invalid security token. Please try again.';
    } else {
        $username = trim($_POST['username'] ?? '');
        $password = $_POST['password'] ?? '';

        if (empty($username) || empty($password)) {
            $error = 'Please enter your username and password.';
        } elseif (admin_login($username, $password)) {
            header('Location: index.php');
            exit;
        } else {
            $error = 'Invalid admin credentials.';
        }
    }
}

$siteName = get_setting('site_name', 'TOPBD');
?>
<!DOCTYPE html>
<html lang="en" class="dark">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin Login — <?= e($siteName) ?></title>
    <script src="https://cdn.tailwindcss.com"></script>
    <style>
        body {
            background-color: #0A0D0F;
            color: #F0F4F8;
            font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
        }
    </style>
</head>
<body class="min-h-screen flex items-center justify-center p-4">
    <div class="w-full max-w-md bg-[#13181D] border border-[#222A33] rounded-2xl p-6 sm:p-8 shadow-2xl space-y-6">
        <div class="text-center space-y-2">
            <div class="inline-flex w-12 h-12 rounded-xl bg-gradient-to-tr from-emerald-600 to-emerald-400 items-center justify-center shadow-lg shadow-emerald-950/50 mb-2">
                <svg width="24" height="24" viewBox="0 0 24 24" fill="none" class="text-white">
                    <path d="M4 18L9 13H13L18 6M18 6H13M18 6V11" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"/>
                    <circle cx="18" cy="6" r="1.5" fill="#F42A41"/>
                </svg>
            </div>
            <h1 class="text-xl sm:text-2xl font-bold font-mono tracking-tight text-white"><?= e($siteName) ?> Control Center</h1>
            <p class="text-xs text-slate-400">Enter your administrative credentials to manage your platform</p>
        </div>

        <?php if (!empty($error)): ?>
            <div class="p-3.5 rounded-xl bg-rose-950/60 border border-rose-800/60 text-xs text-rose-300">
                <?= e($error) ?>
            </div>
        <?php endif; ?>

        <form method="POST" class="space-y-4">
            <input type="hidden" name="csrf_token" value="<?= e(csrf_token()) ?>" />

            <div class="space-y-1.5">
                <label class="block text-xs font-semibold text-slate-300">Username or Email</label>
                <input type="text" name="username" required autofocus placeholder="admin" class="w-full px-3.5 py-2.5 bg-[#0A0D0F] border border-[#222A33] rounded-xl text-white text-sm focus:outline-none focus:border-emerald-500 transition-colors" />
            </div>

            <div class="space-y-1.5">
                <label class="block text-xs font-semibold text-slate-300">Password</label>
                <input type="password" name="password" required placeholder="••••••••" class="w-full px-3.5 py-2.5 bg-[#0A0D0F] border border-[#222A33] rounded-xl text-white text-sm focus:outline-none focus:border-emerald-500 transition-colors" />
            </div>

            <button type="submit" class="w-full py-3 rounded-xl bg-emerald-600 hover:bg-emerald-500 text-white font-bold text-sm shadow-lg shadow-emerald-950/60 transition-all cursor-pointer">
                Sign In to Dashboard
            </button>
        </form>

        <div class="pt-2 text-center text-xs text-slate-500 border-t border-[#222A33]">
            <a href="/" class="hover:text-slate-300 transition-colors">← Back to Public Board</a>
        </div>
    </div>
</body>
</html>
