<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>
<?php
session_start();
include("config/database.php");

if(isset($_SESSION['user_id'])){
    header("Location: dashboard.php");
    exit;
}

$error="";

if(isset($_POST['login']))
{
    $email=mysqli_real_escape_string($conn,$_POST['email']);
    $password=MD5($_POST['password']);

    $query=mysqli_query($conn,"SELECT * FROM users WHERE email='$email' AND password='$password' AND status=1");

    if(mysqli_num_rows($query)>0)
    {
        $row=mysqli_fetch_assoc($query);

        $_SESSION['user_id']=$row['id'];
        $_SESSION['name']=$row['name'];
        $_SESSION['role']=$row['role'];

        header("Location: dashboard.php");
        exit;
    }
    else
    {
        $error="Invalid Email or Password";
    }
}
?>

<!DOCTYPE html>
<html>
<head>

<meta charset="UTF-8">

<title>Login</title>

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">

<style>

body{
background:#f4f6fb;
display:flex;
justify-content:center;
align-items:center;
height:100vh;
font-family:Segoe UI;
}

.card{
width:400px;
border-radius:20px;
padding:30px;
border:none;
box-shadow:0 10px 30px rgba(0,0,0,.08);
}

.btn-login{
background:#6f42c1;
color:#fff;
}

.btn-login:hover{
background:#5b33a6;
color:#fff;
}

</style>

</head>

<body>

<div class="card">

<h2 class="text-center mb-4">GlowPOS</h2>

<?php
if($error!="")
{
echo "<div class='alert alert-danger'>$error</div>";
}
?>

<form method="post">

<div class="mb-3">
<label>Email</label>
<input type="email" class="form-control" name="email" required>
</div>

<div class="mb-3">
<label>Password</label>
<input type="password" class="form-control" name="password" required>
</div>

<button class="btn btn-login w-100" name="login">
Login
</button>

</form>

</div>

</body>
</html>