<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5">
    <title>{% block title %}服务监测{% endblock %} - 运行维护平台</title>
    <style>
        :root {
            --primary: #2563eb;
            --primary-hover: #1d4ed8;
            --green: #16a34a;
            --yellow: #d97706;
            --red: #dc2626;
            --gray-50: #f9fafb;
            --gray-100: #f3f4f6;
            --gray-200: #e5e7eb;
            --gray-400: #9ca3af;
            --gray-500: #6b7280;
            --gray-700: #374151;
            --gray-900: #111827;
        }
        * { box-sizing: border-box; margin: 0; padding: 0; }
        body {
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Microsoft YaHei", sans-serif;
            background: #f3f4f6;
            min-height: 100vh;
        }

        /* 侧边栏 */
        .sidebar {
            position: fixed;
            left: 0;
            top: 0;
            width: 200px;
            height: 100vh;
            background: linear-gradient(180deg, #1e293b 0%, #334155 100%);
            z-index: 100;
            transition: transform 0.3s ease;
        }
        .sidebar-brand {
            color: #fff;
            font-size: 18px;
            font-weight: 700;
            padding: 20px;
            border-bottom: 1px solid rgba(255,255,255,0.1);
        }
        .sidebar-nav {
            padding: 16px 0;
        }
        .nav-item {
            display: flex;
            align-items: center;
            gap: 12px;
            padding: 14px 20px;
            color: #94a3b8;
            text-decoration: none;
            font-size: 14px;
            transition: all 0.2s;
        }
        .nav-item:hover {
            background: rgba(255,255,255,0.05);
            color: #fff;
        }
        .nav-item.active {
            background: var(--primary);
            color: #fff;
        }
        .nav-icon {
            font-size: 16px;
            width: 20px;
            text-align: center;
        }

        /* 顶部条 */
        .topbar {
            position: fixed;
            left: 200px;
            top: 0;
            right: 0;
            height: 56px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            display: flex;
            align-items: center;
            justify-content: space-between;
            padding: 0 24px;
            z-index: 99;
        }
        .topbar-left {
            display: flex;
            align-items: center;
            gap: 16px;
        }
        .hamburger {
            display: none;
            background: rgba(255,255,255,0.2);
            border: 1px solid rgba(255,255,255,0.35);
            color: #fff;
            font-size: 18px;
            padding: 8px 12px;
            border-radius: 6px;
            cursor: pointer;
        }
        .topbar-home {
            background: rgba(255,255,255,0.2);
            color: #fff;
            border: 1px solid rgba(255,255,255,0.35);
            padding: 6px 14px;
            border-radius: 8px;
            font-size: 13px;
            font-weight: 600;
            text-decoration: none;
        }
        .topbar-home:hover {
            background: rgba(255,255,255,0.3);
        }
        .topbar-user {
            color: #fff;
            display: flex;
            align-items: center;
            gap: 14px;
            font-size: 14px;
        }
        .btn-logout {
            background: rgba(255,255,255,0.15);
            color: #fff;
            border: 1px solid rgba(255,255,255,0.3);
            padding: 8px 16px;
            border-radius: 8px;
            cursor: pointer;
            font-size: 14px;
        }
        .btn-logout:hover {
            background: rgba(255,255,255,0.25);
        }

        /* 主内容区 */
        .main-content {
            margin-left: 200px;
            padding: 84px 24px 28px;
            min-height: 100vh;
        }

        /* 通用组件样式 */
        .section-head {
            display: flex;
            align-items: center;
            justify-content: space-between;
            margin-bottom: 16px;
        }
        .section-title {
            font-size: 18px;
            font-weight: 700;
            color: var(--gray-900);
            display: flex;
            align-items: center;
            gap: 8px;
        }
        .btn-add {
            background: var(--primary);
            color: #fff;
            border: none;
            padding: 8px 16px;
            border-radius: 8px;
            font-size: 14px;
            font-weight: 600;
            cursor: pointer;
            text-decoration: none;
        }
        .btn-add:hover {
            background: var(--primary-hover);
        }
        .empty {
            text-align: center;
            padding: 40px;
            color: var(--gray-500);
        }
        .footer {
            text-align: center;
            padding: 20px;
            color: var(--gray-500);
            font-size: 12px;
        }

        /* 移动端响应式 */
        @media screen and (max-width: 768px) {
            .sidebar {
                transform: translateX(-200px);
            }
            .sidebar.open {
                transform: translateX(0);
            }
            .topbar {
                left: 0;
                padding: 0 12px;
            }
            .main-content {
                margin-left: 0;
                padding: 72px 12px 24px;
            }
            .hamburger {
                display: block;
            }
            .topbar-home {
                display: none;
            }
            .topbar-user {
                font-size: 13px;
                gap: 8px;
            }
            .topbar-user span {
                max-width: 100px;
                overflow: hidden;
                text-overflow: ellipsis;
                white-space: nowrap;
            }
            .btn-logout {
                padding: 6px 10px;
                font-size: 12px;
            }
            /* 侧边栏菜单项 */
            .nav-item {
                padding: 12px 16px;
                font-size: 13px;
            }
            .sidebar-brand {
                font-size: 16px;
                padding: 16px;
            }
        }
    </style>
    {% block extra_css %}{% endblock %}
</head>
<body>
    <!-- 侧边栏 -->
    <aside class="sidebar" id="sidebar">
        <div class="sidebar-brand">🛠️ 运维</div>
        <nav class="sidebar-nav">
            <a href="/service-monitor" class="nav-item {{ 'active' if active_menu == 'targets' }}">
                <span class="nav-icon">📊</span>
                <span>监测目标</span>
            </a>
            <a href="/service-monitor/reports" class="nav-item {{ 'active' if active_menu == 'reports' }}">
                <span class="nav-icon">📋</span>
                <span>巡检报告</span>
            </a>
            <a href="/service-monitor/statistics" class="nav-item {{ 'active' if active_menu == 'statistics' }}">
                <span class="nav-icon">📈</span>
                <span>监测统计</span>
            </a>
            <a href="/service-monitor/schedule" class="nav-item {{ 'active' if active_menu == 'schedule' }}">
                <span class="nav-icon">⏰</span>
                <span>定时任务</span>
            </a>
            {% if is_admin %}
            <a href="/service-monitor/notification" class="nav-item {{ 'active' if active_menu == 'notification' }}">
                <span class="nav-icon">🔔</span>
                <span>通知配置</span>
            </a>
            <a href="/service-monitor/targets" class="nav-item {{ 'active' if active_menu == 'manage' }}">
                <span class="nav-icon">⚙️</span>
                <span>目标管理</span>
            </a>
            {% endif %}
        </nav>
    </aside>

    <!-- 顶部条 -->
    <header class="topbar">
        <div class="topbar-left">
            <button class="hamburger" id="hamburger" onclick="toggleSidebar()">☰</button>
            {% if token_auth %}
            <a href="/login?next={{ request.url | urlencode }}" class="topbar-home">🏠 返回首页</a>
            {% else %}
            <a href="/" class="topbar-home">🏠 返回首页</a>
            {% endif %}
        </div>
        <div class="topbar-user">
            {% if token_auth %}
            <span>👤 访客</span>
            <a href="/login?next={{ request.url | urlencode }}" class="btn-logout" style="text-decoration:none;">🔑 登录</a>
            {% else %}
            <span>{{ user.username }}（{{ '管理员' if is_admin else '用户' }}）</span>
            <button class="btn-logout" onclick="handleLogout()">🚪 退出</button>
            {% endif %}
        </div>
    </header>

    <!-- 主内容区 -->
    <main class="main-content">
        {% block content %}{% endblock %}
    </main>

    {% if not no_footer %}
    <div class="footer">运行维护平台 · 服务监测模块</div>
    {% endif %}

    <script>
        function toggleSidebar() {
            document.getElementById('sidebar').classList.toggle('open');
        }

        // 点击遮罩关闭侧边栏
        document.addEventListener('click', function(e) {
            const sidebar = document.getElementById('sidebar');
            const hamburger = document.getElementById('hamburger');
            if (sidebar.classList.contains('open') &&
                !sidebar.contains(e.target) &&
                !hamburger.contains(e.target)) {
                sidebar.classList.remove('open');
            }
        });

        async function handleLogout() {
            try {
                await fetch('/logout', {
                    method: 'POST',
                    headers: {'Content-Type': 'application/json'},
                    credentials: 'include'
                });
            } catch (e) {}
            window.location.href = '/login';
        }

        {% if token_auth %}
        // 访客模式：点击导航菜单跳转登录页
        (function() {
            var loginUrl = '/login?next=' + encodeURIComponent(window.location.href);
            document.querySelectorAll('.sidebar-nav .nav-item').forEach(function(link) {
                link.addEventListener('click', function(e) {
                    e.preventDefault();
                    if (confirm('查看更多功能请先登录，是否前往登录页？')) {
                        window.location.href = loginUrl;
                    }
                });
            });
        })();
        {% endif %}
    </script>
    {% block extra_js %}{% endblock %}
</body>
</html>