<?php
session_start();
include("db.php"); // Database connection file

$error = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $username = $conn->real_escape_string($_POST['username']);
    $password = $conn->real_escape_string($_POST['password']);

    $sql = "SELECT * FROM users WHERE username='$username' AND password=MD5('$password')";
$result = $conn->query($sql);

if ($result->num_rows == 1) {
    $user = $result->fetch_assoc();
    $_SESSION['username'] = $user['username'];
    $_SESSION['role'] = $user['role'];  // 👈 very important
    header("Location: dashboard.php");
    exit();
} else {
    $error = "Invalid Username or Password!";
}

}
?>


<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login</title>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css" rel="stylesheet">
    <link rel="stylesheet" href="css/style.css">

</head>

<body>

    <div class="login-page">
        <div class="login-box"> 
    <h2>Login</h2>

    <?php if($error != ""): ?>
      <p style="color: red; text-align:center;"><?php echo $error; ?></p>
    <?php endif; ?>

    <form method="POST" action="">
        <div class="input-group">
            <input type="text" name="username" placeholder="Username" required>
            <i class="fa fa-user"></i>
        </div>
        <div class="input-group">
            <input type="password" name="password" placeholder="Password" required>
            <i class="fa fa-lock"></i>
        </div>
        <div class="options">
            <label><input type="checkbox" name="remember"> Remember me</label>
        </div>
        <button type="submit" class="login-btn">Login</button>
    </form>
</div>
    </div>
</body>

</html>