<!DOCTYPE html>
<html lang="zh-CN" id="html-root">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{{ title }}</title>
    <link rel="stylesheet" href="/static/css/styles.css">
    <style>
        :root {
            /* 深色主題顏色變數 */
            --bg-primary: #1e1e1e;
            --bg-secondary: #2d2d30;
            --bg-tertiary: #252526;
            --surface-color: #333333;
            --text-primary: #cccccc;
            --text-secondary: #9e9e9e;
            --accent-color: #007acc;
            --accent-hover: #005a9e;
            --border-color: #464647;
            --success-color: #4caf50;
            --warning-color: #ff9800;
            --error-color: #f44336;
            --info-color: #2196f3;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: var(--bg-primary);
            color: var(--text-primary);
            line-height: 1.6;
            min-height: 100vh;
            display: flex;
            flex-direction: column;
        }

        /* 等待會話時的樣式 */
        .waiting-container {
            display: flex;
            align-items: center;
            justify-content: center;
            min-height: 100vh;
            text-align: center;
        }

        .waiting-content {
            max-width: 600px;
            padding: 40px;
            background: var(--bg-secondary);
            border-radius: 12px;
            box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
        }

        .waiting-title {
            font-size: 2.5em;
            font-weight: bold;
            color: var(--accent-color);
            margin-bottom: 20px;
        }

        .waiting-description {
            color: var(--text-secondary);
            font-size: 1.2em;
            margin-bottom: 30px;
        }

        .waiting-status {
            padding: 20px;
            background: rgba(0, 122, 204, 0.1);
            border: 1px solid var(--accent-color);
            border-radius: 8px;
            color: var(--accent-color);
        }

        /* 連接狀態指示器 */
        .connection-status {
            position: fixed;
            top: 20px;
            right: 20px;
            z-index: 1000;
            display: flex;
            align-items: center;
            gap: 8px;
            padding: 8px 12px;
            background: var(--bg-secondary);
            border: 1px solid var(--border-color);
            border-radius: 6px;
            font-size: 12px;
            font-weight: 500;
        }

        .connection-indicator {
            display: inline-block;
            width: 8px;
            height: 8px;
            border-radius: 50%;
            background: var(--error-color);
            transition: all 0.3s ease;
        }

        .connection-indicator.connected {
            background: var(--success-color);
            box-shadow: 0 0 8px rgba(76, 175, 80, 0.5);
        }

        .connection-indicator.connecting {
            background: var(--warning-color);
            animation: pulse 1s infinite;
        }

        .connection-indicator.error {
            background: var(--error-color);
        }

        .connection-indicator.disconnected {
            background: var(--text-secondary);
        }

        @keyframes pulse {
            0%, 100% { opacity: 1; }
            50% { opacity: 0.5; }
        }

        /* 主容器 - 有會話時顯示 */
        .main-container {
            display: none;
            width: 100%;
            margin: 0 auto;
            padding: 20px;
            background: var(--bg-primary);
            color: var(--text-primary);
            min-height: 100vh;
            flex-direction: column;
        }

        .main-container.active {
            display: flex;
        }

        /* 回饋界面樣式 */
        .feedback-container {
            width: 100%;
            max-width: 1000px;
            margin: 0 auto;
        }

        .header {
            background: var(--bg-secondary);
            border-bottom: 1px solid var(--border-color);
            padding: 15px 0;
            margin-bottom: 20px;
            border-radius: 8px 8px 0 0;
        }

        .header-content {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 0 20px;
        }

        .title {
            font-size: 24px;
            font-weight: bold;
            color: var(--accent-color);
            margin: 0;
        }

        .project-info {
            color: var(--text-secondary);
            font-size: 14px;
        }

        .ai-summary-section,
        .feedback-section,
        .command-section {
            background: var(--bg-secondary);
            border: 1px solid var(--border-color);
            border-radius: 8px;
            padding: 20px;
            margin-bottom: 20px;
        }

        .ai-summary-section h2,
        .feedback-section h3,
        .command-section h3 {
            color: var(--accent-color);
            margin-bottom: 16px;
            margin-top: 0;
        }

        .input-group {
            margin-bottom: 16px;
        }

        .input-label {
            display: block;
            font-weight: 500;
            margin-bottom: 8px;
            color: var(--text-primary);
        }

        .text-input,
        .command-input-line {
            width: 100%;
            background: var(--bg-primary);
            border: 1px solid var(--border-color);
            border-radius: 6px;
            padding: 12px;
            color: var(--text-primary);
            font-size: 14px;
            line-height: 1.5;
            resize: vertical;
            font-family: inherit;
            transition: border-color 0.3s ease;
            box-sizing: border-box;
        }

        .text-input:focus,
        .command-input-line:focus {
            outline: none;
            border-color: var(--accent-color);
        }

        .command-input-line {
            font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
        }

        .command-output {
            background: #0f0f0f;
            border: 2px solid var(--border-color);
            border-radius: 6px;
            padding: 12px;
            font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
            font-size: 14px;
            line-height: 1.4;
            color: #00ff00;
            text-shadow: 0 0 5px #00ff00;
            white-space: pre-wrap;
            overflow-y: auto;
            width: 100%;
            box-sizing: border-box;
        }

        .btn {
            padding: 10px 20px;
            border: none;
            border-radius: 6px;
            cursor: pointer;
            font-size: 14px;
            font-weight: 500;
            transition: all 0.3s ease;
            text-decoration: none;
            display: inline-flex;
            align-items: center;
            justify-content: center;
            gap: 8px;
            margin-right: 10px;
        }

        .btn-primary {
            background: var(--accent-color);
            color: white;
        }

        .btn-primary:hover {
            background: var(--accent-hover);
        }

        .btn-secondary {
            background: var(--surface-color);
            color: var(--text-primary);
            border: 1px solid var(--border-color);
        }

        .btn-secondary:hover {
            background: var(--bg-tertiary);
        }

        .button-group {
            display: flex;
            gap: 10px;
            margin-top: 16px;
        }
    </style>
