<?php

function h($v)
{
    return htmlspecialchars(
        (string)$v,
        ENT_QUOTES,
        'UTF-8'
    );
}

$logs = $logs ?? [];

?>

<div class="container-fluid py-4">

    <div class="card border-0 shadow-sm mb-4"
         style="
            border-radius:24px;
            background:linear-gradient(135deg,#0f172a,#1d4ed8);
            color:#fff;
         ">

        <div class="card-body p-4">

            <h3 class="fw-bold mb-1">
                <i class="fa fa-shield-halved"></i>
                Security Monitor
            </h3>

            <div class="opacity-75">
                Permission Audit Trail
            </div>

        </div>

    </div>

    <div class="card border-0 shadow-sm"
         style="border-radius:22px;">

        <div class="card-body">

            <h5 class="fw-bold mb-3">
                Recent Permission Activity
            </h5>

            <div class="table-responsive">

                <table class="table table-bordered align-middle">

                    <thead class="table-dark">

                        <tr>

                            <th>ID</th>

                            <th>User</th>

                            <th>Role</th>

                            <th>Permission</th>

                            <th>Action</th>

                            <th>Result</th>

                            <th>IP</th>

                            <th>Date</th>

                        </tr>

                    </thead>

                    <tbody>

                        <?php if (!$logs): ?>

                            <tr>
                                <td colspan="8"
                                    class="text-center text-muted">
                                    No records found
                                </td>
                            </tr>

                        <?php endif; ?>

                        <?php foreach ($logs as $row): ?>

                            <tr>

                                <td>
                                    <?= h($row['id']) ?>
                                </td>

                                <td>
                                    <?= h($row['user_id']) ?>
                                </td>

                                <td>
                                    <?= h($row['role_code']) ?>
                                </td>

                                <td>
                                    <?= h($row['permission_key']) ?>
                                </td>

                                <td>
                                    <?= h($row['action']) ?>
                                </td>

                                <td>

                                    <?php if (
                                        $row['result'] === 'allowed'
                                    ): ?>

                                        <span class="badge bg-success">
                                            Allowed
                                        </span>

                                    <?php else: ?>

                                        <span class="badge bg-danger">
                                            Denied
                                        </span>

                                    <?php endif; ?>

                                </td>

                                <td>
                                    <?= h($row['ip_address']) ?>
                                </td>

                                <td>
                                    <?= h($row['created_at']) ?>
                                </td>

                            </tr>

                        <?php endforeach; ?>

                    </tbody>

                </table>

            </div>

        </div>

    </div>

</div>
