<?php
/**
 * 分系统后台布局（CRM / 进销存 PSI 共用）
 * 独立框架：左侧导航使用各子系统自身的 $nav，不再复用 admin 全量后台导航，
 * 因此 CRM/PSI 内不会出现「管理后台」仪表盘及 products/news 等后台模块。
 */
use Core\Theme;
$sys     = $_sys ?? 'crm';
$sysName = $sys === 'psi' ? '进销存 PSI' : '客户管理 CRM';
$nav     = \subsys_nav($sys);
$seg     = $_seg ?? '';
$site    = Theme::get('site_name', '酷冰甲 · 降温服');
$brandIc = $sys === 'psi' ? 'package' : 'handshake';
$isAuthed = is_admin() || subsys_user($sys);

// 子系统导航 key -> 图标（避免逐个控制器改造，统一在此映射）
$icMap = [
    'dashboard' => 'dashboard', 'customers' => 'handshake', 'leads' => 'lightbulb',
    'followups' => 'phone', 'contacts' => 'users', 'materials' => 'package',
    'products' => 'shopping-bag', 'suppliers' => 'truck', 'purchases' => 'inbox',
    'sales' => 'cart', 'stock' => 'database', 'orders' => 'receipt', 'users' => 'users', 'admin' => 'shield',
    'sales_orders' => 'shopping-bag', 'purchase_orders' => 'file-text', 'outbounds' => 'package', 'reports' => 'bar-chart',
    'reminders' => 'bell', 'notifications' => 'settings',
];
?>
<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title><?php echo e($sysName); ?> · <?php echo e($site); ?></title>
    <link rel="stylesheet" href="<?php echo asset('css/admin.css'); ?>">
    <link rel="stylesheet" href="<?php echo asset('css/subsys.css'); ?>">
</head>
<body class="admin subsys subsys-<?php echo e($sys); ?>">
<?php if ($isAuthed): ?>
    <aside class="ss-side">
        <a class="ss-brand" href="<?php echo site_url($sys); ?>">
            <span class="ss-brand-ic"><?php echo admin_icon($brandIc, 22); ?></span>
            <span class="ss-brand-text"><b><?php echo e($sysName); ?></b><small><?php echo e($site); ?></small></span>
        </a>
        <nav class="ss-nav">
            <?php foreach ($nav as $it): ?>
                <?php $k = $it['k'] ?? ''; ?>
                <?php if (in_array($k, subsys_pages($sys), true) && !subsys_page_can($sys, $k)) continue; ?>
                <?php $active = ($k === $seg); ?>
                <a href="<?php echo site_url($it['url'] ?? '#'); ?>" class="ss-nav-item<?php echo $active ? ' active' : ''; ?>">
                    <span class="ss-ic"><?php echo admin_icon($icMap[$k] ?? 'file-text', 20); ?></span>
                    <span class="ss-label"><?php echo e($it['label'] ?? ''); ?></span>
                </a>
            <?php endforeach; ?>
        </nav>
        <div class="ss-side-foot">
            <a href="<?php echo site_url(); ?>" target="_blank" class="ss-foot-link"><?php echo admin_icon('globe', 18); ?> 查看前台</a>
        </div>
    </aside>
    <div class="ss-body">
        <header class="ss-top">
            <div class="ss-top-left">
                <button class="menu-toggle" type="button" aria-label="菜单"><?php echo admin_icon('menu', 22); ?></button>
                <span class="ss-crumb"><?php echo e($sysName); ?></span>
            </div>
            <?php if ($sys === 'psi'):
                $unread = \psi_unread_events(); ?>
                <a class="ss-bell" href="<?php echo \Core\App::url('PSI/reminders'); ?>" title="紧急提醒">
                    <?php echo admin_icon('bell', 20) ?: '🔔'; ?>
                    <?php if ($unread > 0): ?><span class="ss-badge"><?php echo $unread > 99 ? '99+' : $unread; ?></span><?php endif; ?>
                </a>
            <?php endif; ?>
            <div class="ss-user">
                <span class="ss-user-name">
                    <span class="ss-avatar"><?php echo admin_icon('user', 15); ?></span>
                    <?php echo e($_SESSION['admin_name'] ?? '操作员'); ?>
                    <span class="role-badge role-<?php echo e(admin_role()); ?>"><?php echo e(admin_role_label(admin_role())); ?></span>
                </span>
                <?php if (in_array(admin_role(), ['super_admin', 'admin'], true)): ?>
                    <a class="btn-soft btn-sm" href="<?php echo site_url('admin'); ?>"><?php echo admin_icon('dashboard', 15); ?> 管理后台</a>
                <?php endif; ?>
                <a class="btn-soft btn-sm" href="<?php echo site_url('admin/password'); ?>"><?php echo admin_icon('key', 15); ?> 密码</a>
                <a class="btn-soft btn-sm" href="<?php echo site_url('admin/logout'); ?>"><?php echo admin_icon('logout', 15); ?> 退出</a>
            </div>
        </header>
        <main class="ss-content">
        <?php if (!empty($_SESSION['flash'])): $f = $_SESSION['flash']; unset($_SESSION['flash']); ?>
            <div class="alert alert-<?php echo $f['type'] === 'ok' ? 'ok' : 'err'; ?>"><?php echo e($f['msg']); ?></div>
        <?php endif; ?>
        <?php echo $content; ?>
        </main>
    </div>
<?php else: ?>
    <div class="admin-login-wrap"><?php echo $content; ?></div>
<?php endif; ?>
<script src="<?php echo asset('js/admin.js'); ?>"></script>
</body>
</html>
