<?php
session_start();
include 'config.php';

$msg = "";

if(isset($_POST['login'])){
    $username = $_POST['username'];
    $password = $_POST['password'];

    // Fetch admin user
    $stmt = $conn->prepare("SELECT id, username, password FROM users WHERE username=? AND role='admin'");
    $stmt->bind_param("s", $username);
    $stmt->execute();
    $result = $stmt->get_result();

    if($result->num_rows === 1){
        $row = $result->fetch_assoc();
        if($password === $row['password']){  // plain text comparison
            $_SESSION['admin_id'] = $row['id'];
            $_SESSION['admin_name'] = $row['username'];
            header("Location: admin_dashboard.php"); // Redirect to dashboard
            exit;
        } else {
            $msg = "Invalid username or password!";
        }
    } else {
        $msg = "Invalid username or password!";
    }

    $stmt->close();
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Admin Login</title>
    <style>
        body { font-family: Arial; background: #ffe6f0; display:flex; justify-content:center; align-items:center; height:100vh; }
        .login-container { background:white; padding:30px; border-radius:10px; box-shadow:0 5px 20px rgba(0,0,0,0.2); width:300px; text-align:center; }
        input { width:100%; padding:10px; margin:10px 0; border-radius:5px; border:1px solid #ccc; }
        button { width:100%; padding:10px; border:none; border-radius:5px; background:#d21dd8ff; color:white; font-weight:bold; cursor:pointer; }
        button:hover { background:#a815b8; }
        .msg { color:red; margin-top:10px; }
    </style>
</head>
<body>
    <div class="login-container">
        <h2>Admin Login</h2>
        <form method="POST">
            <input type="text" name="username" placeholder="Username" required>
            <input type="password" name="password" placeholder="Password" required>
            <button type="submit" name="login">Login</button>
        </form>
        <?php if($msg != ""): ?>
            <div class="msg"><?= $msg ?></div>
        <?php endif; ?>
    </div>
</body>
</html>
