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

$rows = $rows ?? [];
?>

<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-users-gear"></i>
                User Scope Audit
            </h3>
            <div class="opacity-75">Check automatic conference, role, and landing page for each user.</div>
        </div>
    </div>

    <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/user-scope-audit" class="row g-3">
                <div class="col-md-9">
                    <input name="q" class="form-control" value="<?= usa_h($_GET['q'] ?? '') ?>" placeholder="Search email, name, role">
                </div>
                <div class="col-md-3">
                    <button class="btn btn-primary rounded-pill w-100">
                        <i class="fa fa-search"></i>
                        Search
                    </button>
                </div>
            </form>
        </div>
    </div>

    <div class="card border-0 shadow-sm" style="border-radius:22px;">
        <div class="card-body p-4">
            <div class="table-responsive">
                <table class="table table-bordered align-middle">
                    <thead class="table-dark">
                    <tr>
                        <th>User</th>
                        <th>Resolved Role</th>
                        <th>Org</th>
                        <th>Available Conferences</th>
                        <th>Auto Selected</th>
                        <th>Landing</th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php foreach ($rows as $r): ?>
                        <?php
                            $u = $r['user'] ?? [];
                            $available = $r['available'] ?? [];
                            $s = $r['selected'] ?? [];
                        ?>
                        <tr>
                            <td>
                                <strong><?= usa_h($u['email'] ?? '') ?></strong>
                                <div class="text-muted small">
                                    ID <?= (int)($u['id'] ?? 0) ?>
                                    <?= usa_h(($u['first_name'] ?? '') . ' ' . ($u['last_name'] ?? '') . ' ' . ($u['name'] ?? '')) ?>
                                </div>
                            </td>
                            <td>
                                <span class="badge bg-primary"><?= usa_h($s['role'] ?? '') ?></span>
                                <div class="text-muted small">DB role: <?= usa_h($u['role'] ?? $u['role_code'] ?? $u['user_role'] ?? '') ?></div>
                            </td>
                            <td><?= usa_h($s['org_id'] ?? ($u['org_id'] ?? '')) ?></td>
                            <td>
                                <?php foreach ($available as $c): ?>
                                    <div class="small">
                                        #<?= (int)($c['id'] ?? 0) ?>
                                        <?= usa_h($c['short_name'] ?? '') ?>
                                        <span class="text-muted">org <?= (int)($c['org_id'] ?? 0) ?></span>
                                    </div>
                                <?php endforeach; ?>
                                <?php if (!$available): ?>
                                    <span class="text-muted">None</span>
                                <?php endif; ?>
                            </td>
                            <td>
                                <strong><?= usa_h($s['conference_id'] ?? '') ?></strong>
                                <div class="text-muted small"><?= usa_h($s['short_name'] ?? '') ?></div>
                            </td>
                            <td><code><?= usa_h($s['landing'] ?? '') ?></code></td>
                        </tr>
                    <?php endforeach; ?>

                    <?php if (!$rows): ?>
                        <tr>
                            <td colspan="6" class="text-center text-muted py-4">No users found.</td>
                        </tr>
                    <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
