<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Web WeChat - 登录</title>
    <link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        .login-container {
            display: flex;
            align-items: center;
            justify-content: center;
            min-height: 100vh;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            padding: 20px;
        }
        
        .login-box {
            background-color: var(--bg-secondary);
            border-radius: var(--radius-lg);
            padding: 40px;
            width: 100%;
            max-width: 400px;
            box-shadow: var(--shadow);
        }
        
        .login-header {
            text-align: center;
            margin-bottom: 32px;
        }
        
        .login-header i {
            font-size: 48px;
            color: var(--accent-blue);
            margin-bottom: 16px;
        }
        
        .login-header h1 {
            font-size: 28px;
            font-weight: 600;
            margin-bottom: 8px;
        }
        
        .login-header p {
            color: var(--text-secondary);
            font-size: 14px;
        }
        
        .form-group {
            margin-bottom: 20px;
        }
        
        .form-group label {
            display: block;
            font-size: 14px;
            color: var(--text-secondary);
            margin-bottom: 8px;
        }
        
        .form-control {
            width: 100%;
            padding: 12px 16px;
            background-color: var(--bg-tertiary);
            border: 1px solid var(--border-color);
            border-radius: var(--radius-md);
            color: var(--text-primary);
            font-size: 16px;
            transition: border-color 0.2s;
        }
        
        .form-control:focus {
            outline: none;
            border-color: var(--accent-blue);
        }
        
        .login-btn {
            width: 100%;
            padding: 14px;
            background-color: var(--accent-blue);
            color: white;
            border: none;
            border-radius: var(--radius-md);
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: background-color 0.2s;
            margin-top: 8px;
        }
        
        .login-btn:hover {
            background-color: #007aff;
        }
        
        .login-btn:disabled {
            opacity: 0.6;
            cursor: not-allowed;
        }
        
        .error-message {
            color: var(--accent-red);
            font-size: 14px;
            margin-top: 8px;
            text-align: center;
        }
        
        .save-info {
            display: flex;
            align-items: center;
            gap: 8px;
            margin-top: 16px;
            color: var(--text-secondary);
            font-size: 14px;
        }
        
        .save-info input[type="checkbox"] {
            accent-color: var(--accent-blue);
        }
    </style>
</head>
<body class="dark-theme">
    <div class="login-container">
        <div class="login-box">
            <div class="login-header">
                <i class="fas fa-comments"></i>
                <h1>Web WeChat</h1>
                <p>苹果风格 · 深色主题 · 即时通讯</p>
            </div>
            
            <form id="loginForm">
                <div class="form-group">
                    <label for="account">账号 / 手机号</label>
                    <input 
                        type="text" 
                        id="account" 
                        class="form-control" 
                        placeholder="请输入英飞思想家账号"
                        required
                    >
                </div>
                
                <div class="form-group">
                    <label for="password">密码</label>
                    <input 
                        type="password" 
                        id="password" 
                        class="form-control" 
                        placeholder="请输入密码"
                        required
                    >
                </div>
                
                <div class="error-message" id="errorMessage"></div>
                
                <button type="submit" class="login-btn" id="loginBtn">
                    <span id="loginText">登录</span>
                    <i class="fas fa-spinner fa-spin" id="loginSpinner" style="display: none;"></i>
                </button>
                
                <div class="save-info">
                    <input type="checkbox" id="saveInfo" checked>
                    <label for="saveInfo">记住账号密码</label>
                </div>
            </form>
        </div>
    </div>
    
    <script>
        document.getElementById('loginForm').addEventListener('submit', async (e) => {
            e.preventDefault();
            
            const account = document.getElementById('account').value.trim();
            const password = document.getElementById('password').value.trim();
            const saveInfo = document.getElementById('saveInfo').checked;
            const errorMessage = document.getElementById('errorMessage');
            const loginBtn = document.getElementById('loginBtn');
            const loginText = document.getElementById('loginText');
            const loginSpinner = document.getElementById('loginSpinner');
            
            // 验证输入
            if (!account || !password) {
                errorMessage.textContent = '请输入账号和密码';
                return;
            }
            
            // 显示加载状态
            loginText.style.display = 'none';
            loginSpinner.style.display = 'inline-block';
            loginBtn.disabled = true;
            errorMessage.textContent = '';
            
            try {
                const response = await fetch('/login', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        account: account,
                        password: password
                    })
                });
                
                const result = await response.json();
                
                if (result.success) {
                    // 保存到localStorage
                    if (saveInfo) {
                        localStorage.setItem('user_account', account);
                    }
                    
                    // 跳转到主页面
                    window.location.href = '/';
                } else {
                    errorMessage.textContent = result.message;
                }
            } catch (error) {
                errorMessage.textContent = '网络错误，请稍后重试';
                console.error('登录失败:', error);
            } finally {
                // 恢复按钮状态
                loginText.style.display = 'inline-block';
                loginSpinner.style.display = 'none';
                loginBtn.disabled = false;
            }
        });
        
        // 检查是否已保存账号
        window.addEventListener('DOMContentLoaded', () => {
            const savedAccount = localStorage.getItem('user_account');
            if (savedAccount) {
                document.getElementById('account').value = savedAccount;
            }
        });
    </script>
</body>
</html>