<?php
/** @var array $users @var string $sys @var string $sysName @var array $pages */
$labelMap = [];
foreach (\subsys_nav($sys) as $it) { $labelMap[$it['k']] = $it['label']; }
$roleLabels = ['super_admin' => '超级管理员', 'admin' => '管理员', 'user' => '普通用户', 'none' => '无'];
$upper = strtoupper($sys);
?>
<div class="page-head">
    <h1>用户管理</h1>
    <p class="sub"><?php echo e($sysName); ?> · 管理系统内的用户账号及其页面访问权限</p>
</div>

<?php echo flash_html(); ?>

<div class="panel">
    <div class="panel-bar">
        <h3>账号列表</h3>
        <a class="btn" href="<?php echo site_url($upper . '/users/create'); ?>"><?php echo admin_icon('user-plus', 16); ?> 新增用户</a>
    </div>
    <div class="table-wrap">
        <table class="tbl">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>用户名</th>
                    <th>姓名</th>
                    <th>角色</th>
                    <th>页面权限</th>
                    <th>状态</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
            <?php foreach ($users as $u): ?>
                <?php
                    $raw = $u[$sys . '_perms'] ?? '';
                    $dec = is_string($raw) && $raw !== '' ? @json_decode($raw, true) : [];
                    $perms = is_array($dec) ? array_keys(array_filter($dec)) : [];
                    $role  = $u[$sys . '_role'] ?? 'user';
                ?>
                <tr>
                    <td><?php echo (int)$u['id']; ?></td>
                    <td><?php echo e($u['username']); ?></td>
                    <td><?php echo e($u['name'] ?? ''); ?></td>
                    <td><span class="role-badge role-<?php echo e($role); ?>"><?php echo e($roleLabels[$role] ?? $role); ?></span></td>
                    <td class="perm-cell">
                        <?php if (empty($perms)): ?>
                            <span class="muted">仅仪表盘</span>
                        <?php else: foreach ($perms as $p): ?>
                            <span class="perm-chip"><?php echo e($labelMap[$p] ?? $p); ?></span>
                        <?php endforeach; endif; ?>
                    </td>
                    <td>
                        <?php if (!empty($u['status'])): ?><span class="st st-on">启用</span><?php else: ?><span class="st st-off">停用</span><?php endif; ?>
                    </td>
                    <td class="row-actions">
                        <a href="<?php echo site_url($upper . '/users/edit/' . $u['id']); ?>">编辑</a>
                        <a href="<?php echo site_url($upper . '/users/reset/' . $u['id']); ?>">重置密码</a>
                        <form method="post" class="inline-form" action="<?php echo site_url($upper . '/users/destroy/' . $u['id']); ?>" data-confirm="确认删除该用户？">
                            <?php echo csrf_field(); ?>
                            <button type="submit" class="link-danger">删除</button>
                        </form>
                    </td>
                </tr>
            <?php endforeach; ?>
            <?php if (empty($users)): ?>
                <tr><td colspan="7" class="muted" style="text-align:center;padding:24px">暂无用户</td></tr>
            <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
