<?php
session_start();
require_once __DIR__ . '/../../MPHX/common.php';
$user = check_user();
if (!$user) {
    header('Location: /user/login.php');
    exit;
}
$page = isset($_GET['page']) ? $_GET['page'] : 'sy';
// 获取站点名称
$site_name = isset($conf['name']) && !empty($conf['name']) ? $conf['name'] : '控制面板';
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo htmlspecialchars($site_name); ?></title>
    <link rel="stylesheet" href="/static/css/all.min.css?v=<?php echo time(); ?>">
    <link rel="stylesheet" href="/templates/modern/assets/css/style.css?v=<?php echo time(); ?>">
</head>
<body>
    <!-- 手机端遮罩层 -->
    <div class="mobile-overlay" id="mobileOverlay"></div>
    
    <!-- 手机端侧边抽屉菜单 -->
    <div class="mobile-sidebar" id="mobileSidebar">
        <div class="mobile-sidebar-header">
            <div class="mobile-brand">
                <i class="fas fa-server"></i>
                <span><?php echo htmlspecialchars($site_name); ?></span>
            </div>
            <button class="mobile-close" id="mobileClose"><i class="fas fa-times"></i></button>
        </div>
        <div class="mobile-menu">
            <a href="?page=sy" class="mobile-menu-item <?php echo $page=='sy'?'active':''; ?>">
                <i class="fas fa-home"></i> <span>首页</span>
            </a>
            
            <!-- 网站管理分组 -->
            <div class="mobile-menu-group">
                <div class="mobile-menu-toggle-item" onclick="toggleMobileMenuGroup(this)">
                    <i class="fas fa-globe"></i> <span>网站管理</span> <i class="fas fa-chevron-down arrow"></i>
                </div>
                <div class="mobile-submenu">
                    <a href="?page=set&gn=php" class="mobile-submenu-item <?php echo $page=='set' && isset($_GET['gn']) && $_GET['gn']=='php'?'active':''; ?>">
                        <i class="fas fa-code"></i> <span>PHP版本</span>
                    </a>
                    <a href="?page=set&gn=ym" class="mobile-submenu-item <?php echo $page=='set' && isset($_GET['gn']) && $_GET['gn']=='ym'?'active':''; ?>">
                        <i class="fas fa-globe"></i> <span>域名管理</span>
                    </a>
                    <a href="?page=set&gn=mrwd" class="mobile-submenu-item <?php echo $page=='set' && isset($_GET['gn']) && $_GET['gn']=='mrwd'?'active':''; ?>">
                        <i class="fas fa-file"></i> <span>默认文档</span>
                    </a>
                    <a href="?page=set&gn=yxml" class="mobile-submenu-item <?php echo $page=='set' && isset($_GET['gn']) && $_GET['gn']=='yxml'?'active':''; ?>">
                        <i class="fas fa-folder-open"></i> <span>运行目录</span>
                    </a>
                    <a href="?page=set&gn=wjt" class="mobile-submenu-item <?php echo $page=='set' && isset($_GET['gn']) && $_GET['gn']=='wjt'?'active':''; ?>">
                        <i class="fas fa-magic"></i> <span>伪静态</span>
                    </a>
                    <a href="?page=set&gn=zml" class="mobile-submenu-item <?php echo $page=='set' && isset($_GET['gn']) && $_GET['gn']=='zml'?'active':''; ?>">
                        <i class="fas fa-sitemap"></i> <span>子目录绑定</span>
                    </a>
                </div>
            </div>
            
            <!-- 安全管理分组 -->
            <div class="mobile-menu-group">
                <div class="mobile-menu-toggle-item" onclick="toggleMobileMenuGroup(this)">
                    <i class="fas fa-shield-alt"></i> <span>安全管理</span> <i class="fas fa-chevron-down arrow"></i>
                </div>
                <div class="mobile-submenu">
                    <a href="?page=set&gn=ssl" class="mobile-submenu-item <?php echo $page=='set' && isset($_GET['gn']) && $_GET['gn']=='ssl'?'active':''; ?>">
                        <i class="fas fa-lock"></i> <span>SSL证书</span>
                    </a>
                    <a href="?page=set&gn=fdl" class="mobile-submenu-item <?php echo $page=='set' && isset($_GET['gn']) && $_GET['gn']=='fdl'?'active':''; ?>">
                        <i class="fas fa-link"></i> <span>防盗链</span>
                    </a>
                    <a href="?page=set&gn=pass" class="mobile-submenu-item <?php echo $page=='set' && isset($_GET['gn']) && $_GET['gn']=='pass'?'active':''; ?>">
                        <i class="fas fa-key"></i> <span>访问密码</span>
                    </a>
                </div>
            </div>
            
            <!-- 数据库分组 -->
            <div class="mobile-menu-group">
                <div class="mobile-menu-toggle-item" onclick="toggleMobileMenuGroup(this)">
                    <i class="fas fa-database"></i> <span>数据库</span> <i class="fas fa-chevron-down arrow"></i>
                </div>
                <div class="mobile-submenu">
                    <a href="?page=sql" class="mobile-submenu-item <?php echo $page=='sql'?'active':''; ?>">
                        <i class="fas fa-database"></i> <span>数据库管理</span>
                    </a>
                    <a href="?page=sqlqx" class="mobile-submenu-item <?php echo $page=='sqlqx'?'active':''; ?>">
                        <i class="fas fa-user-lock"></i> <span>权限管理</span>
                    </a>
                </div>
            </div>
            
            <!-- 文件管理分组 -->
            <div class="mobile-menu-group">
                <div class="mobile-menu-toggle-item" onclick="toggleMobileMenuGroup(this)">
                    <i class="fas fa-folder"></i> <span>文件管理</span> <i class="fas fa-chevron-down arrow"></i>
                </div>
                <div class="mobile-submenu">
                    <a href="?page=file" class="mobile-submenu-item <?php echo $page=='file'?'active':''; ?>">
                        <i class="fas fa-folder"></i> <span>文件管理</span>
                    </a>
                    <a href="?page=ftp_settings" class="mobile-submenu-item <?php echo $page=='ftp_settings'?'active':''; ?>">
                        <i class="fas fa-exchange-alt"></i> <span>FTP管理</span>
                    </a>
                </div>
            </div>
            
            <!-- 备份管理 -->
            <a href="?page=backup" class="mobile-menu-item <?php echo $page=='backup'?'active':''; ?>">
                <i class="fas fa-archive"></i> <span>备份管理</span>
            </a>
            
            <!-- 系统监控分组 -->
            <div class="mobile-menu-group">
                <div class="mobile-menu-toggle-item" onclick="toggleMobileMenuGroup(this)">
                    <i class="fas fa-chart-line"></i> <span>系统监控</span> <i class="fas fa-chevron-down arrow"></i>
                </div>
                <div class="mobile-submenu">
                    <a href="?page=traffic" class="mobile-submenu-item <?php echo $page=='traffic'?'active':''; ?>">
                        <i class="fas fa-chart-line"></i> <span>流量统计</span>
                    </a>
                    <a href="?page=cron" class="mobile-submenu-item <?php echo $page=='cron'?'active':''; ?>">
                        <i class="fas fa-clock"></i> <span>定时任务</span>
                    </a>
                </div>
            </div>
            
            <!-- 一键部署分组 -->
            <div class="mobile-menu-group">
                <div class="mobile-menu-toggle-item" onclick="toggleMobileMenuGroup(this)">
                    <i class="fas fa-rocket"></i> <span>一键部署</span> <i class="fas fa-chevron-down arrow"></i>
                </div>
                <div class="mobile-submenu">
                    <a href="?page=deploy" class="mobile-submenu-item <?php echo $page=='deploy'?'active':''; ?>">
                        <i class="fas fa-rocket"></i> <span>程序部署</span>
                    </a>
                    <a href="?page=deploy_logs" class="mobile-submenu-item <?php echo $page=='deploy_logs'?'active':''; ?>">
                        <i class="fas fa-history"></i> <span>部署历史</span>
                    </a>
                </div>
            </div>
            
            <!-- APP管理分组 -->
            <div class="mobile-menu-group">
                <div class="mobile-menu-toggle-item" onclick="toggleMobileMenuGroup(this)">
                    <i class="fas fa-mobile-alt"></i> <span>APP管理</span> <i class="fas fa-chevron-down arrow"></i>
                </div>
                <div class="mobile-submenu">
                    <a href="?page=app_access" class="mobile-submenu-item <?php echo $page=='app_access'?'active':''; ?>">
                        <i class="fas fa-qrcode"></i> <span>APP访问授权</span>
                    </a>
                    <a href="/app-download.php" class="mobile-submenu-item" target="_blank">
                        <i class="fas fa-download"></i> <span>APP下载</span>
                    </a>
                </div>
            </div>

            <!-- 个人中心分组 -->
            <div class="mobile-menu-group">
                <div class="mobile-menu-toggle-item" onclick="toggleMobileMenuGroup(this)">
                    <i class="fas fa-user"></i> <span>个人中心</span> <i class="fas fa-chevron-down arrow"></i>
                </div>
                <div class="mobile-submenu">
                    <a href="?page=profile" class="mobile-submenu-item <?php echo $page=='profile'?'active':''; ?>">
                        <i class="fas fa-user"></i> <span>个人资料</span>
                    </a>
                    <a href="?page=logs" class="mobile-submenu-item <?php echo $page=='logs'?'active':''; ?>">
                        <i class="fas fa-history"></i> <span>操作日志</span>
                    </a>
                    <a href="?page=notifications" class="mobile-submenu-item <?php echo $page=='notifications'?'active':''; ?>">
                        <i class="fas fa-bell"></i> <span>通知公告</span>
                    </a>
                    <a href="?page=notification-settings" class="mobile-submenu-item <?php echo $page=='notification-settings'?'active':''; ?>">
                        <i class="fas fa-cog"></i> <span>通知设置</span>
                    </a>
                    <a href="?page=pay" class="mobile-submenu-item <?php echo $page=='pay'?'active':''; ?>">
                        <i class="fas fa-credit-card"></i> <span>充值中心</span>
                    </a>
                    <a href="?page=orders" class="mobile-submenu-item <?php echo $page=='orders'?'active':''; ?>">
                        <i class="fas fa-shopping-cart"></i> <span>订单历史</span>
                    </a>
                </div>
            </div>
            
            <div class="mobile-menu-divider"></div>
            <a href="/user/login.php?logout=1" class="mobile-menu-item mobile-logout">
                <i class="fas fa-sign-out-alt"></i> <span>退出登录</span>
            </a>
        </div>
    </div>
    
    <nav class="navbar">
        <!-- 手机端汉堡菜单按钮 -->
        <button class="mobile-menu-toggle" id="mobileMenuToggle">
            <i class="fas fa-bars"></i>
        </button>
        
        <div class="nav-brand">
            <i class="fas fa-server"></i>
            <span><?php echo htmlspecialchars($site_name); ?></span>
        </div>
        <div class="nav-menu">
            <!-- 首页 -->
            <a href="?page=sy" class="nav-link <?php echo $page=='sy'?'active':''; ?>">
                <i class="fas fa-home"></i> 首页
            </a>
            
            <!-- 网站下拉菜单 -->
            <div class="nav-dropdown" onclick="toggleDropdown(this)">
                <span class="nav-link <?php echo $page=='set'?'active':''; ?>">
                    <i class="fas fa-globe"></i> 网站 <i class="fas fa-chevron-down dropdown-icon"></i>
                </span>
                <div class="dropdown-menu" onclick="event.stopPropagation()">
                    <a href="?page=set&gn=php" target="_parent">PHP版本</a>
                    <a href="?page=set&gn=ym" target="_parent">域名管理</a>
                    <a href="?page=set&gn=mrwd" target="_parent">默认文档</a>
                    <a href="?page=set&gn=yxml" target="_parent">运行目录</a>
                    <a href="?page=set&gn=wjt" target="_parent">伪静态</a>
                    <a href="?page=set&gn=zml" target="_parent">子目录绑定</a>
                </div>
            </div>

            <!-- 安全管理下拉菜单 -->
            <div class="nav-dropdown" onclick="toggleDropdown(this)">
                <span class="nav-link <?php echo $page=='set' && isset($_GET['gn']) && in_array($_GET['gn'], ['ssl','fdl','pass'])?'active':''; ?>">
                    <i class="fas fa-shield-alt"></i> 安全 <i class="fas fa-chevron-down dropdown-icon"></i>
                </span>
                <div class="dropdown-menu" onclick="event.stopPropagation()">
                    <a href="?page=set&gn=ssl" target="_parent">SSL证书</a>
                    <a href="?page=set&gn=fdl" target="_parent">防盗链</a>
                    <a href="?page=set&gn=pass" target="_parent">访问密码</a>
                </div>
            </div>

            <!-- 数据库下拉菜单 -->
            <div class="nav-dropdown" onclick="toggleDropdown(this)">
                <span class="nav-link <?php echo in_array($page, ['sql','sqlqx'])?'active':''; ?>">
                    <i class="fas fa-database"></i> 数据库 <i class="fas fa-chevron-down dropdown-icon"></i>
                </span>
                <div class="dropdown-menu" onclick="event.stopPropagation()">
                    <a href="?page=sql" target="_parent">数据库管理</a>
                    <a href="?page=sqlqx" target="_parent">权限管理</a>
                </div>
            </div>

            <!-- 文件 -->
            <a href="?page=file" class="nav-link <?php echo $page=='file'?'active':''; ?>">
                <i class="fas fa-folder"></i> 文件
            </a>

            <!-- FTP -->
            <a href="?page=ftp_settings" class="nav-link <?php echo $page=='ftp_settings'?'active':''; ?>">
                <i class="fas fa-exchange-alt"></i> FTP
            </a>

            <!-- 备份 -->
            <a href="?page=backup" class="nav-link <?php echo $page=='backup'?'active':''; ?>">
                <i class="fas fa-archive"></i> 备份
            </a>

            <!-- 监控下拉菜单 -->
            <div class="nav-dropdown" onclick="toggleDropdown(this)">
                <span class="nav-link <?php echo in_array($page, ['traffic','cron'])?'active':''; ?>">
                    <i class="fas fa-chart-line"></i> 监控 <i class="fas fa-chevron-down dropdown-icon"></i>
                </span>
                <div class="dropdown-menu" onclick="event.stopPropagation()">
                    <a href="?page=traffic" target="_parent">流量统计</a>
                    <a href="?page=cron" target="_parent">定时任务</a>
                </div>
            </div>

            <!-- 一键部署下拉菜单 -->
            <div class="nav-dropdown" onclick="toggleDropdown(this)">
                <span class="nav-link <?php echo in_array($page, ['deploy','deploy_logs'])?'active':''; ?>">
                    <i class="fas fa-rocket"></i> 部署 <i class="fas fa-chevron-down dropdown-icon"></i>
                </span>
                <div class="dropdown-menu" onclick="event.stopPropagation()">
                    <a href="?page=deploy" target="_parent">程序部署</a>
                    <a href="?page=deploy_logs" target="_parent">部署历史</a>
                </div>
            </div>

            <!-- APP管理下拉菜单 -->
            <div class="nav-dropdown" onclick="toggleDropdown(this)">
                <span class="nav-link <?php echo $page=='app_access'?'active':''; ?>">
                    <i class="fas fa-mobile-alt"></i> APP <i class="fas fa-chevron-down dropdown-icon"></i>
                </span>
                <div class="dropdown-menu" onclick="event.stopPropagation()">
                    <a href="?page=app_access" target="_parent">访问授权</a>
                    <a href="/app-download.php" target="_blank">APP下载</a>
                </div>
            </div>

            <!-- 我的下拉菜单 -->
            <div class="nav-dropdown" onclick="toggleDropdown(this)">
                <span class="nav-link <?php echo in_array($page, ['profile','logs','notifications','notification-settings','pay','orders'])?'active':''; ?>">
                    <i class="fas fa-user"></i> 我的 <i class="fas fa-chevron-down dropdown-icon"></i>
                </span>
                <div class="dropdown-menu" onclick="event.stopPropagation()">
                    <a href="?page=profile" target="_parent">个人资料</a>
                    <a href="?page=logs" target="_parent">操作日志</a>
                    <a href="?page=notifications" target="_parent">通知公告</a>
                    <a href="?page=notification-settings" target="_parent">通知设置</a>
                    <div class="dropdown-divider"></div>
                    <a href="?page=pay" target="_parent">充值中心</a>
                    <a href="?page=orders" target="_parent">订单历史</a>
                </div>
            </div>
        </div>
        <div class="nav-user">
            <span><?php echo $user['user']; ?></span>
            <a href="/user/login.php?logout=1" title="退出登录"><i class="fas fa-sign-out-alt"></i></a>
        </div>
    </nav>
    
    <main class="main-content">
        <?php if ($page == 'sy'): ?>
            <iframe src="/templates/modern/pages/dashboard.php"></iframe>
        <?php else: ?>
            <iframe src="/user/<?php echo $page; ?>.php<?php echo $page=='set' && isset($_GET['gn']) ? '?gn='.$_GET['gn'] : ''; ?>"></iframe>
        <?php endif; ?>
    </main>
    
    <script>
        // 下拉菜单交互
        function toggleDropdown(el) {
            var isOpen = el.classList.contains('open');
            
            // 关闭所有下拉菜单
            document.querySelectorAll('.nav-dropdown').forEach(function(d) {
                d.classList.remove('open');
            });
            
            // 如果当前没有打开，则打开它
            if (!isOpen) {
                el.classList.add('open');
            }
        }
        
        // 点击页面其他地方关闭下拉菜单
        document.addEventListener('click', function(e) {
            if (!e.target.closest('.nav-dropdown')) {
                document.querySelectorAll('.nav-dropdown').forEach(function(d) {
                    d.classList.remove('open');
                });
            }
        });
        
        // 手机端汉堡菜单抽屉交互
        (function() {
            var menuToggle = document.getElementById('mobileMenuToggle');
            var sidebar = document.getElementById('mobileSidebar');
            var overlay = document.getElementById('mobileOverlay');
            var closeBtn = document.getElementById('mobileClose');
            
            if (!menuToggle || !sidebar || !overlay) return;
            
            function openSidebar() {
                sidebar.classList.add('active');
                overlay.classList.add('active');
                document.body.style.overflow = 'hidden';
            }
            
            function closeSidebar() {
                sidebar.classList.remove('active');
                overlay.classList.remove('active');
                document.body.style.overflow = '';
            }
            
            menuToggle.addEventListener('click', openSidebar);
            closeBtn.addEventListener('click', closeSidebar);
            overlay.addEventListener('click', closeSidebar);
            
            // 点击菜单项后自动关闭抽屉
            sidebar.querySelectorAll('.mobile-menu-item').forEach(function(item) {
                item.addEventListener('click', function() {
                    closeSidebar();
                });
            });
            
            // 点击子菜单项后自动关闭抽屉（排除 target="_blank" 的链接）
            sidebar.querySelectorAll('.mobile-submenu-item').forEach(function(item) {
                item.addEventListener('click', function(e) {
                    // 如果链接是 target="_blank"，不关闭抽屉，让浏览器处理跳转
                    if (item.getAttribute('target') === '_blank') {
                        return;
                    }
                    closeSidebar();
                });
            });
            
            // ESC键关闭抽屉
            document.addEventListener('keydown', function(e) {
                if (e.key === 'Escape' && sidebar.classList.contains('active')) {
                    closeSidebar();
                }
            });
        })();
        
        // 手机端菜单分组折叠
        function toggleMobileMenuGroup(el) {
            var group = el.parentElement;
            var isOpen = group.classList.contains('open');
            
            // 关闭其他分组（手风琴效果）
            document.querySelectorAll('.mobile-menu-group').forEach(function(g) {
                if (g !== group) {
                    g.classList.remove('open');
                }
            });
            
            // 切换当前分组
            group.classList.toggle('open');
        }
    </script>
</body>
</html>
