<?php
require_once "../base.php";
session_start();
if(!isset($_SESSION["userId"]))header("Location: ../login.php");
if(!isAdmin($_SESSION["userId"])){
    http_response_code(403);
    print("Forbidden, you don't have access to this resource.");
    exit;
}
head("Users");
nav();
?>
<div>
<h1>Users</h1>
<a href="add.php" class="btn btn-success">Add</a>
<?php
require "../db.php";
$stmt=$pdo->prepare("SELECT u.id, u.username, u.displayName, u.isAdmin, p.name, p.surname FROM users AS u LEFT JOIN people AS p ON u.person=p.id");
$stmt->execute([]);
?>
<table class="table table-striped table-sm">
    <thead>
        <tr>
            <td>id</td>
            <td>username</td>
            <td>display name</td>
            <td>person</td>
            <td>admin</td>
            <td>actions</td>
        </tr>
    </thead>
    <tbody>
        <?php while($row=$stmt->fetch()): ?>
        <tr>
            <td><?= $row['id'] ?></td>
            <td><?= $row['username'] ?></td>
            <td><?= $row['displayName'] ?></td>
            <td><?= $row['name'].' '.$row['surname'] ?></td>
            <td><?= $row['isAdmin'] ? 'Yes' : 'No' ?></td>
            <td>
                <a href="modify.php?id=<?= $row['id'] ?>" class="btn btn-primary">Modify</a>
                <a href="delete.php?id=<?= $row['id'] ?>" class="btn btn-danger">Delete</a>
            </td>
        </tr>
        <?php endwhile; ?>
    </tbody>
</table>
</div>
<?php
footer();
?>