<?php
ob_start();
require_once 'config.php';
require_once __DIR__ . '/includes/functions.php';
require_once __DIR__ . '/includes/LicenseClient.php';

// 检查登录状态
if (!isLoggedIn()) {
    redirect(SITE_URL . 'modules/home/login.php');
}

$user = currentUser();
$isAdmin = ($user['role'] === 'admin');
$isEmployee = ($user['role'] === 'employee');

$page = $_GET['page'] ?? 'dashboard';

// ========== SVG 图标配置 ==========
$svg_icons = [
    'dashboard'       => 'https://api.iconify.design/mdi:view-dashboard.svg?color=currentColor',
    'apps'            => 'https://api.iconify.design/mdi:application.svg?color=currentColor',
    'licenses'        => 'https://api.iconify.design/mdi:key.svg?color=currentColor',
    'invite'          => 'https://api.iconify.design/mdi:account-plus.svg?color=currentColor',
    'buy_app'         => 'https://api.iconify.design/mdi:cart.svg?color=currentColor',
    'buy_group'       => 'https://api.iconify.design/mdi:account-group.svg?color=currentColor',
    'recharge'        => 'https://api.iconify.design/mdi:wallet.svg?color=currentColor',
    'balance_log'     => 'https://api.iconify.design/mdi:format-list-bulleted.svg?color=currentColor',
    'tickets'         => 'https://api.iconify.design/mdi:ticket.svg?color=currentColor',
    'ticket_view'     => 'https://api.iconify.design/mdi:ticket-outline.svg?color=currentColor',
    'login_log'       => 'https://api.iconify.design/mdi:history.svg?color=currentColor',
    'profile'         => 'https://api.iconify.design/mdi:account.svg?color=currentColor',
    'changepwd'       => 'https://api.iconify.design/mdi:lock.svg?color=currentColor',
    'avatar'          => 'https://api.iconify.design/mdi:camera.svg?color=currentColor',
    'version'         => 'https://api.iconify.design/mdi:information.svg?color=currentColor',
    'agent_dashboard' => 'https://api.iconify.design/mdi:handshake.svg?color=currentColor',
    'agent_upload'    => 'https://api.iconify.design/mdi:package-variant.svg?color=currentColor',
    'downloads'       => 'https://api.iconify.design/mdi:download.svg?color=currentColor',
    'agent_apps'      => 'https://api.iconify.design/mdi:apps.svg?color=currentColor',
    'agent_orders'    => 'https://api.iconify.design/mdi:shopping.svg?color=currentColor',
    'agent_withdraw'  => 'https://api.iconify.design/mdi:cash.svg?color=currentColor',
    'agent_sell'      => 'https://api.iconify.design/mdi:link-variant.svg?color=currentColor',
    'apply_agent'     => 'https://api.iconify.design/mdi:file-document.svg?color=currentColor',
    'stats'           => 'https://api.iconify.design/mdi:chart-bar.svg?color=currentColor',
    'card_codes'      => 'https://api.iconify.design/mdi:credit-card.svg?color=currentColor',
    'user_groups'     => 'https://api.iconify.design/mdi:account-group-outline.svg?color=currentColor',
    'users'           => 'https://api.iconify.design/mdi:account-multiple.svg?color=currentColor',
    'user_detail'     => 'https://api.iconify.design/mdi:account-details.svg?color=currentColor',
    'user_balance'    => 'https://api.iconify.design/mdi:cash-multiple.svg?color=currentColor',
    'encrypt_tool'    => 'https://api.iconify.design/mdi:shield-lock.svg?color=currentColor',
    'orders'          => 'https://api.iconify.design/mdi:package.svg?color=currentColor',
    'agent_products'  => 'https://api.iconify.design/mdi:package-check.svg?color=currentColor',
    'agent_applications' => 'https://api.iconify.design/mdi:account-check.svg?color=currentColor',
    'agent_withdrawals'  => 'https://api.iconify.design/mdi:cash-check.svg?color=currentColor',
    'announcements'   => 'https://api.iconify.design/mdi:bullhorn.svg?color=currentColor',
    'links'           => 'https://api.iconify.design/mdi:link.svg?color=currentColor',
    'changelog'       => 'https://api.iconify.design/mdi:note-text.svg?color=currentColor',
    'config'          => 'https://api.iconify.design/mdi:cog.svg?color=currentColor',
    'payments'        => 'https://api.iconify.design/mdi:credit-card-outline.svg?color=currentColor',
    'updates'         => 'https://api.iconify.design/mdi:cloud-download.svg?color=currentColor',
    'update'          => 'https://api.iconify.design/mdi:cloud-download.svg?color=currentColor',
    'home'            => 'https://api.iconify.design/mdi:home.svg?color=currentColor',
    'logout'          => 'https://api.iconify.design/mdi:logout.svg?color=currentColor',
    'default'         => 'https://api.iconify.design/mdi:circle.svg?color=currentColor',
    'celebration'     => 'https://api.iconify.design/mdi:party-popper.svg?color=%23667eea',
    'resources'       => 'https://api.iconify.design/mdi:package-variant.svg?color=%23667eea',
    'login'          => 'https://cs.demo.lhwks.cn/assets/svg/login.svg',
];

