<?php
// admin/login.php
require_once '../config/config.php';

// If already logged in, redirect to dashboard
if (is_logged_in()) {
    redirect('admin/dashboard.php');
}

$error = '';

// Handle login form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = $_POST['username'] ?? '';
    $password = $_POST['password'] ?? '';
    
    if (!empty($username) && !empty($password)) {
        // Get admin user from database
        $stmt = $pdo->prepare("SELECT * FROM admin_users WHERE username = ?");
        $stmt->execute([$username]);
        $admin = $stmt->fetch();
        
        if ($admin) {
            // Check password (supports both hashed and plain text)
            $password_match = false;
            
            if (substr($admin->password, 0, 4) === '$2y$') {
                // Hashed password
                $password_match = password_verify($password, $admin->password);
            } else {
                // Plain text password
                $password_match = ($password === $admin->password);
            }
            
            if ($password_match) {
                // Set session
                $_SESSION['admin_id'] = $admin->id;
                $_SESSION['admin_username'] = $admin->username;
                $_SESSION['admin_name'] = $admin->full_name;
                $_SESSION['admin_logged_in'] = true;
                
                set_flash('success', 'Login successful!');
                redirect('admin/dashboard.php');
            } else {
                $error = 'Invalid password';
            }
        } else {
            $error = 'User not found';
        }
    } else {
        $error = 'Please enter both username and password';
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin Login - Edoxi Training Center</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">
</head>
<body class="bg-gradient-to-br from-blue-500 to-purple-600 min-h-screen flex items-center justify-center p-4">
    <div class="w-full max-w-md">
        <div class="bg-white rounded-2xl shadow-2xl overflow-hidden">
            <!-- Header with Logo -->
            <div class="bg-white p-6 text-center border-b-4 border-blue-600">
                <img src="https://www.edoxi.com/assets/images/edoxi-logo.svg" 
                     alt="Edoxi Training Center" 
                     class="h-16 mx-auto mb-4">
                <h1 class="text-2xl font-bold text-gray-800">Admin Panel</h1>
                <p class="text-gray-600 text-sm mt-1">Assessment Management System</p>
            </div>
            
            <!-- Login Form -->
            <div class="p-8">
                <div class="text-center mb-6">
                    <div class="inline-flex items-center justify-center w-16 h-16 bg-blue-100 rounded-full mb-3">
                        <i class="fas fa-shield-halved text-blue-600 text-2xl"></i>
                    </div>
                    <h2 class="text-xl font-semibold text-gray-800">Secure Login</h2>
                    <p class="text-gray-600 text-sm mt-1">Enter your credentials to access the dashboard</p>
                </div>

                <?php if ($error): ?>
                    <div class="mb-6 bg-red-50 border-l-4 border-red-500 p-4 rounded-lg">
                        <div class="flex items-center">
                            <i class="fas fa-exclamation-circle text-red-500 mr-3"></i>
                            <p class="text-red-700 text-sm font-medium"><?php echo escape($error); ?></p>
                        </div>
                    </div>
                <?php endif; ?>
                
                <form method="POST" action="">
                    <div class="mb-5">
                        <label class="block text-gray-700 text-sm font-semibold mb-2">
                            <i class="fas fa-user mr-2 text-gray-500"></i>Username
                        </label>
                        <input type="text" name="username" required 
                               placeholder="Enter your username"
                               class="w-full px-4 py-3 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent transition">
                    </div>
                    
                    <div class="mb-6">
                        <label class="block text-gray-700 text-sm font-semibold mb-2">
                            <i class="fas fa-lock mr-2 text-gray-500"></i>Password
                        </label>
                        <input type="password" name="password" required 
                               placeholder="Enter your password"
                               class="w-full px-4 py-3 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent transition">
                    </div>
                    
                    <button type="submit" 
                            class="w-full bg-gradient-to-r from-blue-600 to-purple-600 text-white font-bold py-3 rounded-lg hover:from-blue-700 hover:to-purple-700 transition duration-200 shadow-lg hover:shadow-xl flex items-center justify-center">
                        <i class="fas fa-sign-in-alt mr-2"></i>
                        Login to Dashboard
                    </button>
                </form>
                
                <div class="mt-6 text-center">
                    <!--<a href="<?php echo base_url('admin/create_admin.php'); ?>" -->
                    <!--   class="text-sm text-blue-600 hover:text-blue-800 hover:underline font-medium">-->
                    <!--    <i class="fas fa-user-plus mr-1"></i>Create Admin User-->
                    <!--</a>-->
                </div>
            </div>
        </div>
        
        <!-- Footer -->
        <div class="text-center mt-6">
            <p class="text-white text-sm">
                © <?php echo date('Y'); ?> Edoxi Training Center. All rights reserved.
            </p>
            <p class="text-white text-xs mt-1 opacity-80">
               Delivering Training Excellence, Globally!
            </p>
        </div>
    </div>
</body>
</html>