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

$roles = $roles ?? [];
$catalog = $catalog ?? [];
$matrix = $matrix ?? [];

$selectedRoleId = (int)($_GET['role_id'] ?? ($roles[0]['id'] ?? 0));
?>

<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-user-shield"></i>
                صلاحيات أدوار المؤتمر
            </h3>
            <div class="opacity-75">
                Conference-level privilege matrix for organization users and conference roles.
            </div>
        </div>
    </div>

    <?php if (!empty($_GET['saved'])): ?>
        <div class="alert alert-success">تم حفظ الصلاحيات بنجاح.</div>
    <?php endif; ?>

    <div class="card border-0 shadow-sm mb-4" style="border-radius:22px;">
        <div class="card-body p-4">
            <form method="get" action="/easychair/admin/privilege-matrix" class="row g-2 align-items-end">
                <div class="col-md-5">
                    <label class="form-label fw-bold">اختر الدور / Select Role</label>
                    <select name="role_id" class="form-select" onchange="this.form.submit()">
                        <?php foreach ($roles as $r): ?>
                            <option value="<?= (int)$r['id'] ?>" <?= $selectedRoleId === (int)$r['id'] ? 'selected' : '' ?>>
                                <?= pm_h(($r['name'] ?? '') . ' / ' . ($r['code'] ?? '')) ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>

                <div class="col-md-2">
                    <button class="btn btn-primary w-100">Load</button>
                </div>
            </form>
        </div>
    </div>

    <form method="post" action="/easychair/admin/privilege-matrix/save">
        <input type="hidden" name="role_id" value="<?= (int)$selectedRoleId ?>">

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

                <div class="d-flex justify-content-between align-items-center flex-wrap gap-2 mb-3">
                    <h5 class="fw-bold mb-0">Privilege Catalog</h5>
                    <button class="btn btn-success rounded-pill px-4">
                        <i class="fa fa-save"></i>
                        Save Privileges
                    </button>
                </div>

                <div class="row g-2">
                    <?php foreach ($catalog as $p): ?>
                        <?php
                            $key = (string)($p['privilege_key'] ?? '');
                            $checked = !empty($matrix[$selectedRoleId][$key]);
                        ?>
                        <div class="col-xl-3 col-lg-4 col-md-6">
                            <label class="border rounded-4 p-3 d-block h-100" style="background:#fff;">
                                <div class="d-flex gap-2 align-items-start">
                                    <input type="checkbox"
                                           name="privileges[<?= pm_h($key) ?>]"
                                           value="1"
                                           <?= $checked ? 'checked' : '' ?>
                                           style="margin-top:6px;">
                                    <div>
                                        <strong><?= pm_h(($p['title_ar'] ?? '') ?: ($p['title_en'] ?? $key)) ?></strong>
                                        <div class="text-muted small"><?= pm_h($key) ?></div>
                                        <span class="badge bg-primary mt-1"><?= pm_h($p['module_key'] ?? 'general') ?></span>
                                    </div>
                                </div>
                            </label>
                        </div>
                    <?php endforeach; ?>

                    <?php if (!$catalog): ?>
                        <div class="col-12">
                            <div class="alert alert-warning">
                                No privileges found. Please run the Pack 465 SQL seed first.
                            </div>
                        </div>
                    <?php endif; ?>
                </div>

                <button class="btn btn-success btn-lg rounded-pill mt-4 px-5">
                    <i class="fa fa-save"></i>
                    Save Privileges
                </button>
            </div>
        </div>
    </form>
</div>