$userPages = [
    'dashboard' => '用户中心',
    'apps' => '我的应用',
    'licenses' => '授权码管理',
    'buy_app' => '购买授权',
    'buy_group' => '购买会员',
    'recharge' => '余额充值',
    'balance_log' => '余额明细',
    'tickets' => '工单系统',
    'ticket_view' => '工单详情',
    'profile' => '个人资料',
    'changepwd' => '修改密码',
    'avatar' => '上传头像',
    'version' => '版本信息',
];

$adminPages = [
    'dashboard' => '控制台',
    'stats' => '数据统计',
    'apps' => '应用管理',
    'downloads' => '资源下载(用户用)',
    'resources' => '资源下载(站长用)',
    'card_codes' => '卡密管理',
    'user_groups' => '会员组管理',
    'users' => '用户管理',
    'user_detail' => '用户详情',
    'user_balance' => '用户余额明细',
    'licenses' => '授权管理',
    'orders' => '订单管理',
    'tickets' => '工单管理',
    'ticket_view' => '工单详情',
    'announcements' => '公告管理',
    'links' => '链接管理',
    'quick_login' => '快捷登录',
    'quick_login_config' =>'登录配置',
    'config' => '网站配置',
    'update' => '系统更新',
];

if ($isAdmin) {
    $allowedPages = $adminPages;
} elseif ($isEmployee) {
    $allowedPages = ['dashboard' => '控制台'];
    if (hasPermission('apps.view')) {
        $allowedPages['apps'] = '应用管理';
    }
    if (hasPermission('users.view')) {
        $allowedPages['users'] = '用户管理';
    }
    if (hasPermission('orders.view')) {
        $allowedPages['orders'] = '订单管理';
    }
    if (hasPermission('tickets.handle')) {
        $allowedPages['tickets'] = '工单管理';
    }
} else {
    $allowedPages = $userPages;
}

if (!isset($allowedPages[$page])) $page = 'dashboard';

$pageFile = ($isAdmin || $isEmployee) ? __DIR__ . '/modules/admin/' . $page . '.php' : __DIR__ . '/modules/user/' . $page . '.php';
if (!file_exists($pageFile)) $pageFile = __DIR__ . '/modules/user/dashboard.php';

$siteConfig = getSiteConfig();
$headerLinks = getHeaderLinks();
$friendLinks = getFriendLinks();

function getPageIcon($page) {
    global $svg_icons;
    $iconMap = [
        'dashboard' => 'dashboard', 'apps' => 'apps', 'licenses' => 'licenses',
        'buy_app' => 'buy_app', 'buy_group' => 'buy_group', 'recharge' => 'recharge',
        'balance_log' => 'balance_log', 'tickets' => 'tickets', 'ticket_view' => 'ticket_view',
        'profile' => 'profile', 'changepwd' => 'changepwd', 'avatar' => 'avatar',
        'user_groups' => 'user_groups', 'users' => 'users', 'orders' => 'orders',
        'config' => 'config', 'links' => 'links', 'updates' => 'updates', 'update' => 'update',
        'payments' => 'payments', 'downloads' => 'downloads', 'version' => 'version',
        'changelog' => 'changelog', 'agent_sell' => 'agent_sell',
        'agent_withdrawals' => 'agent_withdrawals', 'user_detail' => 'user_detail',
        'user_balance' => 'user_balance', 'agent_dashboard' => 'agent_dashboard',
        'agent_apps' => 'agent_apps', 'agent_orders' => 'agent_orders',
        'agent_withdraw' => 'agent_withdraw', 'apply_agent' => 'apply_agent',
        'agent_applications' => 'agent_applications', 'announcements' => 'announcements',
        'encrypt_tool' => 'encrypt_tool', 'stats' => 'stats', 'card_codes' => 'card_codes',
        'agent_products' => 'agent_products', 'invite' => 'invite', 'login_log' => 'login_log',
        'resources'=>'resources',
        'quick_login_config'=>'login','quick_login'=>'login',
    ];
    $iconKey = $iconMap[$page] ?? 'default';
    return $svg_icons[$iconKey] ?? $svg_icons['default'];
}

