<?php
session_start();
require '../config/db.php';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $email = $_POST['email'];
    $password = $_POST['password'];

    $stmt = $pdo->prepare("SELECT * FROM users WHERE email = ?");
    $stmt->execute([$email]);
    $user = $stmt->fetch();

    // Verify Password AND Role
    if ($user && password_verify($password, $user['password_hash'])) {
        if ($user['role'] === 'admin') {
            $_SESSION['user_id'] = $user['id'];
            $_SESSION['role'] = 'admin';
            header("Location: index.php");
            exit();
        } else {
            $error = "You are not an Admin.";
        }
    } else {
        $error = "Invalid Credentials.";
    }
}
?>
<div style="display:flex; justify-content:center; align-items:center; height:100vh; background:#ecf0f1;">
    <form method="POST" style="background:white; padding:40px; border-radius:8px; box-shadow:0 4px 10px rgba(0,0,0,0.1); width:300px;">
        <h2 style="text-align:center; color:#2c3e50;">Admin Login</h2>
        <?php if(isset($error)) echo "<p style='color:red; text-align:center;'>$error</p>"; ?>
        <input type="email" name="email" placeholder="Admin Email" required style="width:100%; padding:10px; margin-bottom:10px; box-sizing:border-box;">
        <input type="password" name="password" placeholder="Password" required style="width:100%; padding:10px; margin-bottom:20px; box-sizing:border-box;">
        <button type="submit" style="width:100%; padding:10px; background:#2c3e50; color:white; border:none; border-radius:4px; cursor:pointer;">Login</button>
    </form>
</div>

<?php include 'includes/footer.php'; ?>