import React, { useState } from 'react';
import { Form, Input, Button, Card, message } from 'antd';
import { UserOutlined, LockOutlined, FileTextOutlined } from '@ant-design/icons';
import { useNavigate, Link } from 'react-router-dom';
import axios from 'axios';
import API_BASE_URL from '../config';

const Login = () => {
  const [loading, setLoading] = useState(false);
  const navigate = useNavigate();

  const onFinish = async (values) => {
    setLoading(true);
    try {
      const response = await axios.post(`${API_BASE_URL}/api/auth/login`, values);
      const { token, user } = response.data;
      
      localStorage.setItem('token', token);
      localStorage.setItem('user', JSON.stringify(user));
      
      message.success('登录成功');
      navigate('/dashboard');
    } catch (error) {
      console.error('登录错误详情:', error.response);
      if (error.response?.status === 401) {
        message.error('用户名或密码错误，请重新输入');
      } else {
        message.error(error.response?.data?.message || '登录失败，请检查网络或联系管理员');
      }
    } finally {
      setLoading(false);
    }
  };

  return (
    <div style={{ 
      display: 'flex', 
      justifyContent: 'center', 
      alignItems: 'center', 
      height: '100vh',
      background: 'linear-gradient(135deg, #1677ff 0%, #003a8c 100%)' 
    }}>
      <Card style={{ width: 400, borderRadius: '12px', boxShadow: '0 8px 24px rgba(0,0,0,0.15)' }}>
        <div style={{ textAlign: 'center', marginBottom: 32 }}>
          <div style={{ 
            width: 64, 
            height: 64, 
            background: '#1677ff', 
            borderRadius: '16px', 
            display: 'inline-flex', 
            alignItems: 'center', 
            justifyContent: 'center',
            marginBottom: 16,
            boxShadow: '0 4px 12px rgba(22,119,255,0.3)'
          }}>
            <FileTextOutlined style={{ color: 'white', fontSize: 32 }} />
          </div>
          <h2 style={{ margin: 0, color: '#1f1f1f', fontSize: '24px', fontWeight: 600 }}>科技成果管理系统</h2>
          <p style={{ color: '#8c8c8c', marginTop: 8 }}>请登录您的账号以继续</p>
        </div>
        <Form name="login" onFinish={onFinish}>
          <Form.Item
            name="username"
            rules={[
              { required: true, message: '请输入手机号' },
              { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号格式' }
            ]}
          >
            <Input prefix={<UserOutlined />} placeholder="手机号" size="large" />
          </Form.Item>
          <Form.Item
            name="password"
            rules={[{ required: true, message: '请输入密码' }]}
          >
            <Input.Password prefix={<LockOutlined />} placeholder="密码" size="large" />
          </Form.Item>
          <Form.Item>
            <Button type="primary" htmlType="submit" block size="large" loading={loading}>
              登录
            </Button>
            <div style={{ display: 'flex', justifyContent: 'space-between', marginTop: 16 }}>
              <Link to="/reset-password">忘记密码？</Link>
              <span>还没有账号？ <Link to="/register">立即注册</Link></span>
            </div>
          </Form.Item>
        </Form>
      </Card>
    </div>
  );
};

export default Login;