?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
    <title><?php echo htmlspecialchars($allowedPages[$page] ?? '用户中心'); ?> - <?php echo htmlspecialchars($siteConfig['site_name'] ?? '万能授权系统'); ?></title>
    <link rel="stylesheet" href="/assets/css/style.css">
    <link rel="stylesheet" href="/assets/css/dark.css" id="dark-theme" disabled>
    <link rel="icon" href="/assets/uploads/favicon.ico"> 
    <script src="/assets/js/main.js" defer></script>
    <script src="/assets/js/admin.js" defer></script>
    <script src="/assets/js/qrcode.min.js"></script>
    <script src="/assets/js/chart.umd.min.js"></script>
    <script src="/assets/js/jquery.min.js"></script>
    <style>
    /* 侧边栏样式修复 */
    .sidebar {
        padding-top: 0;
        position: fixed;
        left: 0;
        top: 0;
        height: 100vh;
        z-index: 100;
        overflow-y: auto;
    }
    
    .sidebar-nav {
        margin-top: 0;
        padding-top: 20px;
    }
    
    .sidebar-nav .sidebar-link:first-child {
        margin-top: 0;
    }
    
    .sidebar-link .link-icon {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        width: 24px;
        height: 24px;
        margin-right: 12px;
    }
    
    .sidebar-link .link-icon img {
        width: 20px;
        height: 20px;
        filter: brightness(0) saturate(100%);
    }
    
    .sidebar-link.active .link-icon img {
        filter: brightness(0) invert(1);
    }
    
    /* 主内容区域调整 - 为侧边栏留出空间 */
    .main-content {
        margin-left: 260px;
        display: flex;
        flex-direction: column;
        min-height: 100vh;
    }
    
    /* 侧边栏折叠状态 */
    .sidebar.collapsed ~ .main-content {
        margin-left: 80px;
    }
    
    /* 顶部栏调整 */
    .top-bar-left {
        display: flex;
        align-items: center;
        padding: 16px 24px;
        border-bottom: 1px solid var(--border-color);
        background: var(--bg-primary);
        position: sticky;
        top: 0;
        z-index: 50;
    }
    
    .mobile-menu-toggle {
        display: none;
        background: none;
        border: none;
        font-size: 24px;
        cursor: pointer;
        color: var(--text-primary);
        padding: 8px;
        border-radius: var(--radius-sm);
        transition: background 0.2s;
    }
    
    .mobile-menu-toggle:hover {
        background: var(--bg-secondary);
    }
    
    /* 内容区域 */
    .content-area {
        flex: 1;
        padding: 24px;
    }
    
    /* 页脚调整 */
    .footer {
        margin-top: auto;
        padding: 24px;
        border-top: 1px solid var(--border-color);
        background: var(--bg-primary);
    }
    
    /* 移动端适配 */
    @media (max-width: 768px) {
        .main-content {
            margin-left: 0;
        }
        
        .sidebar.collapsed ~ .main-content {
            margin-left: 0;
        }
        
        .sidebar {
            transform: translateX(-100%);
            transition: transform 0.3s ease;
        }
        
        .sidebar.show {
            transform: translateX(0);
        }
        
        .mobile-menu-toggle {
            display: block;
        }
        
        .top-bar-left {
            padding: 12px 16px;
        }
        
        .content-area {
            padding: 16px;
        }
    }
</style>
</head>

