<!DOCTYPE html>
<html lang="zh-TW">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>工作節點監控 - {{ node_id }}</title>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=SF+Mono:wght@400;500&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        /* 基礎樣式 */
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        :root {
            --primary-color: #64748b;
            --secondary-color: #475569;
            --accent-color: #374151;
            --success-color: #059669;
            --warning-color: #d97706;
            --error-color: #dc2626;
            --background-color: #f8fafc;
            --card-background: #ffffff;
            --card-background-light: #f8fafc;
            --text-primary: #1e293b;
            --text-secondary: #64748b;
            --text-muted: #94a3b8;
            --border-color: #e2e8f0;
            --border-light: #f1f5f9;
            --shadow-xs: 0 1px 2px 0 rgb(0 0 0 / 0.05);
            --shadow-sm: 0 1px 3px 0 rgb(0 0 0 / 0.1), 0 1px 2px -1px rgb(0 0 0 / 0.1);
            --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
            --shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1);
            --gradient-subtle: linear-gradient(135deg, #f8fafc 0%, #f1f5f9 100%);
            --border-radius: 12px;
            --border-radius-sm: 8px;
        }

        body {
            font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
            background: var(--background-color);
            color: var(--text-primary);
            line-height: 1.6;
            margin: 0;
            padding: 24px;
            min-height: 100vh;
        }

        .container {
            max-width: 1400px;
            margin: 0 auto;
            background: var(--card-background);
            border-radius: var(--border-radius);
            box-shadow: var(--shadow-lg);
            overflow: hidden;
            border: 1px solid var(--border-color);
        }

        /* 頭部樣式 */
        .header {
            background: var(--gradient-subtle);
            color: var(--text-primary);
            padding: 2rem;
            display: flex;
            justify-content: space-between;
            align-items: center;
            position: relative;
            border-bottom: 1px solid var(--border-color);
        }

        .header h1 {
            font-size: 1.875rem;
            font-weight: 600;
            margin: 0;
            display: flex;
            align-items: center;
            gap: 0.75rem;
            letter-spacing: -0.025em;
        }

        .header h1::before {
            content: '🖥️';
            font-size: 1.25rem;
            opacity: 0.7;
        }

        .header-right {
            display: flex;
            align-items: center;
            gap: 1rem;
        }

        .user-info {
            background: var(--card-background);
            padding: 0.5rem 1rem;
            border-radius: 50px;
            font-weight: 500;
            font-size: 0.875rem;
            border: 1px solid var(--border-color);
            color: var(--text-secondary);
        }

        .logout-btn {
            background: var(--text-primary);
            color: white;
            padding: 0.5rem 1rem;
            border: none;
            border-radius: 50px;
            font-weight: 500;
            text-decoration: none;
            transition: all 0.2s ease;
            font-size: 0.875rem;
        }

        .logout-btn:hover {
            background: var(--primary-color);
            transform: translateY(-1px);
        }

        /* 內容網格 */
        .content-grid {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 1.5rem;
            padding: 2rem;
            background: var(--background-color);
        }

        .content-grid .card:last-child {
            grid-column: 1 / -1;
        }

        /* 卡片樣式 */
        .card {
            background: var(--card-background);
            border-radius: var(--border-radius);
            padding: 1.5rem;
            box-shadow: var(--shadow-sm);
            border: 1px solid var(--border-color);
            transition: all 0.2s ease;
        }

        .card:hover {
            box-shadow: var(--shadow-md);
            border-color: var(--border-color);
        }

        .card h2 {
            color: var(--text-primary);
            margin-bottom: 1.5rem;
            font-size: 1.25rem;
            font-weight: 600;
            display: flex;
            align-items: center;
            gap: 0.5rem;
        }

        .card h2::before {
            content: attr(data-icon);
            font-size: 1rem;
            opacity: 0.7;
        }

        /* 表格樣式 */
        .table {
            width: 100%;
            border-collapse: collapse;
            margin: 0;
            background: var(--card-background);
        }

        .table th,
        .table td {
            padding: 0.875rem;
            text-align: left;
            border-bottom: 1px solid var(--border-light);
            vertical-align: middle;
        }

        .table th {
            background: var(--card-background-light);
            font-weight: 500;
            color: var(--text-secondary);
            font-size: 0.8125rem;
            text-transform: uppercase;
            letter-spacing: 0.05em;
        }

        .table td {
            font-weight: 400;
            color: var(--text-primary);
        }

        .table tr:hover {
            background: var(--card-background-light);
        }

        /* 按鈕樣式 */
        .refresh-btn {
            background: var(--card-background);
            color: var(--text-secondary);
            border: 1px solid var(--border-color);
            padding: 0.5rem 1rem;
            border-radius: 6px;
            cursor: pointer;
            font-size: 0.8125rem;
            font-weight: 500;
            transition: all 0.2s ease;
            display: flex;
            align-items: center;
            gap: 0.5rem;
        }

        .refresh-btn:hover {
            background: var(--text-primary);
            color: white;
            border-color: var(--text-primary);
        }

        /* 日誌樣式 */
        .logs {
            background: var(--text-primary);
            color: #f8fafc;
            padding: 1.5rem;
            border-radius: var(--border-radius-sm);
            max-height: 500px;
            overflow-y: auto;
            font-family: 'SF Mono', 'Monaco', 'Cascadia Code', 'Roboto Mono', Consolas, monospace;
            font-size: 0.8125rem;
            line-height: 1.6;
            border: 1px solid var(--border-color);
            min-height: 300px;
        }

        .log-entry {
            margin-bottom: 0.5rem;
            padding: 0.25rem 0;
            word-wrap: break-word;
            white-space: pre-wrap;
            opacity: 0.9;
        }

        .log-entry:hover {
            opacity: 1;
            background: rgba(255, 255, 255, 0.05);
            border-radius: 4px;
            padding: 0.25rem 0.5rem;
            margin: 0.25rem -0.5rem;
        }

        /* 狀態標籤 */
        .status {
            padding: 0.25rem 0.75rem;
            border-radius: 50px;
            font-weight: 500;
            font-size: 0.75rem;
            text-transform: uppercase;
            letter-spacing: 0.05em;
            display: inline-flex;
            align-items: center;
            gap: 0.25rem;
        }

        .status.idle {
            background: #ecfdf5;
            color: var(--success-color);
            border: 1px solid #a7f3d0;
        }

        .status.running,
        .status.executing {
            background: #eff6ff;
            color: #2563eb;
            border: 1px solid #93c5fd;
        }

        .status.error,
        .status.failed {
            background: #fef2f2;
            color: var(--error-color);
            border: 1px solid #fecaca;
        }

        .status.pending {
            background: #fffbeb;
            color: var(--warning-color);
            border: 1px solid #fed7aa;
        }

        /* 數據可視化增強 */
        .metric-card {
            display: flex;
            align-items: center;
            gap: 1rem;
            padding: 1rem;
            background: var(--card-background-light);
            border-radius: var(--border-radius-sm);
            border: 1px solid var(--border-color);
        }

        .metric-icon {
            font-size: 1.5rem;
            opacity: 0.7;
        }

        .metric-content h3 {
            margin: 0;
            font-size: 1.5rem;
            font-weight: 600;
            color: var(--text-primary);
        }

        .metric-content p {
            margin: 0;
            color: var(--text-secondary);
            font-size: 0.8125rem;
        }

        /* 圖表容器 */
        .chart-container {
            margin-bottom: 1.5rem;
            padding: 1rem;
            background: var(--card-background-light);
            border-radius: var(--border-radius-sm);
            border: 1px solid var(--border-color);
            position: relative;
            height: 250px;
        }

        /* 響應式設計 */
        @media (max-width: 1024px) {
            .content-grid {
                grid-template-columns: 1fr;
                gap: 1rem;
            }

            .content-grid .card:last-child {
                grid-column: 1;
            }
        }

        @media (max-width: 768px) {
            body {
                padding: 16px;
            }

            .header {
                padding: 1.5rem;
                flex-direction: column;
                gap: 1rem;
                text-align: center;
            }

            .header-right {
                flex-direction: row;
                gap: 0.75rem;
            }

            .content-grid {
                padding: 1.5rem;
                gap: 1rem;
            }

            .card {
                padding: 1.25rem;
            }

            .header h1 {
                font-size: 1.5rem;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>工作節點監控面板</h1>
            <div class="header-right">
                <div class="user-info">
                    <i class="fas fa-user"></i>
                    {{ username }}
                </div>
                <a href="/logout" class="logout-btn">
                    <i class="fas fa-sign-out-alt"></i>
                    登出
                </a>
            </div>
        </div>

        <div class="content-grid">
            <!-- 節點狀態卡片 -->
            <div class="card">
                <h2 data-icon=>節點狀態</h2>
                <table class="table">
                    <tr>
                        <th><i class="fas fa-tag"></i> 節點ID</th>
                        <td id="node-id">{{ node_id }}</td>
                    </tr>
                    <tr>
                        <th><i class="fas fa-circle"></i> 狀態</th>
                        <td><span id="task-status" class="status">{{ initial_status }}</span></td>
                    </tr>
                    <tr>
                        <th><i class="fas fa-tasks"></i> 當前任務</th>
                        <td id="task-id">None</td>
                    </tr>
                    <tr>
                        <th><i class="fas fa-coins"></i> CPT 餘額</th>
                        <td id="cpt-balance">0</td>
                    </tr>
                    <tr>
                        <th><i class="fas fa-network-wired"></i> 本機IP</th>
                        <td id="ip-address">檢測中...</td>
                    </tr>
                    <tr>
                        <th><i class="fas fa-microchip"></i> CPU 使用率</th>
                        <td id="cpu-usage">0%</td>
                    </tr>
                    <tr>
                        <th><i class="fas fa-memory"></i> 記憶體使用率</th>
                        <td id="memory-usage">0%</td>
                    </tr>
                </table>
            </div>

            <!-- 系統資源卡片 -->
            <div class="card">
                <div class="card-header">
                    <h2 data-icon=>系統資源</h2>
                </div>
                
                <!-- CPU 使用率 -->
                <div class="metric-card">
                    <div class="metric-icon">⚡</div>
                    <div class="metric-content">
                        <h3 id="cpu-metric">0%</h3>
                        <p>CPU 使用率</p>
                    </div>
                </div>
                
                <!-- 記憶體使用率 -->
                <div class="metric-card" style="margin-top: 1rem;">
                    <div class="metric-icon"></div>
                    <div class="metric-content">
                        <h3 id="memory-metric">0%</h3>
                        <p>記憶體使用率</p>
                    </div>
                </div>
                
                <!-- 圖表區域 -->
                <div class="chart-container">
                    <canvas id="cpuChart"></canvas>
                </div>
                <div class="chart-container">
                    <canvas id="memoryChart"></canvas>
                </div>
            </div>

            <!-- 系統日誌卡片 -->
            <div class="card">
                <div class="card-header">
                    <h2 data-icon=>系統日誌</h2>
                    <button class="refresh-btn" onclick="refreshLogs()">
                        <i class="fas fa-sync-alt"></i>
                        刷新日誌
                    </button>
                </div>
                <div id="logs" class="logs">
                    <div class="text-center" style="opacity: 0.7;">載入日誌中...</div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
    <script src="/static/js/monitor.js"></script>
</body>
</html>