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

$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-lock"></i> Business Logic RBAC</h3>
            <div class="opacity-75">Superadmin control for every role, permission, and CRUD action.</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">
            <form method="get" action="/easychair/admin/business-rbac" class="row g-2 align-items-end">
                <div class="col-md-4">
                    <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' : '' ?>>
                                <?= br_h($r['name'] . ' / ' . $r['code']) ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
            </form>
        </div>
    </div>

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

        <div class="card border-0 shadow-sm mb-4" style="border-radius:22px;">
            <div class="card-body">
                <h5 class="fw-bold mb-3">Business Permissions</h5>

                <div class="row g-2">
                    <?php foreach ($permissions as $p): ?>
                        <?php $checked = !empty($permMap[$selectedRoleId][$p['permission_key']]); ?>
                        <div class="col-lg-4 col-md-6">
                            <label class="border rounded-4 p-3 d-block h-100">
                                <input type="checkbox" name="permissions[<?= br_h($p['permission_key']) ?>]" value="1" <?= $checked ? 'checked' : '' ?>>
                                <strong><?= br_h($p['title_ar'] ?: $p['title_en']) ?></strong>
                                <div class="text-muted small"><?= br_h($p['permission_key']) ?></div>
                                <span class="badge bg-primary"><?= br_h($p['module_key']) ?></span>
                            </label>
                        </div>
                    <?php endforeach; ?>
                </div>
            </div>
        </div>

        <div class="card border-0 shadow-sm mb-4" style="border-radius:22px;">
            <div class="card-body">
                <h5 class="fw-bold mb-3">CRUD Policies</h5>

                <div class="table-responsive">
                    <table class="table table-bordered align-middle">
                        <thead class="table-dark">
                        <tr>
                            <th>Entity</th>
                            <th>View</th>
                            <th>Create</th>
                            <th>Update</th>
                            <th>Delete</th>
                            <th>Publish</th>
                        </tr>
                        </thead>
                        <tbody>
                        <?php foreach ($entities as $key => $label): ?>
                            <?php $row = $crudMap[$selectedRoleId][$key] ?? []; ?>
                            <tr>
                                <td>
                                    <strong><?= br_h($label) ?></strong>
                                    <div class="text-muted small"><?= br_h($key) ?></div>
                                </td>
                                <?php foreach (['view','create','update','delete','publish'] as $action): ?>
                                    <?php
                                    $col = 'can_' . $action;
                                    $checked = !empty($row[$col]);
                                    ?>
                                    <td class="text-center">
                                        <input type="checkbox" name="crud[<?= br_h($key) ?>][<?= br_h($action) ?>]" value="1" <?= $checked ? 'checked' : '' ?>>
                                    </td>
                                <?php endforeach; ?>
                            </tr>
                        <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>

                <button class="btn btn-success btn-lg">
                    <i class="fa fa-save"></i>
                    Save RBAC for Selected Role
                </button>
            </div>
        </div>
    </form>

</div>
