<?php include "action.php" ?>
<?php
session_start();
require_once 'php/adminconfig.php'; // Include the database connection file

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Retrieve username and password from the form
    $username = $_POST['username'];
    $password = $_POST['password'];
    
    try {
        // Modified query to also select the role column
        $stmt = $conn->prepare("SELECT id, username, password, role FROM admins WHERE username = ?");
        $stmt->execute([$username]); // Execute the prepared statement with the username
        $admin = $stmt->fetch(PDO::FETCH_ASSOC);
        
        // Check if the username exists and the password matches
        if ($admin && $password === $admin['password']) {
            // Set session variables for successful login
            $_SESSION['admin_id'] = $admin['id'];
            $_SESSION['admin_username'] = $admin['username'];
            $_SESSION['user_role'] = $admin['role']; // Add the role to session
            
            // Redirect to the admin dashboard
            header("Location: admin-overview.php");
            exit();
        } else {
            // Show error message if login fails
            $error = "Invalid username or password.";
        }
    } catch (PDOException $e) {
        $error = "A login error occurred. Please try again.";
        error_log("Login error: " . $e->getMessage());
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9, user-scalable=no" />
    <meta name="robots" content="noindex, nofollow">
<link rel="shortcut icon" type="image/png" href="images/icon.jpg" />
    <title>Admin Login</title>
    <link href="https://cdn.jsdelivr.net/npm/remixicon@3.2.0/fonts/remixicon.css" rel="stylesheet">
    <style>
        /* Your existing styles here */
        <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', sans-serif;
            background:  #0a192f;
            height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .login-container {
            background:#1a2742;
            padding: 2rem;
            border-radius: 10px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            width: 100%;
            max-width: 400px;
        }

        .login-header {
            text-align: center;
            margin-bottom: 2rem;
        }

        .login-header i {
            font-size: 6rem;
            color: #1a73e8;
            padding: 20px;
            border-radius: 15px;
            background: #8892b0;
            margin-bottom: 1rem;
        }

        .login-header h1 {
            color: #1a73e8;
            font-size: 1.5rem;
        }

        .form-group {
            margin-bottom: 1rem;
        }

        .form-group label {
            display: block;
            margin-bottom: 0.5rem;
            color: #666;
        }

        .form-group input {
            width: 92.5%;
            padding: 0.75rem;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 1rem;
            background: #1a2742;
        }

        .login-button {
            width: 100%;
            padding: 0.75rem;
            background: navy;
            color: white;
            border: none;
            border-radius: 5px;
            font-size: 1rem;
            cursor: pointer;
            transition: background 0.3s;
        }

        .login-button:hover {
            background: #1557b0;
        }

        .error-message {
            color: #dc3545;
            text-align: center;
            margin-bottom: 1rem;
        }
    </style>
    </style>
</head>
<body>
    <div class="login-container">
        <div class="login-header">
            <i class="ri-admin-line"></i>
            <h1>Admin Login</h1>
        </div>

        <!-- Display error message -->
        <?php if (isset($error)): ?>
            <div class="error-message"><?php echo htmlspecialchars($error); ?></div>
        <?php endif; ?>

        <!-- Login form -->
        <form method="POST" action="">
            <div class="form-group">
                <label for="username">Username</label>
                <input type="text" id="username" name="username" required>
            </div>
            <div class="form-group">
                <label for="password">Password</label>
                <input type="password" id="password" name="password" required>
            </div>
            <button type="submit" class="login-button">Login</button>
        </form>
    </div>
</body>
</html>
