/**
 * 登录页面组件
 * 用户登录界面，展示如何调用后端API进行认证
 * 
 * 【重要说明】：
 * 1. 使用Ant Design表单组件构建专业登录界面
 * 2. 通过AuthContext调用后端登录API
 * 3. 表单验证和错误处理
 * 4. 登录成功后自动跳转到仪表盘
 */

import React, { useState } from 'react';
import { Form, Input, Button, Card, Typography, Divider, message } from 'antd';
import { UserOutlined, LockOutlined, LoginOutlined } from '@ant-design/icons';
import { Link, useNavigate } from 'react-router-dom';
import { useAuth } from '../contexts/AuthContext';

const { Title, Text } = Typography;

/**
 * 登录页面组件
 */
const LoginPage = () => {
  const [loading, setLoading] = useState(false);
  const navigate = useNavigate();
  const { login } = useAuth();

  /**
   * 处理登录表单提交
   * 【API调用标注】这里调用后端登录接口
   */
  const handleLogin = async (values) => {
    try {
      setLoading(true);
      
      // 【API调用标注】调用后端登录接口
      // 后端接口: POST /auth/login
      // 对应后端文件: backend/api/auth_api.py -> login()
      const result = await login({
        username: values.username,
        password: values.password,
      });

      if (result.success) {
        message.success('登录成功！');
        navigate('/Dashboard');
      } else {
        message.error(result.error || '登录失败');
      }
    } catch (error) {
      console.error('登录失败:', error);
      message.error('登录失败，请检查网络连接');
    } finally {
      setLoading(false);
    }
  };

  return (
    <div style={{
      minHeight: '100vh',
      display: 'flex',
      justifyContent: 'center',
      alignItems: 'center',
      background: 'linear-gradient(135deg, #667eea 0%, #764ba2 100%)',
      padding: '20px'
    }}>
      <Card
        style={{
          width: '100%',
          maxWidth: 400,
          boxShadow: '0 8px 32px rgba(0, 0, 0, 0.1)',
          borderRadius: 12,
        }}
      >
        {/* 标题 */}
        <div style={{ textAlign: 'center', marginBottom: 32 }}>
          <Title level={2} style={{ margin: 0, color: '#1890ff' }}>
            AI换脸换装系统
          </Title>
          <Text type="secondary">请登录您的账户</Text>
        </div>

        {/* 登录表单 */}
        <Form
          name="login"
          onFinish={handleLogin}
          autoComplete="off"
          size="large"
        >
          {/* 用户名输入框 */}
          <Form.Item
            name="username"
            rules={[
              { required: true, message: '请输入用户名！' },
              { min: 3, message: '用户名至少3个字符！' },
            ]}
          >
            <Input
              prefix={<UserOutlined />}
              placeholder="用户名"
              autoComplete="username"
            />
          </Form.Item>

          {/* 密码输入框 */}
          <Form.Item
            name="password"
            rules={[
              { required: true, message: '请输入密码！' },
              { min: 6, message: '密码至少6个字符！' },
            ]}
          >
            <Input.Password
              prefix={<LockOutlined />}
              placeholder="密码"
              autoComplete="current-password"
            />
          </Form.Item>

          {/* 登录按钮 */}
          <Form.Item>
            <Button
              type="primary"
              htmlType="submit"
              loading={loading}
              icon={<LoginOutlined />}
              style={{
                width: '100%',
                height: 48,
                fontSize: 16,
                borderRadius: 8,
              }}
            >
              {loading ? '登录中...' : '登录'}
            </Button>
          </Form.Item>
        </Form>

        {/* 分割线 */}
        <Divider>
          <Text type="secondary">还没有账户？</Text>
        </Divider>

        {/* 注册链接 */}
        <div style={{ textAlign: 'center' }}>
          <Link to="/register">
            <Button type="link" size="large">
              立即注册
            </Button>
          </Link>
        </div>

        {/* 系统信息 */}
        <div style={{ 
          textAlign: 'center', 
          marginTop: 24,
          padding: 16,
          background: '#f5f5f5',
          borderRadius: 8
        }}>
          <Text type="secondary" style={{ fontSize: 12 }}>
            专业AI换脸换装系统 | 安全可靠
          </Text>
        </div>
      </Card>
    </div>
  );
};

export default LoginPage; 