<body>
    <div class="app-wrapper">
        <aside class="sidebar" id="sidebar">
            <nav class="sidebar-nav">
                <?php foreach ($allowedPages as $key => $title): ?>
                    <a href="?page=<?php echo $key; ?>" class="sidebar-link <?php echo $page == $key ? 'active' : ''; ?>">
                        <span class="link-icon">
                            <img src="<?php echo getPageIcon($key); ?>" alt="<?php echo $key; ?>">
                        </span>
                        <span class="link-text"><?php echo htmlspecialchars($title); ?></span>
                    </a>
                <?php endforeach ?>
                <a href="/" class="sidebar-link">
                    <span class="link-icon">
                        <img src="<?php echo $svg_icons['home']; ?>" alt="首页">
                    </span>
                    <span class="link-text">返回首页</span>
                </a>
                <a href="/modules/home/logout.php" class="sidebar-link logout-link">
                    <span class="link-icon">
                        <img src="<?php echo $svg_icons['logout']; ?>" alt="退出">
                    </span>
                    <span class="link-text">退出登录</span>
                </a>
            </nav>
        </aside>
        <main class="main-content">
            <div class="top-bar">
    <div class="top-bar-left">
        <button class="mobile-menu-toggle" id="mobileMenuToggle" onclick="document.getElementById('sidebar').classList.toggle('show')">
            <img src="https://api.iconify.design/mdi:menu.svg?color=currentColor" width="24" height="24" alt="菜单">
        </button>
        <a href="/" class="top-bar-logo">
            <img src="<?php echo htmlspecialchars($siteConfig['site_logo'] ?? '/assets/uploads/logo.png'); ?>" alt="Logo">
        </a>
    </div>
    <div class="top-bar-right">
        <div class="user-info-dropdown">
            <button class="user-info-btn" id="userDropdownBtn">
                <img src="<?php echo $svg_icons['profile']; ?>" alt="用户" width="20" height="20">
                <span><?php echo htmlspecialchars($user['username'] ?? '用户'); ?></span>
                <img src="https://api.iconify.design/mdi:chevron-down.svg?color=currentColor" width="16" height="16" class="dropdown-arrow">
            </button>
            <div class="user-dropdown-menu" id="userDropdownMenu" style="display: none;">
                <a href="?page=profile">
                    <img src="<?php echo $svg_icons['profile']; ?>" alt="" width="16" height="16">
                    个人资料
                </a>
                <a href="?page=changepwd">
                    <img src="<?php echo $svg_icons['changepwd']; ?>" alt="" width="16" height="16">
                    修改密码
                </a>
                <hr>
                <a href="/modules/home/logout.php">
                    <img src="<?php echo $svg_icons['logout']; ?>" alt="" width="16" height="16">
                    退出登录
                </a>
            </div>
        </div>
    </div>
</div>
            <div class="content-area">
                <?php include $pageFile; ?>
            </div>
            <footer class="footer">
                <div class="footer-logo">
                    <img src="<?php echo htmlspecialchars($siteConfig['site_logo'] ?? '/assets/uploads/logo.png'); ?>" alt="Logo">
                </div>
                <div class="footer-links">
                    <?php foreach ($friendLinks as $link): ?>
                        <a href="<?php echo htmlspecialchars($link['url']); ?>" target="_blank"><?php echo htmlspecialchars($link['name']); ?></a>
                    <?php endforeach; ?>
                </div>
                <div class="footer-copyright">
                    <?php echo htmlspecialchars($siteConfig['site_copyright'] ?? ''); ?>
                    <?php if (!empty($siteConfig['site_icp'])): ?>
                        <a href="https://beian.miit.gov.cn/" target="_blank"><?php echo htmlspecialchars($siteConfig['site_icp']); ?></a>
                    <?php endif; ?>
                </div>
            </footer>
        </main>
    </div>
<script>
    const sidebar = document.getElementById('sidebar');
    const mobileMenuToggle = document.getElementById('mobileMenuToggle');
    
    const sidebarCollapsed = localStorage.getItem('sidebarCollapsed') === 'true';
    if (sidebarCollapsed && sidebar) {
        sidebar.classList.add('collapsed');
    }
    
    if (mobileMenuToggle) {
        mobileMenuToggle.addEventListener('click', function(e) {
            e.stopPropagation();
            sidebar.classList.toggle('show');
        });
    }
    
    const mainContent = document.querySelector('.main-content');
    if (mainContent) {
        mainContent.addEventListener('click', function() {
            if (window.innerWidth <= 768 && sidebar.classList.contains('show')) {
                sidebar.classList.remove('show');
            }
        });
    }
    
    window.addEventListener('resize', function() {
        if (window.innerWidth > 768 && sidebar.classList.contains('show')) {
            sidebar.classList.remove('show');
        }
    });
