<?php
/** @var array $user @var string $sys @var string $sysName @var array $pages @var bool $edit */
$labelMap = [];
foreach (\subsys_nav($sys) as $it) { $labelMap[$it['k']] = $it['label']; }
$raw   = $user[$sys . '_perms'] ?? '';
$dec   = is_string($raw) && $raw !== '' ? @json_decode($raw, true) : [];
$cur   = is_array($dec) ? array_keys(array_filter($dec)) : [];
$roleKey = $sys . '_role';
$role  = $user[$roleKey] ?? 'user';
$upper = strtoupper($sys);
?>
<div class="page-head">
    <h1><?php echo $edit ? '编辑用户' : '新增用户'; ?></h1>
    <p class="sub"><?php echo e($sysName); ?></p>
</div>

<?php echo flash_html(); ?>

<div class="panel form-card">
    <form method="post" action="<?php echo site_url($upper . '/users/' . ($edit ? 'update/' . $user['id'] : 'store')); ?>">
        <?php echo csrf_field(); ?>
        <div class="form-row">
            <label>用户名 <span class="req">*</span></label>
            <input type="text" name="username" value="<?php echo e($user['username'] ?? ''); ?>" <?php echo $edit ? 'readonly' : ''; ?> required>
            <?php if ($edit): ?><small class="muted">用户名不可修改</small><?php endif; ?>
        </div>
        <div class="form-row">
            <label>姓名</label>
            <input type="text" name="name" value="<?php echo e($user['name'] ?? ''); ?>">
        </div>
        <div class="form-row">
            <label>角色</label>
            <div class="radio-row">
                <label class="radio"><input type="radio" name="<?php echo e($roleKey); ?>" value="user" <?php echo $role !== 'admin' ? 'checked' : ''; ?>> 普通用户</label>
                <label class="radio"><input type="radio" name="<?php echo e($roleKey); ?>" value="admin" <?php echo $role === 'admin' ? 'checked' : ''; ?>> 管理员（可管理本系统用户与权限）</label>
            </div>
        </div>
        <div class="form-row">
            <label>页面权限</label>
            <div class="perm-grid">
                <?php foreach ($pages as $p): ?>
                    <?php if ($p === 'dashboard') continue; ?>
                    <label class="check"><input type="checkbox" name="perm_<?php echo e($p); ?>" value="1" <?php echo in_array($p, $cur, true) ? 'checked' : ''; ?>> <?php echo e($labelMap[$p] ?? $p); ?></label>
                <?php endforeach; ?>
            </div>
            <small class="muted">仪表盘对所有用户默认可见；未勾选的页面该用户不可访问（侧栏隐藏且不可直访）。</small>
        </div>
        <div class="form-row">
            <label>密码 <?php if (!$edit): ?><span class="req">*</span><?php endif; ?></label>
            <input type="password" name="password" autocomplete="new-password" <?php echo $edit ? '' : 'required'; ?>>
            <small class="muted"><?php echo $edit ? '留空则保持原密码' : '请设置登录密码（至少 6 位）'; ?></small>
        </div>
        <div class="form-row">
            <label>状态</label>
            <div class="radio-row">
                <label class="radio"><input type="radio" name="status" value="1" <?php echo (!empty($user['status'])) ? 'checked' : ''; ?>> 启用</label>
                <label class="radio"><input type="radio" name="status" value="0" <?php echo (empty($user['status'])) ? 'checked' : ''; ?>> 停用</label>
            </div>
        </div>
        <div class="form-actions">
            <button type="submit" class="btn"><?php echo $edit ? '保存修改' : '创建用户'; ?></button>
            <a class="btn-soft" href="<?php echo site_url($upper . '/users'); ?>">取消</a>
        </div>
    </form>
</div>
