<?php
require_once __DIR__ . '/../MPHX/common.php';

if (!is_admin_login()) {
    header('Location: /admin/login.php');
    exit;
}

$admin = is_admin_login();

// 获取page参数，用于从外部页面返回时加载指定页面
$default_page = isset($_GET['page']) ? htmlspecialchars($_GET['page']) : 'sy.php';
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>后台管理 - MLEOE宝塔系统</title>
    <link rel="stylesheet" href="/static/css/bootstrap.min.css?v=2026040417">
    <link rel="stylesheet" href="/static/css/all.min.css?v=2026040417">
    <link rel="stylesheet" href="css/style.css?v=2026040501">
</head>
<body>
    <div class="sidebar">
        <div class="brand">
            <i class="fas fa-server"></i> <span>MLEOE宝塔系统</span>
        </div>
        <div class="menu">
            <div class="menu-item active" onclick="loadPage('sy.php')">
                <i class="fas fa-home"></i> <span>后台首页</span>
            </div>
            <div class="menu-item" onclick="toggleSubmenu(this, 'basic')">
                <i class="fas fa-cogs"></i> <span>基础配置</span> <i class="fas fa-chevron-down arrow"></i>
            </div>
            <div class="submenu" id="basic">
                <div class="submenu-item" onclick="loadPage('set.php?gn=kzmb')">网站设置</div>
                <div class="submenu-item" onclick="loadPage('set.php?gn=theme')">主题模板</div>
                <div class="submenu-item" onclick="loadPage('set.php?gn=mail')">邮箱设置</div>
                <div class="submenu-item" onclick="loadPage('set.php?gn=yzf')">支付设置</div>
                <div class="submenu-item" onclick="loadPage('set.php?gn=gl')">账号安全</div>
                <div class="submenu-item" onclick="loadPage('default_pages.php')">默认页面</div>
            </div>
            <div class="menu-item" onclick="toggleSubmenu(this, 'system')">
                <i class="fas fa-cog"></i> <span>系统配置</span> <i class="fas fa-chevron-down arrow"></i>
            </div>
            <div class="submenu" id="system">
                <div class="submenu-item" onclick="loadPage('set.php?gn=api')">API设置</div>
                <div class="submenu-item" onclick="loadPage('set.php?gn=parse_domain')">解析域名</div>
                <div class="submenu-item" onclick="loadPage('set_auth.php')">授权配置</div>
                <div class="submenu-item" onclick="loadPage('update.php')">系统更新</div>
                <div class="submenu-item" onclick="loadPage('author.php')">作者声明</div>
            </div>
            <div class="menu-item" onclick="toggleSubmenu(this, 'ops')">
                <i class="fas fa-tools"></i> <span>运维管理</span> <i class="fas fa-chevron-down arrow"></i>
            </div>
            <div class="submenu" id="ops">
                <div class="submenu-item" onclick="loadPage('tutorial.php')">使用教程</div>
                <div class="submenu-item" onclick="loadPage('monitor.php')">系统监控</div>
                <div class="submenu-item" onclick="loadPage('expire_manage.php')">到期管理</div>
            </div>
            <div class="menu-item" onclick="toggleSubmenu(this, 'message')">
                <i class="fas fa-bullhorn"></i> <span>消息管理</span> <i class="fas fa-chevron-down arrow"></i>
            </div>
            <div class="submenu" id="message">
                <div class="submenu-item" onclick="loadPage('announcement.php')">公告管理</div>
                <div class="submenu-item" onclick="loadPage('chat_manage.php')">官网聊天</div>
                <div class="submenu-item" onclick="loadPage('submit_feedback.php')">提交反馈</div>
            </div>

            <div class="menu-item" onclick="toggleSubmenu(this, 'bt')">
                <i class="fas fa-network-wired"></i> <span>宝塔管理</span> <i class="fas fa-chevron-down arrow"></i>
            </div>
            <div class="submenu" id="bt">
                <div class="submenu-item" onclick="loadPage('list.php?gn=bt')">宝塔列表</div>
                <div class="submenu-item" onclick="loadPage('add.php?gn=bt')">添加宝塔</div>
                <div class="submenu-item" onclick="loadPage('bt_monitor.php')">宝塔监控</div>
            </div>
            <div class="menu-item" onclick="toggleSubmenu(this, 'host')">
                <i class="fas fa-laptop"></i> <span>主机管理</span> <i class="fas fa-chevron-down arrow"></i>
            </div>
            <div class="submenu" id="host">
                <div class="submenu-item" onclick="loadPage('list.php?gn=zj')">主机列表</div>
                <div class="submenu-item" onclick="loadPage('add.php?gn=zj')">添加主机</div>
            </div>
            <div class="menu-item" onclick="toggleSubmenu(this, 'deploy')">
                <i class="fas fa-rocket"></i> <span>一键部署</span> <i class="fas fa-chevron-down arrow"></i>
            </div>
            <div class="submenu" id="deploy">
                <div class="submenu-item" onclick="loadPage('list.php?gn=dd')">订单列表</div>
                <div class="submenu-item" onclick="loadPage('list.php?gn=cx')">程序列表</div>
                <div class="submenu-item" onclick="loadPage('add.php?gn=cx')">添加程序</div>
                <div class="submenu-item" onclick="loadPage('add.php?gn=dr')">导入程序</div>
                <div class="submenu-item" onclick="loadPage('category.php')">程序分类</div>
                <div class="submenu-item" onclick="loadPage('api_keys.php')">系统同步</div>
            </div>
            <div class="menu-item" onclick="toggleSubmenu(this, 'log')">
                <i class="fas fa-file-alt"></i> <span>操作日志</span> <i class="fas fa-chevron-down arrow"></i>
            </div>
            <div class="submenu" id="log">
                <div class="submenu-item" onclick="loadPage('list.php?gn=admin_log')">管理员日志</div>
                <div class="submenu-item" onclick="loadPage('list.php?gn=user_log')">用户日志</div>
                <div class="submenu-item" onclick="loadPage('list.php?gn=host_log')">开通日志</div>
                <div class="submenu-item" onclick="loadPage('list.php?gn=api_log')">API调用日志</div>
            </div>
        </div>
    </div>
    
    <div class="main-content">
        <div class="header">
            <div class="header-left">
                <div class="sidebar-toggle" id="sidebarToggle" title="点击展开/收缩侧边栏" onclick="toggleSidebar()">
                    <i class="fas fa-bars"></i>
                </div>
                <h4><i class="fas fa-tachometer-alt"></i> 后台管理系统</h4>
            </div>
            <div class="user-info">
                <span><i class="fas fa-user-circle"></i> 管理员: <?php echo $admin; ?></span>
                <a href="ajax.php?egn=login&logout=1" class="btn btn-danger btn-sm">
                    <i class="fas fa-sign-out-alt"></i> 退出登录
                </a>
            </div>
        </div>
        <div class="content-area" style="flex: 1; display: flex; flex-direction: column; overflow: hidden;">
            <div class="card" style="padding: 0; overflow: hidden; flex: 1; display: flex; flex-direction: column;">
                <iframe id="mainFrame" src="<?php echo $default_page; ?>" style="width: 100%; border: none; flex: 1; min-height: 0;"></iframe>
            </div>
        </div>
    </div>
    
    <script src="/static/js/jquery.min.js"></script>
    <script>
    function toggleSubmenu(element, id) {
        element.classList.toggle('open');
        $('#'+id).toggleClass('open');
    }
    
    function loadPage(url) {
        var timestamp = new Date().getTime();
        var separator = url.indexOf('?') > -1 ? '&' : '?';
        $('#mainFrame').attr('src', url + separator + '_t=' + timestamp);
    }
    
    // 侧边栏响应式逻辑 - 全局变量和函数
    var sidebar, overlay;
    
    function toggleSidebar() {
        var isMobile = window.innerWidth <= 768;
        
        if (isMobile) {
            // 手机端：切换 active 类
            var isActive = sidebar.classList.toggle('active');
            if (isActive) {
                overlay.style.display = 'block';
                setTimeout(function() { overlay.style.opacity = 1; }, 10);
            } else {
                overlay.style.opacity = 0;
                setTimeout(function() { overlay.style.display = 'none'; }, 300);
            }
        } else {
            // PC/平板端：切换 sidebar-collapsed 类
            document.body.classList.toggle('sidebar-collapsed');
            var isCollapsed = document.body.classList.contains('sidebar-collapsed');
            localStorage.setItem('adminSidebarCollapsed', isCollapsed);
        }
    }
    
    $(document).ready(function() {
        $('.menu-item').on('click', function() {
            if ($(this).attr('onclick') && !$(this).attr('onclick').includes('toggleSubmenu')) {
                $('.menu-item').removeClass('active');
                $(this).addClass('active');
            }
        });
        
        // 初始化侧边栏元素
        sidebar = document.querySelector('.sidebar');
        
        // 创建遮罩层
        overlay = document.createElement('div');
        overlay.className = 'sidebar-overlay';
        overlay.style.cssText = 'position:fixed;top:0;left:0;right:0;bottom:0;background:rgba(0,0,0,0.5);z-index:999;display:none;opacity:0;transition:opacity 0.3s;';
        document.body.appendChild(overlay);

        overlay.addEventListener('click', function() {
            sidebar.classList.remove('active');
            overlay.style.opacity = 0;
            setTimeout(function() { overlay.style.display = 'none'; }, 300);
        });
        
        // 恢复状态（仅非手机端）
        if (window.innerWidth > 768) {
            var savedCollapsed = localStorage.getItem('adminSidebarCollapsed');
            if (savedCollapsed === 'true') {
                document.body.classList.add('sidebar-collapsed');
            }
        }
    });
    </script>
</body>
</html>
