<?php /** @var array|null $user */ /** @var string $error */ /** @var string $ok */ /** @var array $crmPerms */ /** @var array $psiPerms */ ?>
<div class="page-head">
    <div><h1><?php echo $user ? '编辑用户' : '新增用户'; ?></h1></div>
    <a class="btn-ghost" href="<?php echo site_url('admin/users'); ?>">← 返回</a>
</div>
<?php if ($error): ?><div class="alert alert-err"><?php echo e($error); ?></div><?php endif; ?>
<?php if ($ok): ?><div class="alert alert-ok"><?php echo $ok; ?></div><?php endif; ?>

<?php
$showCrm = $user ? (($user['crm_role'] ?? 'none') !== 'none') : true;
$showPsi = $user ? (($user['psi_role'] ?? 'none') !== 'none') : true;
$crmPages = ['customers' => '客户管理', 'leads' => '商机线索', 'followups' => '跟进记录'];
$psiPages = ['materials' => '物料管理', 'products' => '成品管理', 'suppliers' => '供应商', 'purchases' => '采购入库', 'sales' => '销售出库', 'stock' => '库存流水'];
?>
<div class="admin-card" style="max-width:640px">
    <form method="post" action="<?php echo site_url($user ? 'admin/users/update/' . $user['id'] : 'admin/users/store'); ?>">
        <?php echo csrf_field(); ?>
        <div class="field">
            <label>登录账号</label>
            <input name="username" value="<?php echo e($user['username'] ?? ''); ?>" <?php echo $user ? 'readonly' : 'required'; ?>>
            <?php if ($user): ?><small class="muted">账号创建后不可修改</small><?php endif; ?>
        </div>
        <div class="field">
            <label>姓名 / 昵称</label>
            <input name="name" value="<?php echo e($user['name'] ?? ''); ?>">
        </div>
        <div class="field">
            <label>主角色（后台）</label>
            <select name="role">
                <option value="super_admin" <?php echo ($user['role'] ?? '') === 'super_admin' ? 'selected' : ''; ?>>超级管理员（全部权限 + 用户管理）</option>
                <option value="admin" <?php echo ($user['role'] ?? '') === 'admin' ? 'selected' : ''; ?>>管理员（内容管理）</option>
                <option value="user" <?php echo ($user['role'] ?? '') === 'user' ? 'selected' : ''; ?>>用户（受限后台权限）</option>
                <option value="none" <?php echo ($user['role'] ?? '') === 'none' ? 'selected' : ''; ?>>无（无后台权限，仅作为 CRM / PSI 子系统账号）</option>
            </select>
            <small class="muted">「无」用于纯子系统账号：该账号只通过下方 CRM / PSI 角色进入对应系统，不显示任何后台模块，角色分配更清晰。</small>
        </div>
        <div class="field">
            <label>CRM 系统角色</label>
            <select name="crm_role">
                <option value="none" <?php echo ($user['crm_role'] ?? 'none') === 'none' ? 'selected' : ''; ?>>无（不可进入 CRM）</option>
                <option value="user" <?php echo ($user['crm_role'] ?? 'none') === 'user' ? 'selected' : ''; ?>>用户（仅查看 CRM）</option>
                <option value="admin" <?php echo ($user['crm_role'] ?? 'none') === 'admin' ? 'selected' : ''; ?>>管理员（可增删改 CRM）</option>
            </select>
        </div>
        <div class="field">
            <label>PSI 系统角色</label>
            <select name="psi_role">
                <option value="none" <?php echo ($user['psi_role'] ?? 'none') === 'none' ? 'selected' : ''; ?>>无（不可进入 PSI）</option>
                <option value="user" <?php echo ($user['psi_role'] ?? 'none') === 'user' ? 'selected' : ''; ?>>用户（仅查看 PSI）</option>
                <option value="admin" <?php echo ($user['psi_role'] ?? 'none') === 'admin' ? 'selected' : ''; ?>>管理员（可增删改 PSI）</option>
            </select>
        </div>
        <?php if ($showCrm): ?>
        <div class="field">
            <label>CRM 页面权限（勾选可进入的页面，仪表盘始终可见）</label>
            <div class="chk-grid">
                <?php foreach ($crmPages as $k => $l): ?>
                    <label class="chk"><input type="checkbox" name="crm_pages[]" value="<?php echo $k; ?>" <?php echo !empty($crmPerms[$k]) ? 'checked' : ''; ?>> <?php echo $l; ?></label>
                <?php endforeach; ?>
            </div>
            <small class="muted">未勾选的页面将在侧边栏隐藏，且直接访问会被拦截。</small>
        </div>
        <?php endif; ?>
        <?php if ($showPsi): ?>
        <div class="field">
            <label>PSI 页面权限（勾选可进入的页面，仪表盘始终可见）</label>
            <div class="chk-grid">
                <?php foreach ($psiPages as $k => $l): ?>
                    <label class="chk"><input type="checkbox" name="psi_pages[]" value="<?php echo $k; ?>" <?php echo !empty($psiPerms[$k]) ? 'checked' : ''; ?>> <?php echo $l; ?></label>
                <?php endforeach; ?>
            </div>
            <small class="muted">未勾选的页面将在侧边栏隐藏，且直接访问会被拦截。</small>
        </div>
        <?php endif; ?>
        <div class="field">
            <label><?php echo $user ? '重置密码（留空则不修改）' : '登录密码'; ?></label>
            <input type="password" name="password" <?php echo $user ? '' : 'required'; ?>>
        </div>
        <div class="field" style="display:flex;align-items:center;gap:8px">
            <input type="checkbox" name="status" value="1" id="st" <?php echo ($user['status'] ?? 1) ? 'checked' : ''; ?>>
            <label for="st" style="margin:0">启用该账号</label>
        </div>
        <div class="form-actions">
            <button class="btn-primary" type="submit">保存</button>
            <a class="btn-ghost" href="<?php echo site_url('admin/users'); ?>">取消</a>
        </div>
    </form>
</div>
