<?php
function rs_h($v) {
    return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8');
}
function rs_t($k, $f) {
    if (function_exists('ec_t')) return ec_t($k, $f);
    if (function_exists('t')) return t($k, $f);
    return $f;
}

$conferences = $conferences ?? [];
$roles = $roles ?? [];
$summary = $summary ?? [];
$conference = $conference ?? [];
$success = $_SESSION['flash_success'] ?? '';
$error = $_SESSION['flash_error'] ?? '';
unset($_SESSION['flash_success'], $_SESSION['flash_error']);
?>

<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">
            <h3 class="fw-bold mb-1">
                <i class="fa fa-copy"></i>
                <?= rs_h(rs_t('admin.rbac_scope.title','RBAC Scope Manager')) ?>
            </h3>
            <div class="opacity-75">
                <?= rs_h(($conference['org_name'] ?? '') . ' / ' . ($conference['short_name'] ?? '')) ?>
            </div>
        </div>
    </div>

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

    <?php if ($error): ?>
        <div class="alert alert-danger"><?= rs_h($error) ?></div>
    <?php endif; ?>

    <div class="row g-4">
        <div class="col-lg-7">
            <div class="card border-0 shadow-sm h-100" style="border-radius:22px;">
                <div class="card-body p-4">
                    <h5 class="fw-bold mb-3">
                        <?= rs_h(rs_t('admin.rbac_scope.copy','Copy Permissions')) ?>
                    </h5>

                    <div class="alert alert-warning">
                        <?= rs_h(rs_t('admin.rbac_scope.warning','This will replace target permissions for selected roles.')) ?>
                    </div>

                    <form method="post" action="/easychair/admin/rbac-scope/copy" class="row g-3">
                        <div class="col-md-6">
                            <label class="form-label fw-bold"><?= rs_h(rs_t('admin.rbac_scope.source','Source Conference')) ?></label>
                            <select name="source_conference_id" class="form-select" required>
                                <?php foreach ($conferences as $c): ?>
                                    <option value="<?= (int)$c['id'] ?>" <?= (int)$c['id'] === (int)($conference['id'] ?? 0) ? 'selected' : '' ?>>
                                        <?= rs_h(($c['org_name'] ?? 'Org '.$c['org_id']) . ' / ' . ($c['short_name'] ?? ('#'.$c['id']))) ?>
                                    </option>
                                <?php endforeach; ?>
                            </select>
                        </div>

                        <div class="col-md-6">
                            <label class="form-label fw-bold"><?= rs_h(rs_t('admin.rbac_scope.target','Target Conference')) ?></label>
                            <select name="target_conference_id" class="form-select" required>
                                <?php foreach ($conferences as $c): ?>
                                    <option value="<?= (int)$c['id'] ?>">
                                        <?= rs_h(($c['org_name'] ?? 'Org '.$c['org_id']) . ' / ' . ($c['short_name'] ?? ('#'.$c['id']))) ?>
                                    </option>
                                <?php endforeach; ?>
                            </select>
                        </div>

                        <div class="col-12">
                            <label class="form-label fw-bold">Roles</label>
                            <div class="row g-2">
                                <?php foreach ($roles as $r): ?>
                                    <div class="col-md-4">
                                        <label class="border rounded-3 p-2 d-flex gap-2 align-items-center">
                                            <input type="checkbox" name="role_ids[]" value="<?= (int)$r['id'] ?>">
                                            <span>
                                                <strong><?= rs_h($r['code'] ?? '') ?></strong>
                                                <span class="text-muted small"><?= rs_h($r['name'] ?? '') ?></span>
                                            </span>
                                        </label>
                                    </div>
                                <?php endforeach; ?>
                            </div>
                        </div>

                        <div class="col-12">
                            <button class="btn btn-success rounded-pill px-5"
                                    onclick="return confirm('Copy permissions and replace target role permissions?')">
                                <i class="fa fa-copy"></i>
                                <?= rs_h(rs_t('admin.rbac_scope.copy','Copy Permissions')) ?>
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>

        <div class="col-lg-5">
            <div class="card border-0 shadow-sm h-100" style="border-radius:22px;">
                <div class="card-body p-4">
                    <h5 class="fw-bold mb-3">Current Conference RBAC Summary</h5>

                    <div class="table-responsive">
                        <table class="table table-bordered align-middle">
                            <thead class="table-dark">
                            <tr>
                                <th>Role</th>
                                <th>Total Rows</th>
                                <th>Visible Menus</th>
                            </tr>
                            </thead>
                            <tbody>
                            <?php foreach ($summary as $s): ?>
                                <tr>
                                    <td><strong><?= rs_h($s['code'] ?? '') ?></strong></td>
                                    <td><?= (int)($s['total'] ?? 0) ?></td>
                                    <td><span class="badge bg-primary"><?= (int)($s['visible'] ?? 0) ?></span></td>
                                </tr>
                            <?php endforeach; ?>
                            <?php if (!$summary): ?>
                                <tr>
                                    <td colspan="3" class="text-center text-muted py-4">No RBAC summary available.</td>
                                </tr>
                            <?php endif; ?>
                            </tbody>
                        </table>
                    </div>

                    <a href="/easychair/admin/conference-role-permissions" class="btn btn-outline-primary rounded-pill mt-2">
                        Manage Current Conference Permissions
                    </a>
                </div>
            </div>
        </div>
    </div>
</div>