</script>

<!-- 版本更新提醒弹窗 -->
<div id="versionUpdateModal" class="version-update-modal" style="display: none;">
    <div class="version-update-content">
        <div class="version-update-header">
            <div class="update-icon">
                <img src="<?php echo $svg_icons['celebration']; ?>" style="width:48px;height:48px;" alt="更新">
            </div>
            <h2>发现新版本</h2>
            <button class="close-btn" id="closeVersionModal">&times;</button>
        </div>
        <div class="version-update-body">
            <div class="version-badge">
                新版本 <span id="newVersion"></span>
            </div>
            <div class="update-date">
                发布日期：<span id="updateDate"></span>
            </div>
            <div class="update-description">
                <h4>更新内容：</h4>
                <div id="releaseNotes" class="release-notes"></div>
            </div>
        </div>
        <div class="version-update-footer">
            <button class="btn btn-secondary" id="remindLaterBtn">稍后提醒</button>
            <button class="btn btn-primary" id="viewChangelogBtn">查看完整更新日志</button>
        </div>
    </div>
</div>

<style>
.top-bar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 24px;
    border-bottom: 1px solid var(--border-color);
    background: var(--bg-primary);
    position: sticky;
    top: 0;
    z-index: 50;
}

.top-bar-logo {
    display: flex;
    align-items: center;
    gap: 10px;
    text-decoration: none;
    margin-left: 16px;
}

.top-bar-logo img {
    height: 32px;
    width: auto;
}

.top-bar-logo span {
    font-size: 16px;
    font-weight: 600;
    color: var(--text-primary);
}

.top-bar-right {
    display: flex;
    align-items: center;
    gap: 16px;
}

.user-info-btn {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: 30px;
    cursor: pointer;
    font-size: 14px;
    color: var(--text-primary);
    transition: all 0.2s;
}

.user-info-btn:hover {
    background: var(--bg-primary);
    border-color: var(--primary-color);
}

.user-dropdown-menu {
    position: absolute;
    top: 100%;
    right: 24px;
    margin-top: 8px;
    background: var(--bg-primary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-md);
    min-width: 180px;
    z-index: 100;
}

.user-dropdown-menu a {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 12px 16px;
    color: var(--text-primary);
    text-decoration: none;
    transition: background 0.2s;
}

.user-dropdown-menu a:hover {
    background: var(--bg-secondary);
}

.user-dropdown-menu hr {
    margin: 4px 0;
    border: none;
    border-top: 1px solid var(--border-color);
}

/* 移动端适配 */
@media (max-width: 768px) {
    .top-bar {
        padding: 8px 16px;
    }
    .top-bar-logo span {
        display: none;
    }
    .user-info-btn span {
        display: none;
    }
    .user-dropdown-menu {
        right: 16px;
    }
}
.version-update-modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0,0,0,0.6);
    backdrop-filter: blur(8px);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 10002;
    animation: fadeIn 0.3s ease;
}
.version-update-content {
    background: var(--bg-primary);
    border-radius: 24px;
    width: 90%;
    max-width: 500px;
    max-height: 80vh;
    overflow-y: auto;
    box-shadow: 0 25px 50px -12px rgba(0,0,0,0.25);
    animation: slideUp 0.4s ease;
}
.version-update-header {
    padding: 24px 24px 16px;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    gap: 12px;
    position: relative;
}
.version-update-header h2 {
    flex: 1;
    margin: 0;
    font-size: 24px;
}
.version-update-header .close-btn {
    background: none;
    border: none;
    font-size: 28px;
    cursor: pointer;
    color: var(--text-muted);
    line-height: 1;
}
.version-update-body {
    padding: 24px;
}
.version-badge {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    display: inline-block;
    padding: 6px 16px;
    border-radius: 30px;
    font-size: 14px;
    font-weight: 600;
    margin-bottom: 16px;
}
.update-date {
    color: var(--text-muted);
    font-size: 13px;
    margin-bottom: 20px;
}
.update-description h4 {
    margin-bottom: 12px;
    font-size: 16px;
}
.release-notes {
    background: var(--bg-secondary);
    border-radius: 12px;
    padding: 16px;
    font-size: 14px;
    line-height: 1.6;
    color: var(--text-secondary);
    white-space: pre-wrap;
}
.version-update-footer {
    padding: 16px 24px 24px;
    display: flex;
    gap: 12px;
    justify-content: flex-end;
    border-top: 1px solid var(--border-color);
}
@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}
@keyframes slideUp {
    from { transform: translateY(30px); opacity: 0; }
    to { transform: translateY(0); opacity: 1; }
}
</style>

