<?php
$h = static fn($v) => htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8');
$base = '/easychair/admin/users';
$qs = $_GET;

/*
|--------------------------------------------------------------------------
| PACK1301A FIX - Compatibility with original AdminUserController
|--------------------------------------------------------------------------
| Original controller passes $rows, not $users.
*/
$users = $users ?? $rows ?? [];
$total = $total ?? $totalUsers ?? count($users);
$page = $page ?? $currentPage ?? (int)($_GET['page'] ?? 1);
$pages = $pages ?? $totalPages ?? 1;
$perPage = $perPage ?? (int)($_GET['per_page'] ?? 25);
$q = $q ?? $search ?? (string)($_GET['q'] ?? '');
$roleFilter = $roleFilter ?? $roleFilterId ?? (string)($_GET['role_filter'] ?? '');
$statusFilter = $statusFilter ?? $statusFilterValue ?? (string)($_GET['status'] ?? '');
$conferenceId = $conferenceId ?? $selectedConference ?? (int)($_GET['conference_id'] ?? 0);
$conferences = $conferences ?? [];
$roles = $roles ?? [];
$flash = $flash ?? null;

/*
|--------------------------------------------------------------------------
| PACK1301 FINAL ROW ADAPTER
|--------------------------------------------------------------------------
| Real users table columns:
| id, first_name, last_name, email, mobile, job_title, is_active, created_at
*/
foreach ($users as &$u) {

    $first = trim((string)($u['first_name'] ?? ''));
    $last  = trim((string)($u['last_name'] ?? ''));
    $email = trim((string)($u['email'] ?? ''));

    $full = trim($first . ' ' . $last);
    if ($full === '') {
        $full = $email;
    }

    $u['name'] = $u['name'] ?? $full;
    $u['display_name'] = $u['display_name'] ?? $full;
    $u['username'] = $u['username'] ?? $email;
    $u['conference_roles'] = $u['conference_roles'] ?? ($u['global_role_name'] ?? $u['global_role'] ?? '');
    $u['status'] = $u['status'] ?? ((int)($u['is_active'] ?? 0) === 1 ? 'Active' : 'Inactive');
    $u['created_at'] = $u['created_at'] ?? '';
    $u['mobile'] = $u['mobile'] ?? '';
    $u['job_title'] = $u['job_title'] ?? '';
}
unset($u);



$csrf = $csrf ?? ($_SESSION['_csrf_admin_users'] ?? '');