</head>
<body>
    <!-- 連接狀態指示器 -->
    <div class="connection-status">
        <div class="connection-indicator" id="connectionIndicator"></div>
        <span id="connectionText">連接中...</span>
    </div>

    <!-- 等待會話的頁面 -->
    <div class="waiting-container" id="waitingContainer">
        <div class="waiting-content">
            <h1 class="waiting-title">MCP Feedback Enhanced</h1>
            <p class="waiting-description">
                Web UI 互動式回饋收集工具
            </p>
            <div class="waiting-status">
                等待 MCP 服務調用以建立回饋會話...
            </div>

            <!-- 等待页队列入口 -->
            <div id="waitingQueueSection" style="margin-top: 24px; width: 100%; max-width: 480px;">
                <div style="display: flex; gap: 8px; align-items: stretch;">
                    <textarea id="waitingQueueInput" placeholder="预先输入指令，AI 下次调用时自动发送..." style="flex:1; min-height:60px; max-height:120px; background:#2d2d30; color:#ccc; border:1px solid #464647; border-radius:6px; padding:8px 10px; font-size:0.9em; resize:vertical; font-family:inherit;"></textarea>
                    <button id="waitingQueueBtn" style="background:#374151; color:#d1d5db; border:1px solid #4b5563; border-radius:6px; padding:8px 12px; cursor:pointer; font-size:0.85em; white-space:nowrap;">📥 加入队列</button>
                </div>
                <div style="display:flex; justify-content:space-between; align-items:center; margin-top:8px;">
                    <span id="waitingQueueInfo" style="color:#6b7280; font-size:0.8em;">队列: 0 条</span>
                    <button id="waitingQueueToggle" style="background:none; border:none; color:#6b7280; font-size:0.8em; cursor:pointer; text-decoration:underline;">查看队列</button>
                </div>
                <div id="waitingQueuePanel" style="display:none; background:#252526; border:1px solid #464647; border-radius:6px; padding:10px; margin-top:8px;">
                    <div id="waitingQueueList" style="max-height:160px; overflow-y:auto;"></div>
                    <div id="waitingQueueEmpty" style="color:#6b7280; text-align:center; padding:8px; font-size:0.85em;">队列为空</div>
                    <div style="text-align:right; margin-top:6px;">
                        <button id="waitingClearQueueBtn" style="font-size:0.75em; padding:3px 8px; background:#374151; color:#9ca3af; border:1px solid #4b5563; border-radius:4px; cursor:pointer;">清空</button>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 主要回饋界面 -->
    <div class="main-container" id="mainContainer">
        <!-- 這裡將動態載入回饋界面內容 -->
    </div>

    <!-- JavaScript -->
    <script src="/static/js/i18n.js?v=2025010505"></script>
    <!-- 載入所有模組 -->
    <script src="/static/js/modules/utils.js?v=2025010505"></script>

    <script src="/static/js/modules/websocket-manager.js?v=2025010505"></script>
    <script src="/static/js/modules/image-handler.js?v=2025010505"></script>
    <script src="/static/js/modules/settings-manager.js?v=2025010505"></script>
    <script src="/static/js/modules/ui-manager.js?v=2025010505"></script>
    <script src="/static/js/modules/auto-refresh-manager.js?v=2025010505"></script>
    <!-- 主應用程式 -->
    <script src="/static/js/app.js?v=2025010505"></script>
    <script>
    (function() {
        var queueBtn = document.getElementById('waitingQueueBtn');
        var queueInput = document.getElementById('waitingQueueInput');
        var queueInfo = document.getElementById('waitingQueueInfo');
        var queueToggle = document.getElementById('waitingQueueToggle');
        var queuePanel = document.getElementById('waitingQueuePanel');
        var queueList = document.getElementById('waitingQueueList');
        var queueEmpty = document.getElementById('waitingQueueEmpty');
        var clearBtn = document.getElementById('waitingClearQueueBtn');

        if (!queueBtn || !queueInput) return;

        function escapeHtml(s) {
            var div = document.createElement('div');
            div.textContent = s;
            return div.innerHTML;
        }

        function refreshQueue() {
            fetch('/api/queue-status')
            .then(function(r) { return r.json(); })
            .then(function(data) {
                queueInfo.textContent = '队列: ' + data.size + ' 条';
                renderList(data.items || []);
            })
            .catch(function() {});
        }

        function renderList(items) {
            if (items.length === 0) {
                queueList.innerHTML = '';
                queueEmpty.style.display = 'block';
                return;
            }
            queueEmpty.style.display = 'none';
            var html = '';
            items.forEach(function(item, idx) {
                var t = new Date(item.queued_at * 1000).toLocaleTimeString();
                var preview = item.feedback_preview || '(空)';
                html += '<div style="display:flex; justify-content:space-between; align-items:center; padding:5px 8px; background:#1e1e1e; border-radius:4px; margin-bottom:3px; font-size:0.83em;">'
                    + '<div style="flex:1; overflow:hidden; white-space:nowrap; text-overflow:ellipsis; color:#ccc;">'
                    + '<span style="color:#666; margin-right:5px;">#' + (idx+1) + '</span>'
                    + escapeHtml(preview)
                    + '</div>'
                    + '<div style="display:flex; align-items:center; gap:6px; margin-left:8px; flex-shrink:0;">'
                    + '<span style="color:#666; font-size:0.8em;">' + t + '</span>'
                    + '<button data-id="' + item.id + '" class="wq-remove" style="background:none; border:none; color:#f44336; cursor:pointer; font-size:1em; padding:0 2px;">\u00d7</button>'
                    + '</div></div>';
            });
            queueList.innerHTML = html;
            queueList.querySelectorAll('.wq-remove').forEach(function(btn) {
                btn.addEventListener('click', function() {
                    fetch('/api/queue-item/' + btn.dataset.id, { method: 'DELETE' })
                    .then(function() { refreshQueue(); })
                    .catch(function() {});
                });
            });
        }

        queueBtn.addEventListener('click', function() {
            var text = queueInput.value.trim();
            if (!text) return;
            fetch('/api/queue-feedback', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({ feedback: text, images: [], settings: {} })
            })
            .then(function(r) { return r.json(); })
            .then(function(data) {
                if (data.status === 'success') {
                    queueInput.value = '';
                    refreshQueue();
                }
            })
            .catch(function() {});
        });

        queueToggle.addEventListener('click', function() {
            var visible = queuePanel.style.display !== 'none';
            queuePanel.style.display = visible ? 'none' : 'block';
            queueToggle.textContent = visible ? '查看队列' : '收起队列';
            if (!visible) refreshQueue();
        });

        clearBtn.addEventListener('click', function() {
            fetch('/api/queue', { method: 'DELETE' })
            .then(function() { refreshQueue(); })
            .catch(function() {});
        });

        refreshQueue();
    })();
    </script>
</body>
</html>
