<?php
ob_start();
require_once '../includes/admin_auth.php';

$adminAuth = new AdminAuth();

if ($adminAuth->isLoggedIn()) {
    header("Location: index.php");
    exit();
}

$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = $_POST['username'] ?? '';
    $password = $_POST['password'] ?? '';
    
    $result = $adminAuth->login(['username' => $username, 'password' => $password]);
    
    if ($result['success']) {
        header("Location: index.php");
        exit();
    } else {
        $error = $result['message'];
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin Login - <?php echo SITE_NAME; ?></title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        body {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
    </style>
</head>
<body class="min-h-screen flex items-center justify-center">
    <div class="max-w-md w-full mx-4">
        <div class="bg-white rounded-2xl shadow-2xl overflow-hidden">
            <!-- Header -->
            <div class="bg-gradient-to-r from-gray-900 to-gray-800 text-white p-8 text-center">
                <div class="flex items-center justify-center space-x-3 mb-4">
                    <i class="fas fa-shield-alt text-3xl text-purple-400"></i>
                    <div>
                        <h1 class="text-2xl font-bold"><?php echo SITE_NAME; ?></h1>
                        <p class="text-gray-300 text-sm">Administration Panel</p>
                    </div>
                </div>
                <p class="text-gray-300">Restricted Access - Authorized Personnel Only</p>
            </div>
            
            <!-- Login Form -->
            <div class="p-8">
                <?php if ($error): ?>
                <div class="bg-red-50 border border-red-200 text-red-700 px-4 py-3 rounded-lg mb-6">
                    <div class="flex items-center">
                        <i class="fas fa-exclamation-circle mr-3"></i>
                        <span><?php echo htmlspecialchars($error); ?></span>
                    </div>
                </div>
                <?php endif; ?>
                
                <form method="POST" action="">
                    <div class="space-y-6">
                        <div>
                            <label class="block text-gray-700 font-medium mb-2">
                                <i class="fas fa-user mr-2"></i>Username
                            </label>
                            <input type="text" name="username" required
                                   class="w-full px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-purple-500 focus:border-transparent"
                                   placeholder="Enter admin username">
                        </div>
                        
                        <div>
                            <label class="block text-gray-700 font-medium mb-2">
                                <i class="fas fa-lock mr-2"></i>Password
                            </label>
                            <div class="relative">
                                <input type="password" name="password" required
                                       class="w-full px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-purple-500 focus:border-transparent"
                                       placeholder="Enter password">
                                <button type="button" onclick="togglePassword(this)"
                                        class="absolute right-3 top-3 text-gray-500 hover:text-gray-700">
                                    <i class="fas fa-eye"></i>
                                </button>
                            </div>
                        </div>
                        
                        <div class="flex items-center justify-between">
                            <div class="flex items-center">
                                <input type="checkbox" id="remember" name="remember"
                                       class="h-4 w-4 text-purple-600 focus:ring-purple-500 border-gray-300 rounded">
                                <label for="remember" class="ml-2 text-sm text-gray-700">
                                    Remember me
                                </label>
                            </div>
                            <a href="#" class="text-sm text-purple-600 hover:text-purple-800">
                                Forgot password?
                            </a>
                        </div>
                        
                        <button type="submit"
                                class="w-full bg-gradient-to-r from-purple-600 to-blue-600 text-white font-bold py-3 px-4 rounded-lg hover:opacity-90 transition duration-300">
                            <i class="fas fa-sign-in-alt mr-2"></i>Login to Admin Panel
                        </button>
                    </div>
                </form>
                
                <div class="mt-8 pt-6 border-t border-gray-200">
                    <div class="text-center">
                        <a href="../index.php" class="text-gray-600 hover:text-gray-800">
                            <i class="fas fa-arrow-left mr-2"></i>Back to Main Website
                        </a>
                    </div>
                </div>
            </div>
            
            <!-- Security Notice -->
            <div class="bg-gray-50 border-t border-gray-200 p-4">
                <div class="flex items-center text-sm text-gray-600">
                    <i class="fas fa-info-circle mr-2 text-blue-500"></i>
                    <span>For security reasons, please log out after each session.</span>
                </div>
            </div>
        </div>
        
        <!-- Login Logs -->
        <div class="mt-6 bg-white rounded-lg shadow p-4">
            <h4 class="font-bold text-gray-800 mb-2">Recent Login Attempts</h4>
            <?php
            $db = Database::getInstance()->getConnection();
            $stmt = $db->prepare("SELECT ip_address, attempt_time, success FROM login_attempts 
                                 WHERE username LIKE '%admin%' ORDER BY attempt_time DESC LIMIT 3");
            $stmt->execute();
            $attempts = $stmt->fetchAll();
            ?>
            <div class="space-y-2">
                <?php foreach ($attempts as $attempt): ?>
                <div class="flex items-center justify-between text-sm">
                    <span class="text-gray-600"><?php echo $attempt['ip_address']; ?></span>
                    <span class="<?php echo $attempt['success'] ? 'text-green-600' : 'text-red-600'; ?>">
                        <?php echo $attempt['success'] ? 'Success' : 'Failed'; ?>
                    </span>
                    <span class="text-gray-500"><?php echo date('H:i', strtotime($attempt['attempt_time'])); ?></span>
                </div>
                <?php endforeach; ?>
            </div>
        </div>
    </div>
    
    <script>
        function togglePassword(button) {
            const input = button.parentElement.querySelector('input');
            const icon = button.querySelector('i');
            
            if (input.type === 'password') {
                input.type = 'text';
                icon.classList.remove('fa-eye');
                icon.classList.add('fa-eye-slash');
            } else {
                input.type = 'password';
                icon.classList.remove('fa-eye-slash');
                icon.classList.add('fa-eye');
            }
        }
        
        // Auto-hide error after 5 seconds
        setTimeout(() => {
            const errorDiv = document.querySelector('.bg-red-50');
            if (errorDiv) {
                errorDiv.style.display = 'none';
            }
        }, 5000);
    </script>
</body>
</html>