?>
<style>
.ec-users-wrap{padding:24px;background:#f6f8fb;min-height:100vh}
.ec-users-head{display:flex;justify-content:space-between;gap:16px;align-items:flex-start;margin-bottom:18px}
.ec-users-title{font-size:28px;font-weight:800;color:#14213d;margin:0}
.ec-users-sub{color:#64748b;margin-top:6px}
.ec-card{background:#fff;border:1px solid #e5e7eb;border-radius:18px;box-shadow:0 10px 25px rgba(15,23,42,.06);padding:18px;margin-bottom:18px}
.ec-filter{display:grid;grid-template-columns:2fr 2fr 1.4fr 1fr auto;gap:12px;align-items:end}
.ec-label{font-weight:700;font-size:13px;color:#334155;margin-bottom:6px;display:block}
.ec-input,.ec-select{width:100%;border:1px solid #cbd5e1;border-radius:12px;padding:11px 12px;font-size:14px;background:#fff}
.ec-btn{border:0;border-radius:12px;padding:10px 14px;font-weight:800;cursor:pointer;text-decoration:none;display:inline-flex;align-items:center;gap:7px}
.ec-btn-primary{background:#1d4ed8;color:#fff}
.ec-btn-soft{background:#e0ecff;color:#1d4ed8}
.ec-btn-danger{background:#dc2626;color:#fff}
.ec-btn-warning{background:#f59e0b;color:#111827}
.ec-btn-dark{background:#111827;color:#fff}
.ec-table-wrap{overflow:auto;border-radius:16px;border:1px solid #e5e7eb}
.ec-table{width:100%;border-collapse:collapse;background:#fff;min-width:1100px}
.ec-table th{background:#0f172a;color:#fff;text-align:left;padding:13px;font-size:13px}
.ec-table td{border-top:1px solid #e5e7eb;padding:13px;vertical-align:top;font-size:14px}
.ec-muted{color:#64748b;font-size:13px}
.ec-role{display:inline-block;background:#ecfeff;color:#155e75;border:1px solid #a5f3fc;border-radius:999px;padding:4px 8px;margin:2px;font-size:12px;font-weight:700}
.ec-actions{display:flex;gap:8px;flex-wrap:wrap}
.ec-modal{position:fixed;inset:0;background:rgba(15,23,42,.55);display:none;align-items:center;justify-content:center;z-index:9999;padding:18px}
.ec-modal:target{display:flex}
.ec-modal-box{background:#fff;border-radius:20px;width:min(760px,96vw);max-height:90vh;overflow:auto;padding:22px;box-shadow:0 30px 80px rgba(0,0,0,.25)}
.ec-modal-head{display:flex;justify-content:space-between;align-items:center;margin-bottom:14px}
.ec-close{text-decoration:none;background:#f1f5f9;color:#0f172a;border-radius:10px;padding:8px 12px;font-weight:800}
.ec-form-grid{display:grid;grid-template-columns:1fr 1fr;gap:12px}
.ec-alert{padding:13px 15px;border-radius:14px;margin-bottom:16px;font-weight:700}
.ec-alert-success{background:#dcfce7;color:#166534}
.ec-alert-danger{background:#fee2e2;color:#991b1b}
.ec-alert-warning{background:#fef3c7;color:#92400e}
.ec-pages{display:flex;gap:7px;align-items:center;flex-wrap:wrap;margin-top:16px}
.ec-page{padding:8px 11px;border-radius:10px;background:#fff;border:1px solid #cbd5e1;text-decoration:none;color:#0f172a;font-weight:700}
.ec-page.active{background:#1d4ed8;color:#fff;border-color:#1d4ed8}
@media(max-width:900px){.ec-filter,.ec-form-grid{grid-template-columns:1fr}.ec-users-head{display:block}}
</style>

<div class="ec-users-wrap">
    <div class="ec-users-head">
        <div>
            <h1 class="ec-users-title">SuperAdmin Users</h1>
            <div class="ec-users-sub">
                Conference-scoped users, all roles, search, paging, edit, delete, and password reset.
            </div>
        </div>
        <a class="ec-btn ec-btn-soft" href="<?= $h($base) ?>">Refresh</a>

<a
href="#membershipModal"
class="ec-btn ec-btn-primary"
style="margin-left:10px"
>
+ Add Existing User
</a>

    </div>

    <?php if (!empty($flash)): ?>
        <div class="ec-alert ec-alert-<?= $h($flash['type']) ?>">
            <?= $h($flash['message']) ?>
        </div>
    <?php endif; ?>

    <div class="ec-card">
        <form method="get" action="<?= $h($base) ?>" class="ec-filter">
            <div>
                <label class="ec-label">Conference</label>
                <select class="ec-select" name="conference_id">
                    <?php foreach ($conferences as $c): ?>
                        <option value="<?= (int)$c['id'] ?>" <?= (int)$c['id'] === (int)$conferenceId ? 'selected' : '' ?>>
                            #<?= (int)$c['id'] ?> -
<?= $h(
    $c['short_name']
    ??
    $c['title']
    ??
    $c['conference_name']
    ??
    ('Conference '.$c['id'])
) ?>
                        </option>
                    <?php endforeach; ?>
                </select>
            </div>

            <div>
                <label class="ec-label">Search</label>
                <input class="ec-input" name="q" value="<?= $h($q) ?>" placeholder="Name, email, username...">
            </div>

            <div>
                <label class="ec-label">Role</label>
                <select class="ec-select" name="role">
                    <option value="">All roles</option>
                    <?php foreach ($roles as $r): ?>
                        <option value="<?= $h($r['name']) ?>" <?= $roleFilter === $r['name'] ? 'selected' : '' ?>>
                            <?= $h($r['name']) ?>
                        </option>
                    <?php endforeach; ?>
                </select>
            </div>

            <div>
                <label class="ec-label">Per page</label>
                <select class="ec-select" name="per_page">
                    <?php foreach ([10,25,50,100] as $pp): ?>
                        <option value="<?= $pp ?>" <?= $perPage === $pp ? 'selected' : '' ?>><?= $pp ?></option>
                    <?php endforeach; ?>
                </select>
            </div>

            <button class="ec-btn ec-btn-primary" type="submit">Filter</button>
        </form>
    </div>

    

<!-- ========================================================= -->
<!-- Enterprise Membership Modal -->
<!-- ========================================================= -->

<div id="membershipModal" class="ec-modal">

<div class="ec-modal-box">

<div class="ec-modal-head">

<h2>
Add User To Conference
</h2>

<a href="#" class="ec-close">
Close
</a>

</div>

<form
method="post"
action="/easychair/admin/users/conference-membership/store"
>

<div class="ec-form-grid">

<div>

<label class="ec-label">

Conference

</label>

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

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

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

<?= htmlspecialchars(
$c['short_name']
??
$c['title']
??
$c['conference_name']
??
('Conference '.$c['id'])
) ?>

</option>

<?php endforeach; ?>

</select>

</div>

<div>

<label class="ec-label">

Conference Role

</label>

<select
class="ec-select"
name="role_id"
required
>

<?php foreach($roles as $r): ?>

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

<?= htmlspecialchars($r['name']) ?>

</option>

<?php endforeach; ?>

</select>

</div>

<div style="grid-column:1/-1">

<label class="ec-label">

Existing User

</label>

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

<option value="">

Select User

</option>

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

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

<?= htmlspecialchars(
($u['display_name']
??
trim(($u['first_name']??'').' '.($u['last_name']??'')))
.' - '.
($u['email']??'')
) ?>

</option>

<?php endforeach; ?>

</select>

</div>

</div>

<div
style="
margin-top:20px;
display:flex;
justify-content:flex-end;
gap:10px;
"
>

<button
class="ec-btn ec-btn-primary"
>

Assign User

</button>

<a
href="#"
class="ec-btn ec-btn-soft"
>

Cancel

</a>

</div>

</form>

</div>

</div>




<?php require __DIR__ . '/partials/_bulk_toolbar.php'; ?>

<div class="ec-card">
        <div class="ec-muted" style="margin-bottom:12px">
            Showing <?= count($users) ?> of <?= (int)$total ?> users for selected conference.
        </div>

        <div class="ec-table-wrap">
            <table class="ec-table">
                <thead>
                    <tr>
                        <th width="40">
<input
type="checkbox"
id="ecSelectAll">
</th>

<th>ID</th>
                        <th>User</th>
                        <th>Email</th>
                        <th>Username</th>
                        <th>Conference Roles</th>
                        <th>Status</th>
                        <th>Created</th>
                        <th style="width:290px">Actions</th>
                    </tr>
                </thead>
                <tbody>
                <?php if (empty($users)): ?>
                    <tr>
                        <td colspan="8" class="ec-muted">No users found for this conference/filter.</td>
                    </tr>
                <?php endif; ?>

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

<input
type="checkbox"
class="ecUserSelect"
value="<?= (int)$u['id'] ?>">

</td>

<td>

#<?= (int)$u['id'] ?>

</td>
                        <td><strong><?= $h($u['display_name']) ?></strong></td>
                        <td><?= $h($u['email']) ?></td>
                        <td><?= $h($u['username']) ?></td>
                        <td>
                            <?php
                            $roleList = array_filter(array_map('trim', explode(',', (string)$u['conference_roles'])));
                            if (!$roleList) {
                                echo '<span class="ec-muted">No conference role</span>';
                            } else {
                                foreach ($roleList as $rr) {
                                    echo '<span class="ec-role">' . $h($rr) . '</span>';
                                }
                            }
                            ?>
                        </td>
                        <td><?= $h($u['status']) ?></td>
                        <td><span class="ec-muted"><?= $h($u['created_at']) ?></span></td>
                        <td>
                            <div class="ec-actions">
                                <a class="ec-btn ec-btn-soft" href="#edit<?= (int)$u['id'] ?>">Edit</a>
                                <a class="ec-btn ec-btn-warning" href="#pass<?= (int)$u['id'] ?>">Reset</a>
                                <a class="ec-btn ec-btn-danger" href="#delete<?= (int)$u['id'] ?>">Delete</a>
                            </div>
                        </td>
                    </tr>

                    <div id="edit<?= (int)$u['id'] ?>" class="ec-modal">
                        <div class="ec-modal-box">
                            <div class="ec-modal-head">
                                <h2>Edit User #<?= (int)$u['id'] ?></h2>
                                <a class="ec-close" href="#">Close</a>
                            </div>
                            <form method="post" action="/easychair/admin/users/update">
                                <input type="hidden" name="_csrf" value="<?= $h($csrf) ?>">
                                <input type="hidden" name="id" value="<?= (int)$u['id'] ?>">
                                <input type="hidden" name="conference_id" value="<?= (int)$conferenceId ?>">
                                <input type="hidden" name="q" value="<?= $h($q) ?>">
                                <input type="hidden" name="role" value="<?= $h($roleFilter) ?>">
                                <input type="hidden" name="page" value="<?= (int)$page ?>">

                                <div class="ec-form-grid">
                                    <div>
                                        <label class="ec-label">Name / Full name</label>
                                        <input class="ec-input" name="name" value="<?= $h($u['display_name']) ?>">
                                        <input type="hidden" name="full_name" value="<?= $h($u['display_name']) ?>">
                                    </div>
                                    <div>
                                        <label class="ec-label">Email</label>
                                        <input class="ec-input" name="email" value="<?= $h($u['email']) ?>">
                                    </div>
                                    <div>
                                        <label class="ec-label">Username</label>
                                        <input class="ec-input" name="username" value="<?= $h($u['username']) ?>">
                                    </div>
                                    <div>
    <label class="ec-label">Mobile</label>
    <input class="ec-input"
           name="mobile"
           value="<?= $h($u['mobile']) ?>">
</div>

<div>
    <label class="ec-label">Job Title</label>
    <input class="ec-input"
           name="job_title"
           value="<?= $h($u['job_title']) ?>">
</div>

<div>
    <label class="ec-label">Global Role</label>
    <select class="ec-select" name="role_id">
        <?php foreach($roles as $r): ?>
            <option
                value="<?= (int)$r['id'] ?>"
                <?= (int)$r['id']==(int)$u['role_id'] ? 'selected' : '' ?>>
                <?= $h($r['name']) ?>
            </option>
        <?php endforeach; ?>
    </select>
</div>
                                    <div>
                                        <label class="ec-label">Status</label>
                                        <select class="ec-select" name="status">
                                            <?php foreach (['active','inactive','pending','blocked'] as $s): ?>
                                                <option value="<?= $s ?>" <?= (string)$u['status'] === $s ? 'selected' : '' ?>><?= ucfirst($s) ?></option>
                                            <?php endforeach; ?>
                                        </select>
                                    </div>
                                </div>

                                <div style="margin-top:16px">
                                    <button class="ec-btn ec-btn-primary" type="submit">Save User</button>
                                </div>
                            </form>
                        </div>
                    </div>

                    <div id="pass<?= (int)$u['id'] ?>" class="ec-modal">
                        <div class="ec-modal-box">
                            <div class="ec-modal-head">
                                <h2>Reset Password</h2>
                                <a class="ec-close" href="#">Close</a>
                            </div>
                            <form method="post" action="/easychair/admin/users/reset-password">
                                <input type="hidden" name="_csrf" value="<?= $h($csrf) ?>">
                                <input type="hidden" name="id" value="<?= (int)$u['id'] ?>">
                                <input type="hidden" name="conference_id" value="<?= (int)$conferenceId ?>">
                                <input type="hidden" name="q" value="<?= $h($q) ?>">
                                <input type="hidden" name="role" value="<?= $h($roleFilter) ?>">
                                <input type="hidden" name="page" value="<?= (int)$page ?>">

                                <label class="ec-label">New Password minimum 8 characters</label>
                                <input class="ec-input" type="text" name="password" placeholder="Enter new password" required minlength="8">

                                <div style="margin-top:16px">
                                    <button class="ec-btn ec-btn-warning" type="submit">Reset Password</button>
                                </div>
                            </form>
                        </div>
                    </div>

                    <div id="delete<?= (int)$u['id'] ?>" class="ec-modal">
                        <div class="ec-modal-box">
                            <div class="ec-modal-head">
                                <h2>Delete User</h2>
                                <a class="ec-close" href="#">Close</a>
                            </div>
                            <p>Are you sure you want to delete <strong><?= $h($u['display_name']) ?></strong>?</p>
                            <p class="ec-muted">This removes conference role links and user conference links first.</p>
                            <form method="post" action="/easychair/admin/users/delete">
                                <input type="hidden" name="_csrf" value="<?= $h($csrf) ?>">
                                <input type="hidden" name="id" value="<?= (int)$u['id'] ?>">
                                <input type="hidden" name="conference_id" value="<?= (int)$conferenceId ?>">
                                <input type="hidden" name="q" value="<?= $h($q) ?>">
                                <input type="hidden" name="role" value="<?= $h($roleFilter) ?>">
                                <input type="hidden" name="page" value="<?= (int)$page ?>">
                                <button class="ec-btn ec-btn-danger" type="submit">Yes, Delete User</button>
                            </form>
                        </div>
                    </div>
                <?php endforeach; ?>
                </tbody>
            </table>
        </div>

        <div class="ec-pages">
            <?php
            $start = max(1, $page - 3);
            $end = min($pages, $page + 3);
            for ($p = $start; $p <= $end; $p++):
                $qs['page'] = $p;
                $qs['conference_id'] = $conferenceId;
                $qs['q'] = $q;
                $qs['role'] = $roleFilter;
                $qs['per_page'] = $perPage;
            ?>
                <a class="ec-page <?= $p === $page ? 'active' : '' ?>" href="<?= $h($base . '?' . http_build_query($qs)) ?>">
                    <?= $p ?>
                </a>
            <?php endfor; ?>
        </div>
    </div>
</div>

<script>

const box=document.getElementById("ecUserSearch");

if(box){

box.addEventListener("keyup",function(){

const q=this.value.trim();

if(q.length<2){

document.getElementById("ecUserResults").style.display="none";

return;

}

fetch("/easychair/admin/users/search?q="+encodeURIComponent(q))

.then(r=>r.json())

.then(rows=>{

let html="";

rows.forEach(r=>{

html+=`
<div
style="padding:10px;border-bottom:1px solid #eee;cursor:pointer"
onclick="
document.getElementById('ecUserSearch').value='${r.first_name} ${r.last_name}';
document.getElementById('ecUserId').value='${r.id}';
document.getElementById('ecUserResults').style.display='none';
"
>

<strong>${r.first_name} ${r.last_name}</strong>

<br>

<small>${r.email}</small>






</div>
`;

});

const res=document.getElementById("ecUserResults");

res.innerHTML=html;

res.style.display="block";

});

});

}

</script>


<script>

document.addEventListener("DOMContentLoaded",function(){

const all=document.getElementById("ecSelectAll");

if(!all)return;

function refreshBulkToolbar(){

let n=0;

document.querySelectorAll(".ecUserSelect").forEach(c=>{

if(c.checked)n++;

});

document.getElementById("ecSelectedCount").innerHTML=n+" Selected";

document.getElementById("ecBulkToolbar").style.display=

n>0

?

"flex"

:

"none";

}

all.addEventListener("change",function(){

document.querySelectorAll(".ecUserSelect").forEach(c=>{

c.checked=all.checked;

});

refreshBulkToolbar();

});

document.querySelectorAll(".ecUserSelect").forEach(c=>{

c.addEventListener("change",refreshBulkToolbar);

});


});

function bulkAction(status){

const ids=[];

document.querySelectorAll(".ecUserSelect:checked").forEach(c=>{

ids.push(c.value);

});

if(ids.length===0){

alert("Please select users.");

return;

}

const f=document.createElement("form");

f.method="post";

f.action="/easychair/admin/users/bulk-status";

ids.forEach(function(id){

const i=document.createElement("input");

i.type="hidden";

i.name="users[]";

i.value=id;

f.appendChild(i);

});

const s=document.createElement("input");

s.type="hidden";

s.name="status";

s.value=status;

f.appendChild(s);

document.body.appendChild(f);

f.submit();

}

</script>


<script>
function openBulkConferenceModal(){

    const ids = [];

    document.querySelectorAll(".ecUserSelect:checked").forEach(function(c){
        ids.push(c.value);
    });

    if(ids.length === 0){
        alert("Please select users first.");
        return;
    }

    const box = document.getElementById("bulkConferenceUsers");

    if(box){
        box.innerHTML = "";

        ids.forEach(function(id){
            const input = document.createElement("input");
            input.type = "hidden";
            input.name = "users[]";
            input.value = id;
            box.appendChild(input);
        });
    }

    location.href = "#bulkAssignConferenceModal";
}
</script>


<script>

function showBulkConferenceModal(){

const ids=[];

document.querySelectorAll(".ecUserSelect:checked").forEach(function(c){

ids.push(c.value);

});

if(ids.length===0){

alert("Please select users first.");

return;

}

const box=document.getElementById("bulkConferenceUsers");

if(box){

box.innerHTML="";

ids.forEach(function(id){

const i=document.createElement("input");

i.type="hidden";

i.name="users[]";

i.value=id;

box.appendChild(i);

});

}

document.getElementById(
"bulkAssignConferenceModal"
).style.display="flex";

}

function hideBulkConferenceModal(){

document.getElementById(
"bulkAssignConferenceModal"
).style.display="none";

}



<div
id="bulkAssignConferenceModal"
class="ec-modal"
>

<div class="ec-modal-box">

<div class="ec-modal-head">

<h2>

Bulk Conference Assignment

</h2>

<a
href="javascript:void(0)"
class="ec-close"
onclick="hideBulkConferenceModal()"
>

Close

</a>

</div>

<form
method="post"
action="/easychair/admin/users/bulk-assign-conference"
id="bulkConferenceForm"
>

<div class="ec-form-grid">

<div>

<label class="ec-label">

Conference

</label>

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

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

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

<?= htmlspecialchars(
$c['short_name']
??
$c['title_en']
??
$c['title_ar']
??
('Conference '.$c['id'])
) ?>

</option>

<?php endforeach; ?>

</select>

</div>

<div>

<label class="ec-label">

Conference Role

</label>

<select
class="ec-select"
name="role_id"
required
>

<?php foreach($roles as $r): ?>

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

<?= htmlspecialchars($r['name']) ?>

</option>

<?php endforeach; ?>

</select>

</div>

</div>

<div
id="bulkConferenceUsers"
style="display:none"
></div>

<br>

<button
class="ec-btn ec-btn-primary"
>

Assign Selected Users

</button>

</form>

</div>

</div>

</script>

