<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>WCG - Watch Commits for Git</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 20px;
        }
        
        .container {
            max-width: 1200px;
            margin: 0 auto;
        }
        
        header {
            text-align: center;
            color: white;
            margin-bottom: 30px;
        }
        
        header h1 {
            font-size: 2.5em;
            margin-bottom: 10px;
        }
        
        header p {
            font-size: 1.1em;
            opacity: 0.9;
        }
        
        .card {
            background: white;
            border-radius: 12px;
            padding: 30px;
            margin-bottom: 20px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
        }
        
        .card h2 {
            color: #333;
            margin-bottom: 20px;
            padding-bottom: 10px;
            border-bottom: 2px solid #667eea;
        }
        
        .form-group {
            margin-bottom: 20px;
        }
        
        label {
            display: block;
            margin-bottom: 5px;
            color: #555;
            font-weight: 600;
        }
        
        input[type="text"],
        input[type="password"],
        input[type="time"] {
            width: 100%;
            padding: 12px;
            border: 2px solid #e0e0e0;
            border-radius: 6px;
            font-size: 14px;
            transition: border-color 0.3s;
        }
        
        input:focus {
            outline: none;
            border-color: #667eea;
        }
        
        button {
            background: #667eea;
            color: white;
            border: none;
            padding: 12px 24px;
            border-radius: 6px;
            cursor: pointer;
            font-size: 16px;
            font-weight: 600;
            transition: background 0.3s;
        }
        
        button:hover {
            background: #5568d3;
        }
        
        button.danger {
            background: #e74c3c;
        }
        
        button.danger:hover {
            background: #c0392b;
        }
        
        .repo-item {
            background: #f8f9fa;
            padding: 15px;
            border-radius: 6px;
            margin-bottom: 10px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        
        .repo-info {
            flex: 1;
        }
        
        .repo-info strong {
            display: block;
            color: #333;
            margin-bottom: 5px;
        }
        
        .repo-info span {
            color: #666;
            font-size: 0.9em;
        }
        
        .message {
            padding: 12px;
            border-radius: 6px;
            margin-bottom: 15px;
            display: none;
        }
        
        .message.success {
            background: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        
        .message.error {
            background: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        
        .message.show {
            display: block;
        }
        
        .grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
            gap: 15px;
        }
    </style>
</head>
<body>
    <div class="container">
        <header>
            <h1>🔍 WCG</h1>
            <p>Watch Commits for Git - 自动化GitHub提交监控与汇总</p>
        </header>
        
        <div class="card">
            <h2>LLM 配置</h2>
            <div id="llm-message" class="message"></div>
            <form id="llm-form">
                <div class="form-group">
                    <label for="api_url">API URL</label>
                    <input type="text" id="api_url" name="api_url" value="{{ config.llm.api_url }}" required>
                </div>
                <div class="form-group">
                    <label for="api_key">API Key</label>
                    <input type="password" id="api_key" name="api_key" value="{{ config.llm.api_key }}" required>
                </div>
                <div class="form-group">
                    <label for="model">模型</label>
                    <input type="text" id="model" name="model" value="{{ config.llm.model }}" required>
                </div>
                <button type="submit">保存 LLM 配置</button>
            </form>
        </div>
        
        <div class="card">
            <h2>仓库配置</h2>
            <div id="repo-message" class="message"></div>
            
            <div id="repo-list">
                {% for repo in config.repositories %}
                <div class="repo-item" data-name="{{ repo.name }}" data-branch="{{ repo.branch }}">
                    <div class="repo-info">
                        <strong>{{ repo.name }} ({{ repo.branch }})</strong>
                        <span>Webhook: {{ repo.webhook_url[:50] }}...</span>
                    </div>
                    <button class="danger" onclick="removeRepo('{{ repo.name }}', '{{ repo.branch }}')">删除</button>
                </div>
                {% endfor %}
            </div>
            
            <hr style="margin: 20px 0; border: none; border-top: 1px solid #e0e0e0;">
            
            <h3 style="margin-bottom: 15px; color: #555;">添加新仓库</h3>
            <form id="repo-form">
                <div class="grid">
                    <div class="form-group">
                        <label for="repo_name">仓库名称 (owner/repo)</label>
                        <input type="text" id="repo_name" name="name" placeholder="例如: octocat/Hello-World" required>
                    </div>
                    <div class="form-group">
                        <label for="repo_branch">分支名称</label>
                        <input type="text" id="repo_branch" name="branch" placeholder="例如: main" required>
                    </div>
                    <div class="form-group">
                        <label for="webhook_url">Webhook URL</label>
                        <input type="text" id="webhook_url" name="webhook_url" placeholder="https://..." required>
                    </div>
                </div>
                <button type="submit">添加仓库</button>
            </form>
        </div>
        
        <div class="card">
            <h2>其他设置</h2>
            <div id="settings-message" class="message"></div>
            <form id="settings-form">
                <div class="grid">
                    <div class="form-group">
                        <label for="schedule_time">定时执行时间 (HH:MM)</label>
                        <input type="text" id="schedule_time" name="schedule_time" value="{{ config.schedule_time }}" placeholder="10:00" required>
                    </div>
                    <div class="form-group">
                        <label for="timezone">时区 (Timezone)</label>
                        <input type="text" id="timezone" name="timezone" value="{{ config.timezone }}" placeholder="Asia/Shanghai" required>
                        <small style="color: #666; font-size: 0.85em;">使用标准时区名称，如 Asia/Shanghai (UTC+8)</small>
                    </div>
                    <div class="form-group">
                        <label for="summary_dir">汇总保存目录</label>
                        <input type="text" id="summary_dir" name="summary_dir" value="{{ config.summary_dir }}" required>
                    </div>
                </div>
                <div class="form-group">
                    <label for="github_token">GitHub Token</label>
                    <input type="password" id="github_token" name="github_token" value="{{ config.github_token }}" required>
                </div>
                <button type="submit">保存设置</button>
            </form>
        </div>
    </div>
    
    <script>
        function showMessage(elementId, message, isSuccess) {
            const el = document.getElementById(elementId);
            el.textContent = message;
            el.className = 'message show ' + (isSuccess ? 'success' : 'error');
            setTimeout(() => {
                el.className = 'message';
            }, 5000);
        }
        
        document.getElementById('llm-form').addEventListener('submit', async (e) => {
            e.preventDefault();
            const formData = new FormData(e.target);
            const data = Object.fromEntries(formData.entries());
            
            try {
                const response = await fetch('/api/llm', {
                    method: 'POST',
                    headers: {'Content-Type': 'application/json'},
                    body: JSON.stringify(data)
                });
                const result = await response.json();
                showMessage('llm-message', result.message, result.success);
            } catch (error) {
                showMessage('llm-message', '更新失败: ' + error.message, false);
            }
        });
        
        document.getElementById('repo-form').addEventListener('submit', async (e) => {
            e.preventDefault();
            const formData = new FormData(e.target);
            const data = Object.fromEntries(formData.entries());
            
            try {
                const response = await fetch('/api/repository', {
                    method: 'POST',
                    headers: {'Content-Type': 'application/json'},
                    body: JSON.stringify(data)
                });
                const result = await response.json();
                showMessage('repo-message', result.message, result.success);
                
                if (result.success) {
                    setTimeout(() => location.reload(), 1000);
                }
            } catch (error) {
                showMessage('repo-message', '添加失败: ' + error.message, false);
            }
        });
        
        async function removeRepo(name, branch) {
            if (!confirm(`确定要删除 ${name} (${branch}) 吗？`)) return;
            
            try {
                const response = await fetch('/api/repository', {
                    method: 'DELETE',
                    headers: {'Content-Type': 'application/json'},
                    body: JSON.stringify({name, branch})
                });
                const result = await response.json();
                showMessage('repo-message', result.message, result.success);
                
                if (result.success) {
                    setTimeout(() => location.reload(), 1000);
                }
            } catch (error) {
                showMessage('repo-message', '删除失败: ' + error.message, false);
            }
        }
        
        document.getElementById('settings-form').addEventListener('submit', async (e) => {
            e.preventDefault();
            const formData = new FormData(e.target);
            const data = Object.fromEntries(formData.entries());
            
            try {
                const response = await fetch('/api/config/full', {
                    method: 'POST',
                    headers: {'Content-Type': 'application/json'},
                    body: JSON.stringify(data)
                });
                const result = await response.json();
                showMessage('settings-message', result.message, result.success);
            } catch (error) {
                showMessage('settings-message', '更新失败: ' + error.message, false);
            }
        });
    </script>
</body>
</html>
