<?php
require_once 'includes/db.php';

// Simple check if already logged in
if (isset($_SESSION['admin_id'])) {
    header('Location: dashboard.php');
    exit;
}

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

    $stmt = $pdo->prepare("SELECT * FROM admin_users WHERE email = ?");
    $stmt->execute([$email]);
    $admin = $stmt->fetch();

    if ($admin && password_verify($password, $admin['password_hash'])) {
        $_SESSION['admin_id'] = $admin['id'];
        $_SESSION['admin_role'] = $admin['role'];
        // Also set a cookie/localStorage for API if needed, but Session is fine for now
        header('Location: dashboard.php');
        exit;
    } else {
        $error = 'Invalid credentials';
    }
}
?>
<!DOCTYPE html>
<html lang="en" class="dark">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login - AnonChat Admin</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <script>
        tailwind.config = {
            darkMode: 'class',
            theme: {
                extend: {
                    colors: {
                        primary: '#6366f1',
                        dark: '#0f172a',
                        darker: '#020617',
                        surface: '#1e293b',
                    }
                }
            }
        }
    </script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
</head>
<body class="bg-darker flex items-center justify-center h-screen relative overflow-hidden">

    <!-- Background Effects -->
    <div class="absolute top-0 left-0 w-full h-full overflow-hidden z-0">
        <div class="absolute -top-1/2 -left-1/4 w-[1000px] h-[1000px] rounded-full bg-primary/10 blur-3xl animate-pulse"></div>
        <div class="absolute -bottom-1/2 -right-1/4 w-[800px] h-[800px] rounded-full bg-purple-600/10 blur-3xl animate-pulse" style="animation-delay: 2s"></div>
    </div>

    <div class="relative z-10 w-full max-w-md p-8">
        
        <div class="text-center mb-8">
            <div class="inline-flex items-center justify-center w-16 h-16 rounded-full bg-surface border border-gray-700 shadow-xl mb-4 text-primary text-2xl">
                <i class="fas fa-shield-alt"></i>
            </div>
            <h1 class="text-3xl font-bold text-white tracking-tight">AnonChat <span class="text-primary">Admin</span></h1>
            <p class="text-gray-400 mt-2">Enterprise Moderation Portal</p>
        </div>

        <div class="bg-surface/50 backdrop-blur-xl border border-gray-700 rounded-2xl p-8 shadow-2xl">
            
            <?php if($error): ?>
                <div class="bg-red-500/10 border border-red-500/50 text-red-500 px-4 py-3 rounded-lg mb-6 flex items-center text-sm">
                    <i class="fas fa-exclamation-circle mr-2"></i> <?= htmlspecialchars($error) ?>
                </div>
            <?php endif; ?>

            <form method="post" class="space-y-6">
                <div>
                    <label class="block text-xs font-bold text-gray-400 uppercase mb-2">Email Address</label>
                    <div class="relative">
                        <span class="absolute inset-y-0 left-0 flex items-center pl-3 text-gray-500">
                            <i class="fas fa-envelope"></i>
                        </span>
                        <input type="email" name="email" class="w-full bg-dark/50 border border-gray-600 rounded-lg pl-10 pr-4 py-3 text-white placeholder-gray-500 focus:outline-none focus:border-primary focus:ring-1 focus:ring-primary transition-all" placeholder="admin@anonchat.com" required>
                    </div>
                </div>

                <div>
                    <label class="block text-xs font-bold text-gray-400 uppercase mb-2">Password</label>
                    <div class="relative">
                        <span class="absolute inset-y-0 left-0 flex items-center pl-3 text-gray-500">
                            <i class="fas fa-lock"></i>
                        </span>
                        <input type="password" name="password" class="w-full bg-dark/50 border border-gray-600 rounded-lg pl-10 pr-4 py-3 text-white placeholder-gray-500 focus:outline-none focus:border-primary focus:ring-1 focus:ring-primary transition-all" placeholder="••••••••" required>
                    </div>
                </div>

                <button type="submit" class="w-full bg-primary hover:bg-indigo-600 text-white font-bold py-3 rounded-lg shadow-lg shadow-primary/25 transition-all transform hover:scale-[1.02]">
                    Authenticate
                </button>
            </form>
        </div>

        <p class="text-center text-gray-600 text-xs mt-8">
            &copy; <?= date('Y') ?> AnonChat Inc. Secure System.
        </p>

    </div>

</body>
</html>
<!-- TEST -->
