<?php
session_start();
include('includes/db_pdo.php'); // Include your PDO database connection

// Destroy session if expired (30 minutes inactivity)
if (isset($_SESSION['stamp']) && (time() - $_SESSION['stamp'] > 1800)) {
    session_unset();
    session_destroy();
}

// Redirect if already logged in
if (isset($_SESSION["alogin"])) {
    header("Location: dashboard.php");
    exit();
}

$error_message = "";

// Check if form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $username = trim($_POST["username"] ?? "");
    $password = $_POST["password"] ?? "";

    if (empty($username) || empty($password)) {
        $error_message = "⚠ Please enter both username and password.";
    } else {
        try {
            // Check user in the database
            $query = "SELECT * FROM admin WHERE UserName = :username LIMIT 1";
            $stmt = $conn->prepare($query);
            $stmt->bindParam(':username', $username);
            $stmt->execute();
            $user = $stmt->fetch(PDO::FETCH_ASSOC);

            // Verify credentials
            if ($user && password_verify($password, $user['Password'])) {
                $_SESSION["alogin"] = $username;
                $_SESSION['stamp'] = time(); // Timestamp for session validation

                header("Location: dashboard.php");
                exit();
            } else {
                $error_message = "❌ Invalid username or password.";
            }
        } catch (PDOException $e) {
            $error_message = "Database error: " . $e->getMessage();
        }
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login - Result Management System</title>
    
    <!-- Bootstrap & FontAwesome -->
    <link rel="stylesheet" href="assets/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="assets/fontawesome-6/css/all.min.css">
    <link rel="stylesheet" href="css/loginstyle.css">
    
    <!-- FontAwesome Kit (Fix for missing icons) -->
    <!-- <script src="https://kit.fontawesome.com/a076d05399.js" crossorigin="anonymous"></script> -->

    <style>
    /* Google Font */
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600&display=swap');

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: 'Poppins', sans-serif;
}

/* Multi-Gradient Background */
body {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    background: linear-gradient(135deg, #007bff, #00bcd4, #4caf50);
}

/* Login Box */
.login-container {
    width: 100%;
    max-width: 400px;
    background: white;
    padding: 25px;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
    border-radius: 10px;
    text-align: center;
}

.login-box h2 {
    margin-bottom: 10px;
    font-weight: 600;
    color: #007bff;
}

.welcome-text {
    font-size: 14px;
    color: #666;
    margin-bottom: 20px;
}

/* Bootstrap Input Fields */
.input-group .input-group-text {
    background: #f1f1f1;
    border-right: none;
    color: #333;
}

.input-group input {
    border-left: none;
}

/* Error Message */
.alert-danger {
    padding: 8px;
    font-size: 14px;
}

/* Login Button */
.btn-primary {
    background: #007bff;
    border: none;
    font-size: 16px;
    transition: 0.3s;
}

.btn-primary:hover {
    background: #0056b3;
}

/* Forgot Password & Sign Up Links */
.text-center a {
    text-decoration: none;
    color: #007bff;
    font-size: 14px;
}

.text-center a:hover {
    text-decoration: underline;
}
</style>
</head>
<body>

    <div class="login-container">
        <div class="login-box">
            <h2><i class="fa fa-user-circle"></i> Login</h2>
            <p class="welcome-text">Welcome to Result Management System</p>

            <?php if (!empty($error_message)) : ?>
                <div class="alert alert-danger"><?php echo htmlspecialchars($error_message); ?></div>
            <?php endif; ?>

            <form action="adminlogin.php" method="POST">
                <div class="mb-3 input-group">
                    <span class="input-group-text"><i class="fa fa-user"></i></span>
                    <input type="text" class="form-control" name="username" placeholder="Username" required>
                </div>
                
                <div class="mb-3 input-group">
                    <span class="input-group-text"><i class="fa fa-lock"></i></span>
                    <input type="password" class="form-control" name="password" placeholder="Password" required>
                </div>

                <button type="submit" class="btn btn-primary w-100"><i class="fa fa-sign-in-alt"></i> Login</button>
            </form>

            <div class="text-center mt-3">
                <a href="forgot-password.php">Forgot Password?</a> | <a href="register.php">Sign Up</a>
            </div>
        </div>
    </div>

    <!-- Bootstrap JS -->
    <script src="assets/bootstrap/js/bootstrap.bundle.min.js"></script>
</body>
</html>
