<?php
function aa_h($v) {
    return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8');
}

$rows = $rows ?? [];
$conference = $conference ?? [];
$success = $_SESSION['flash_success'] ?? '';
$output = $_SESSION['flash_output'] ?? '';
unset($_SESSION['flash_success'], $_SESSION['flash_output']);

$byRole = [];
foreach ($rows as $r) {
    $code = $r['role']['code'] ?? 'unknown';
    $byRole[$code][] = $r;
}
?>

<div class="container-fluid py-3">
    <div class="card border-0 shadow-sm mb-4" style="border-radius:24px;background:linear-gradient(135deg,#0f172a,#2563eb);color:#fff;">
        <div class="card-body p-4 d-flex justify-content-between align-items-center flex-wrap gap-3">
            <div>
                <h3 class="fw-bold mb-1">
                    <i class="fa fa-shield-halved"></i>
                    Access Audit
                </h3>
                <div class="opacity-75">
                    Conference: <?= aa_h($conference['short_name'] ?? '') ?> #<?= (int)($conference['id'] ?? 0) ?>
                </div>
            </div>

            <form method="post" action="/easychair/admin/access-audit/heal">
                <button class="btn btn-light rounded-pill px-4" onclick="return confirm('Heal permissions for current conference?')">
                    <i class="fa fa-wand-magic-sparkles"></i>
                    Heal Current Conference
                </button>
            </form>
        </div>
    </div>

    <?php if ($success): ?>
        <div class="alert alert-success"><?= aa_h($success) ?></div>
    <?php endif; ?>

    <?php if ($output): ?>
        <details class="mb-4">
            <summary class="fw-bold">Healer Output</summary>
            <pre class="bg-dark text-white p-3 rounded mt-2" style="max-height:360px;overflow:auto;"><?= aa_h($output) ?></pre>
        </details>
    <?php endif; ?>

    <?php foreach ($byRole as $roleCode => $items): ?>
        <div class="card border-0 shadow-sm mb-4" style="border-radius:22px;">
            <div class="card-body p-4">
                <h5 class="fw-bold mb-3">
                    <i class="fa fa-user-shield text-primary"></i>
                    <?= aa_h($roleCode) ?>
                </h5>

                <div class="table-responsive">
                    <table class="table table-bordered align-middle">
                        <thead class="table-dark">
                        <tr>
                            <th>Menu</th>
                            <th>URL</th>
                            <th>Allowed</th>
                            <th>View</th>
                            <th>Create</th>
                            <th>Update</th>
                            <th>Delete</th>
                        </tr>
                        </thead>
                        <tbody>
                        <?php foreach ($items as $r): ?>
                            <?php $p = $r['permission'] ?? []; ?>
                            <tr>
                                <td>
                                    <strong><?= aa_h($r['menu']['label'] ?? '') ?></strong>
                                    <div class="text-muted small"><?= aa_h($r['menu']['module_key'] ?? '') ?></div>
                                </td>
                                <td><code><?= aa_h($r['menu']['url'] ?? '') ?></code></td>
                                <td><?= !empty($p['is_allowed']) ? '<span class="badge bg-success">YES</span>' : '<span class="badge bg-secondary">NO</span>' ?></td>
                                <td><?= !empty($p['can_view']) ? '<span class="badge bg-success">YES</span>' : '<span class="badge bg-secondary">NO</span>' ?></td>
                                <td><?= !empty($p['can_create']) ? '<span class="badge bg-success">YES</span>' : '<span class="badge bg-secondary">NO</span>' ?></td>
                                <td><?= !empty($p['can_update']) ? '<span class="badge bg-success">YES</span>' : '<span class="badge bg-secondary">NO</span>' ?></td>
                                <td><?= !empty($p['can_delete']) ? '<span class="badge bg-danger">YES</span>' : '<span class="badge bg-secondary">NO</span>' ?></td>
                            </tr>
                        <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    <?php endforeach; ?>
</div>
