<?php

declare(strict_types=1);

require_once '../../private/db.php';

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

$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    $username = trim($_POST['username'] ?? '');
    $password = trim($_POST['password'] ?? '');

    if ($username === '' || $password === '') {

        $error = 'Please enter username and password.';

    } else {

        $stmt = $pdo->prepare(
            "SELECT * FROM users WHERE username = ? LIMIT 1"
        );

        $stmt->execute([$username]);

        $user = $stmt->fetch();

       // if ($user && password_verify($password, $user['password'])) {
          if ($user && $password === $user['password']) {  

            session_regenerate_id(true);

            $_SESSION['user_id'] = $user['id'];
            $_SESSION['username'] = $user['username'];
            $_SESSION['role'] = $user['role'];

            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>EpiPos Login</title>

<link rel="stylesheet" href="assets/css/app.css">

<style>

body {
    margin: 0;
    background: #111827;
    font-family: Arial, sans-serif;
    color: #ffffff;
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
}

.login-container {
    width: 380px;
    background: #1f2937;
    border-radius: 14px;
    padding: 40px;
    box-shadow: 0 0 25px rgba(0,0,0,0.4);
}

.login-container h1 {
    text-align: center;
    margin-bottom: 30px;
    color: #ffffff;
}

.input-group {
    margin-bottom: 20px;
}

.input-group label {
    display: block;
    margin-bottom: 8px;
    font-size: 14px;
}

.input-group input {
    width: 100%;
    padding: 14px;
    border: none;
    border-radius: 8px;
    background: #374151;
    color: #ffffff;
    box-sizing: border-box;
    font-size: 15px;
}

.input-group input:focus {
    outline: none;
    border: 2px solid #2563eb;
}

.login-btn {
    width: 100%;
    background: #2563eb;
    color: #ffffff;
    border: none;
    padding: 14px;
    border-radius: 8px;
    font-size: 16px;
    cursor: pointer;
    transition: background 0.2s ease;
}

.login-btn:hover {
    background: #1d4ed8;
}

.error-box {
    background: #7f1d1d;
    color: #ffffff;
    padding: 12px;
    border-radius: 8px;
    margin-bottom: 20px;
    text-align: center;
}

.demo-box {
    margin-top: 25px;
    background: #0f172a;
    padding: 15px;
    border-radius: 8px;
    font-size: 14px;
    line-height: 1.8;
}

</style>

</head>
<body>

<div class="login-container">

<h1>EpiPos</h1>

<?php if ($error): ?>

<div class="error-box">
    <?= htmlspecialchars($error, ENT_QUOTES, 'UTF-8') ?>
</div>

<?php endif; ?>

<form method="POST" autocomplete="off">

    <div class="input-group">
        <label>Username</label>
        <input
            type="text"
            name="username"
            required
        >
    </div>

    <div class="input-group">
        <label>Password</label>
        <input
            type="password"
            name="password"
            required
        >
    </div>

    <button type="submit" class="login-btn">
        Login To EpiPos
    </button>

</form>

<!--
<div class="demo-box">


<strong>Default Login</strong><br>

Username: mic<br>
Password: mic7779311

</div>
-->

</div>

</body>
</html>