<?php
function role_h($v) {
    return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8');
}
$roles = $roles ?? [];
$matrix = $matrix ?? [];
?>

<style>
.role-card{
    border:0;
    border-radius:22px;
    box-shadow:0 14px 38px rgba(15,23,42,.08);
    height:100%;
}
.role-code{
    background:#e0f2fe;
    color:#075985;
    border-radius:999px;
    padding:7px 12px;
    font-weight:800;
    display:inline-block;
}
</style>

<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> إدارة الأدوار والصلاحيات</h3>
            <div class="opacity-75">Role matrix and privilege overview</div>
        </div>
    </div>

    <div class="row g-3 mb-4">
        <?php foreach ($roles as $r): ?>
            <div class="col-xl-3 col-md-4 col-sm-6">
                <div class="card role-card">
                    <div class="card-body">
                        <div class="d-flex justify-content-between align-items-start gap-2">
                            <div>
                                <h5 class="fw-bold mb-1"><?= role_h($r['name']) ?></h5>
                                <span class="role-code"><?= role_h($r['code']) ?></span>
                            </div>
                            <div class="text-end">
                                <div class="fw-bold"><?= (int)$r['global_users_count'] ?></div>
                                <div class="text-muted small">users</div>
                            </div>
                        </div>

                        <hr>

                        <div class="text-muted small mb-2">Conference memberships: <?= (int)$r['conference_users_count'] ?></div>

                        <?php foreach (($matrix[$r['code']] ?? ['Custom role']) as $m): ?>
                            <div class="small mb-1">
                                <i class="fa fa-check text-success"></i>
                                <?= role_h($m) ?>
                            </div>
                        <?php endforeach; ?>
                    </div>
                </div>
            </div>
        <?php endforeach; ?>
    </div>

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

            <div class="table-responsive">
                <table class="table table-bordered align-middle">
                    <thead class="table-dark">
                        <tr>
                            <th>ID</th>
                            <th>Name</th>
                            <th>Code</th>
                            <th>Global Users</th>
                            <th>Conference Users</th>
<th class="text-center">
    Actions
</th>                        
</tr>
                    </thead>
                    <tbody>
                    <?php foreach ($roles as $r): ?>
                        <tr>
                            <td><?= (int)$r['id'] ?></td>
                            <td class="fw-bold"><?= role_h($r['name']) ?></td>
                            <td><span class="badge bg-primary"><?= role_h($r['code']) ?></span></td>
                            <td><?= (int)$r['global_users_count'] ?></td>
                            <td><?= (int)$r['conference_users_count'] ?></td>

<td class="text-center">

    <a
href="/easychair/admin/roles/edit?id=<?= (int)$r['id'] ?>"
class="btn btn-sm btn-warning rounded-pill">

        <i class="fa fa-edit"></i>

    </a>

    <?php if (!in_array($r['code'], [

        'superadmin',
        'admin',
        'author',
        'reviewer',
        'organizer'

    ], true)): ?>

        <a
href="/easychair/admin/roles/delete?id=<?= (int)$r['id'] ?>"
class="btn btn-sm btn-danger rounded-pill"
onclick="return confirm('Delete role?')">

            <i class="fa fa-trash"></i>

        </a>

    <?php endif; ?>

</td>
                        </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
            </div>

        </div>
    </div>

<?php
$users = $users ?? [];
$conferences = $conferences ?? [];
$organizations = $organizations ?? [];
?>

<hr class="my-4">

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

    <div class="card-body">

        <h5 class="fw-bold mb-3">

            <i class="fa fa-user-shield"></i>
            Assign User Role

        </h5>

        <form
            method="POST"
            action="/easychair/admin/roles/assign"
            class="row g-3"
        >

            <div class="col-md-3">

                <label class="form-label fw-bold">
                    User
                </label>

                <select
                    name="user_id"
                    class="form-select"
                    required
                >

                    <option value="">
                        Select user
                    </option>

                    <?php foreach ($users as $u): ?>

                        <option
                            value="<?= (int)$u['id'] ?>"
                        >

                            <?= role_h(
                                ($u['first_name'] ?? '')
                                . ' '
                                . ($u['last_name'] ?? '')
                                . ' (' .
                                ($u['email'] ?? '')
                                . ')'
                            ) ?>

                        </option>

                    <?php endforeach; ?>

                </select>

            </div>

            <div class="col-md-3">

                <label class="form-label fw-bold">
                    Organization
                </label>

                <select
                    name="org_id"
                    class="form-select"
                    required
                >

                    <option value="">
                        Select organization
                    </option>

                    <?php foreach ($organizations as $o): ?>

                        <option
                            value="<?= (int)$o['id'] ?>"
                        >

                            <?= role_h(
                                $o['name'] ?? ''
                            ) ?>

                        </option>

                    <?php endforeach; ?>

                </select>

            </div>

            <div class="col-md-3">

                <label class="form-label fw-bold">
                    Conference
                </label>

                <select
                    name="conference_id"
                    class="form-select"
                    required
                >

                    <option value="">
                        Select conference
                    </option>

                    <?php foreach ($conferences as $c): ?>

                        <option
                            value="<?= (int)$c['id'] ?>"
                        >

                            <?= role_h(
                                $c['title'] ?? ''
                            ) ?>

                        </option>

                    <?php endforeach; ?>

                </select>

            </div>

            <div class="col-md-2">

                <label class="form-label fw-bold">
                    Role
                </label>

                <select
                    name="role_code"
                    class="form-select"
                    required
                >

                    <option value="">
                        Select role
                    </option>

                    <option value="org_admin">
                        Organization Admin
                    </option>

                    <option value="conference_chair">
                        Conference Chair
                    </option>

                    <option value="organizer">
                        Organizer
                    </option>

                    <option value="track_chair">
                        Track Chair
                    </option>

                    <option value="reviewer">
                        Reviewer
                    </option>

                    <option value="author">
                        Author
                    </option>

                </select>

            </div>

            <div class="col-md-1 d-flex align-items-end">

                <button
                    type="submit"
                    class="btn btn-primary w-100"
                >

                    Assign

                </button>

            </div>

        </form>

    </div>

</div>


</div>
