<?php
function rb_h($v) {
    return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8');
}
$success = $_SESSION['flash_success'] ?? '';
unset($_SESSION['flash_success']);
?>

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

    <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-sliders"></i>
                SuperAdmin RBAC Matrix
            </h3>
            <div class="opacity-75">
                Customize sidebar visibility and CRUD/action permissions per role for this conference.
            </div>
            <div class="mt-2">
                <span class="badge bg-light text-dark"><?= rb_h($conference['short_name'] ?? '') ?></span>
                <span class="badge bg-warning text-dark"><?= rb_h($conference['org_name'] ?? ('Org #' . ($conference['org_id'] ?? ''))) ?></span>
            </div>
        </div>
    </div>

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

    <div class="alert alert-info">
        <strong>Important:</strong>
        Super Admin is always allowed. For other roles, unchecked means hidden from sidebar or denied for that action.
    </div>

    <div class="card border-0 shadow-sm mb-4" style="border-radius:22px;" id="menu">
        <div class="card-body p-4">
            <h4 class="fw-bold mb-3">
                <i class="fa fa-bars"></i>
                Sidebar Visibility
            </h4>

            <form method="post" action="/easychair/admin/rbac-matrix/menu/save">
                <div class="table-responsive">
                    <table class="table table-bordered align-middle">
                        <thead class="table-dark">
                        <tr>
                            <th style="min-width:280px;">Menu Item</th>
                            <?php foreach ($roles as $role): ?>
                                <th class="text-center"><?= rb_h($role['name']) ?><div class="small"><?= rb_h($role['code']) ?></div></th>
                            <?php endforeach; ?>
                        </tr>
                        </thead>
                        <tbody>
                        <?php foreach ($menuItems as $mi): ?>
                            <tr>
                                <td>
                                    <strong><i class="fa <?= rb_h($mi['icon'] ?? 'fa-circle') ?>"></i> <?= rb_h($mi['label_ar'] ?: $mi['label']) ?></strong>
                                    <div class="text-muted small"><?= rb_h($mi['url']) ?></div>
                                    <div class="small"><span class="badge bg-secondary"><?= rb_h($mi['module_key']) ?></span></div>
                                </td>
                                <?php foreach ($roles as $role): ?>
                                    <?php
                                    $checked = (int)($menuPerms[(int)$role['id']][(int)$mi['id']] ?? 1) === 1;
                                    $isSuper = ($role['code'] ?? '') === 'superadmin';
                                    ?>
                                    <td class="text-center">
                                        <input type="checkbox"
                                               name="menu[<?= (int)$role['id'] ?>][<?= (int)$mi['id'] ?>]"
                                               value="1"
                                               <?= $checked ? 'checked' : '' ?>
                                               <?= $isSuper ? 'disabled' : '' ?>>
                                        <?php if ($isSuper): ?>
                                            <input type="hidden" name="menu[<?= (int)$role['id'] ?>][<?= (int)$mi['id'] ?>]" value="1">
                                        <?php endif; ?>
                                    </td>
                                <?php endforeach; ?>
                            </tr>
                        <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>

                <button class="btn btn-success btn-lg rounded-pill px-5">
                    Save Sidebar Permissions
                </button>
            </form>
        </div>
    </div>

    <div class="card border-0 shadow-sm" style="border-radius:22px;" id="actions">
        <div class="card-body p-4">
            <h4 class="fw-bold mb-3">
                <i class="fa fa-shield-halved"></i>
                CRUD / Action Permissions
            </h4>

            <form method="post" action="/easychair/admin/rbac-matrix/actions/save">
                <?php foreach ($roles as $role): ?>
                    <div class="border rounded-4 p-3 mb-4">
                        <h5 class="fw-bold mb-3">
                            <?= rb_h($role['name']) ?>
                            <span class="badge bg-primary"><?= rb_h($role['code']) ?></span>
                        </h5>

                        <div class="table-responsive">
                            <table class="table table-bordered align-middle">
                                <thead class="table-dark">
                                <tr>
                                    <th style="min-width:260px;">Module</th>
                                    <?php foreach ($actions as $a): ?>
                                        <th class="text-center"><?= rb_h($a) ?></th>
                                    <?php endforeach; ?>
                                </tr>
                                </thead>

                                <tbody>
                                <?php foreach ($modules as $m): ?>
                                    <tr>
                                        <td>
                                            <strong><?= rb_h($m['module_label_ar'] ?: $m['module_label']) ?></strong>
                                            <div class="text-muted small"><?= rb_h($m['module_key']) ?></div>
                                        </td>

                                        <?php foreach ($actions as $a): ?>
                                            <?php
                                            $checked = (int)($actionPerms[(int)$role['id']][$m['module_key']][$a] ?? 1) === 1;
                                            $isSuper = ($role['code'] ?? '') === 'superadmin';
                                            ?>
                                            <td class="text-center">
                                                <input type="checkbox"
                                                       name="actions[<?= (int)$role['id'] ?>][<?= rb_h($m['module_key']) ?>][<?= rb_h($a) ?>]"
                                                       value="1"
                                                       <?= $checked ? 'checked' : '' ?>
                                                       <?= $isSuper ? 'disabled' : '' ?>>
                                                <?php if ($isSuper): ?>
                                                    <input type="hidden"
                                                           name="actions[<?= (int)$role['id'] ?>][<?= rb_h($m['module_key']) ?>][<?= rb_h($a) ?>]"
                                                           value="1">
                                                <?php endif; ?>
                                            </td>
                                        <?php endforeach; ?>
                                    </tr>
                                <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                <?php endforeach; ?>

                <button class="btn btn-success btn-lg rounded-pill px-5">
                    Save CRUD / Action Permissions
                </button>
            </form>
        </div>
    </div>
</div>
