<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录 - 问题排查助手</title>
    <style>
        :root {
            --primary: #2563eb;
            --primary-hover: #1d4ed8;
            --danger: #dc2626;
            --gray-50: #f9fafb;
            --gray-100: #f3f4f6;
            --gray-200: #e5e7eb;
            --gray-500: #6b7280;
            --gray-700: #374151;
        }

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

        body {
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Microsoft YaHei", sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
        }

        .login-card {
            background: white;
            border-radius: 16px;
            box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
            width: 100%;
            max-width: 400px;
            overflow: hidden;
        }

        .login-header {
            background: linear-gradient(135deg, #1e3a8a 0%, #1e40af 100%);
            color: white;
            padding: 32px;
            text-align: center;
        }

        .login-header h1 {
            font-size: 24px;
            font-weight: 700;
            margin-bottom: 8px;
        }

        .login-header p {
            opacity: 0.8;
            font-size: 14px;
        }

        .login-body {
            padding: 32px;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-label {
            display: block;
            font-size: 14px;
            font-weight: 600;
            color: var(--gray-700);
            margin-bottom: 8px;
        }

        .form-input {
            width: 100%;
            padding: 12px 14px;
            border: 1px solid var(--gray-200);
            border-radius: 8px;
            font-size: 15px;
            transition: border-color 0.2s, box-shadow 0.2s;
        }

        .form-input:focus {
            outline: none;
            border-color: var(--primary);
            box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
        }

        .checkbox-label {
            display: flex;
            align-items: center;
            font-size: 14px;
            color: var(--gray-700);
            cursor: pointer;
        }

        .checkbox-label input {
            margin-right: 8px;
            width: 16px;
            height: 16px;
        }

        .btn-login {
            width: 100%;
            padding: 14px;
            background: var(--primary);
            color: white;
            border: none;
            border-radius: 8px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: background 0.2s;
            margin-top: 24px;
        }

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

        .btn-login:disabled {
            opacity: 0.6;
            cursor: not-allowed;
        }

        .error-msg {
            background: #fee2e2;
            border: 1px solid #fca5a5;
            color: #991b1b;
            padding: 12px;
            border-radius: 8px;
            font-size: 14px;
            margin-top: 16px;
            display: none;
        }

        .login-footer {
            background: var(--gray-50);
            padding: 16px 32px;
            text-align: center;
            border-top: 1px solid var(--gray-200);
        }

        .login-footer p {
            font-size: 13px;
            color: var(--gray-500);
        }
    </style>
</head>
<body>
    <div class="login-card">
        <div class="login-header">
            <h1>🔐 问题排查助手</h1>
            <p>请登录以继续访问系统</p>
        </div>

        <div class="login-body">
            <form id="loginForm">
                <div class="form-group">
                    <label class="form-label">用户名</label>
                    <input type="text" id="username"
                           class="form-input"
                           placeholder="请输入用户名"
                           autocomplete="username"
                           required>
                </div>

                <div class="form-group">
                    <label class="form-label">密码</label>
                    <input type="password" id="password"
                           class="form-input"
                           placeholder="请输入密码"
                           autocomplete="current-password"
                           required>
                </div>

                <div class="form-group">
                    <label class="checkbox-label">
                        <input type="checkbox" id="remember">
                        记住登录状态
                    </label>
                </div>

                <button type="submit" class="btn-login" id="loginBtn">
                    🔑 登  录
                </button>

                <div id="errorMsg" class="error-msg"></div>
            </form>
        </div>

        <div class="login-footer">
            <p>如有账号问题，请联系管理员</p>
        </div>
    </div>

    <script>
        // 登录表单提交
        document.getElementById('loginForm').addEventListener('submit', async (e) => {
            e.preventDefault();

            const username = document.getElementById('username').value.trim();
            const password = document.getElementById('password').value;
            const remember = document.getElementById('remember').checked;

            if (!username || !password) {
                showError('请输入用户名和密码');
                return;
            }

            // 显示加载状态
            const loginBtn = document.getElementById('loginBtn');
            const originalText = loginBtn.textContent;
            loginBtn.textContent = '⏳ 登录中...';
            loginBtn.disabled = true;

            try {
                const resp = await fetch('/login', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify({ username, password, remember }),
                    credentials: 'include'
                });

                const data = await resp.json();

                if (data.success) {
                    // 登录成功，跳转主页
                    window.location.href = '/';
                } else {
                    showError(data.error || '登录失败');
                    loginBtn.textContent = originalText;
                    loginBtn.disabled = false;
                }
            } catch (e) {
                showError('网络错误，请稍后重试');
                loginBtn.textContent = originalText;
                loginBtn.disabled = false;
            }
        });

        function showError(msg) {
            const errorEl = document.getElementById('errorMsg');
            errorEl.textContent = msg;
            errorEl.style.display = 'block';
        }

        // 清除错误提示
        document.getElementById('username').addEventListener('input', () => {
            document.getElementById('errorMsg').style.display = 'none';
        });

        document.getElementById('password').addEventListener('input', () => {
            document.getElementById('errorMsg').style.display = 'none';
        });
    </script>
</body>
</html>