<?php
require_once 'config.php';

$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = trim($_POST['username'] ?? '');
    $password = trim($_POST['password'] ?? '');

    if ($username !== '' && $password !== '') {
        $stmt = $mysqli->prepare('
            SELECT id, username, password_hash,
                   role,
                   can_projects, can_items, can_orders,
                   can_suppliers, can_receipts, can_stock
            FROM users
            WHERE username = ?
        ');
        $stmt->bind_param('s', $username);
        $stmt->execute();
        $stmt->bind_result(
            $id, $uName, $hash,
            $role,
            $can_projects, $can_items, $can_orders,
            $can_suppliers, $can_receipts, $can_stock
        );
        if ($stmt->fetch()) {
            if (password_verify($password, $hash)) {
                $_SESSION['user_id']  = $id;
                $_SESSION['username'] = $uName;
                $_SESSION['user'] = [
                    'id'           => $id,
                    'username'     => $uName,
                    'role'         => $role,
                    'can_projects' => (int)$can_projects,
                    'can_items'    => (int)$can_items,
                    'can_orders'   => (int)$can_orders,
                    'can_suppliers'=> (int)$can_suppliers,
                    'can_receipts' => (int)$can_receipts,
                    'can_stock'    => (int)$can_stock,
                ];
                header('Location: index.php');
                exit;
            } else {
                $error = 'Neispravna lozinka.';
            }
        } else {
            $error = 'Korisnik ne postoji.';
        }
        $stmt->close();
    } else {
        $error = 'Unesite korisničko ime i lozinku.';
    }
}
?>
<!doctype html>
<html lang="hr">
<head>
    <meta charset="utf-8">
    <title>Prijava - Evidencija materijala</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="assets/css/style.css" rel="stylesheet">
</head>
<body class="bg-light">
<div class="container d-flex align-items-center justify-content-center" style="min-height:100vh;">
    <div class="card shadow-sm" style="max-width:400px;width:100%;">
        <div class="card-body">
            <h4 class="mb-3 text-center">Evidencija materijala</h4>
            <?php if ($error): ?>
                <div class="alert alert-danger py-2 small"><?php echo htmlspecialchars($error); ?></div>
            <?php endif; ?>
            <form method="post">
                <div class="mb-3">
                    <label class="form-label">Korisničko ime</label>
                    <input type="text" name="username" class="form-control" required autofocus>
                </div>
                <div class="mb-3">
                    <label class="form-label">Lozinka</label>
                    <input type="password" name="password" class="form-control" required>
                </div>
                <button class="btn btn-primary w-100 mb-2" type="submit">Prijava</button>
            </form>            
        </div>
    </div>
</div>
</body>
</html>