<script>
function checkVersionUpdate() {
    const userRole = '<?php echo $user['role'] ?? 'user'; ?>';
    const currentVersion = '<?php echo SYS_VERSION; ?>';
    
    if (userRole === 'admin') {
        return;
    }
    
    fetch('/api/get_latest_version.php')
        .then(res => res.json())
        .then(data => {
            if (data.success && data.version !== currentVersion) {
                fetch(`/api/check_version_reminded.php?version=${data.version}`)
                    .then(res => res.json())
                    .then(remindData => {
                        if (!remindData.reminded) {
                            showVersionModal(data);
                        }
                    });
            }
        })
        .catch(err => console.error('检查版本更新失败:', err));
}

function showVersionModal(versionData) {
    const userRole = '<?php echo $user['role'] ?? 'user'; ?>';
    if (userRole === 'admin') return;
    
    const modal = document.getElementById('versionUpdateModal');
    if (!modal) return;
    
    document.getElementById('newVersion').innerText = versionData.version;
    document.getElementById('updateDate').innerText = versionData.created_at || '刚刚';
    
    let notes = versionData.release_notes || '暂无更新说明';
    notes = notes.replace(/\n/g, '<br>');
    document.getElementById('releaseNotes').innerHTML = notes;
    
    modal.style.display = 'flex';
    
    fetch('/api/mark_version_reminded.php', {
        method: 'POST',
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
        body: `version=${versionData.version}`
    });
}

document.getElementById('closeVersionModal')?.addEventListener('click', function() {
    document.getElementById('versionUpdateModal').style.display = 'none';
});

document.getElementById('remindLaterBtn')?.addEventListener('click', function() {
    document.getElementById('versionUpdateModal').style.display = 'none';
});

document.getElementById('viewChangelogBtn')?.addEventListener('click', function() {
    window.location.href = '?page=version';
});

document.getElementById('versionUpdateModal')?.addEventListener('click', function(e) {
    if (e.target === this) {
        this.style.display = 'none';
    }
});

document.addEventListener('DOMContentLoaded', function() {
    checkVersionUpdate();
});
</script>

<script>
// 自动检查更新（每天提示一次）- 使用 SVG 图标替代 Emoji
$(document).ready(function() {
    var lastCheck = localStorage.getItem('update_check_date');
    var today = new Date().toDateString();
    
    if (lastCheck !== today) {
        $.ajax({
            url: '/update_api/check_update.php',
            method: 'GET',
            dataType: 'json',
            success: function(res) {
                if (res.has_update) {
                    localStorage.setItem('update_check_date', today);
                    
                    if (res.is_force) {
                        alert('强制更新：' + res.version + '，请立即更新！');
                        window.location.href = 'admin.php?page=update';
                    } else {
                        if (confirm('发现新版本 ' + res.version + '\n\n' + (res.title || '') + '\n' + (res.description || '') + '\n\n是否立即更新？')) {
                            window.location.href = 'admin.php?page=update';
                        }
                    }
                }
            }
        });
    }
});
// 用户下拉菜单
const userDropdownBtn = document.getElementById('userDropdownBtn');
const userDropdownMenu = document.getElementById('userDropdownMenu');

if (userDropdownBtn && userDropdownMenu) {
    userDropdownBtn.addEventListener('click', function(e) {
        e.stopPropagation();
        userDropdownMenu.style.display = userDropdownMenu.style.display === 'none' ? 'block' : 'none';
    });
    
    document.addEventListener('click', function() {
        userDropdownMenu.style.display = 'none';
    });
    
    userDropdownMenu.addEventListener('click', function(e) {
        e.stopPropagation();
    });
}
</script>
</body>
</html>