import React, { useState } from 'react';
import { useNavigate } from 'react-router-dom';
import { Form, Input, Button, message } from 'antd';
import { MobileOutlined, SafetyOutlined, ShopOutlined } from '@ant-design/icons';
import { loginByCode, sendSellerCode } from '@/api/auth';
import { useAuthStore } from '@/store/auth';
import './Login.css';

const Login: React.FC = () => {
  const [loading, setLoading] = useState(false);
  const [countdown, setCountdown] = useState(0);
  const [form] = Form.useForm();
  const navigate = useNavigate();
  const setAuth = useAuthStore((s) => s.setAuth);

  const startCountdown = () => {
    setCountdown(60);
    const timer = setInterval(() => {
      setCountdown((prev) => {
        if (prev <= 1) {
          clearInterval(timer);
          return 0;
        }
        return prev - 1;
      });
    }, 1000);
  };

  const handleSendCode = async () => {
    const phone = form.getFieldValue('phone');
    if (!phone || phone.length !== 11) {
      message.error('请输入正确的手机号');
      return;
    }
    try {
      await sendSellerCode(phone);
      message.success('验证码已发送');
      startCountdown();
    } catch (e: any) {
      message.error(e.message || '发送失败');
    }
  };

  const onFinish = async (values: { phone: string; code: string }) => {
    setLoading(true);
    try {
      const res: any = await loginByCode(values.phone, values.code);
      setAuth(res.data.accessToken, res.data.sellerInfo);
      message.success('登录成功');
      navigate('/dashboard');
    } catch (e: any) {
      message.error(e.message || '登录失败');
    } finally {
      setLoading(false);
    }
  };

  return (
    <div className="merchant-login-container">
      {/* 顶部装饰背景 */}
      <div className="merchant-login-bg">
        <div className="bg-decoration">
          <div className="bg-circle circle-1"></div>
          <div className="bg-circle circle-2"></div>
          <div className="bg-circle circle-3"></div>
        </div>
      </div>

      {/* 中央登录卡片 */}
      <div className="merchant-login-card">
        {/* 品牌区域 */}
        <div className="merchant-brand">
          <div className="merchant-brand-icon">
            <ShopOutlined style={{ fontSize: 56, color: '#ff6b6b' }} />
          </div>
          <h1 className="merchant-brand-title">品住会商家后台</h1>
          <p className="merchant-brand-subtitle">轻松管理您的房源和订单</p>
        </div>

        {/* 登录表单 */}
        <div className="merchant-form-section">
          <Form form={form} onFinish={onFinish} size="large" className="merchant-form">
            <Form.Item
              name="phone"
              rules={[
                { required: true, message: '请输入手机号' },
                { pattern: /^1\d{10}$/, message: '手机号格式不正确' }
              ]}
            >
              <Input
                prefix={<MobileOutlined className="merchant-input-icon" />}
                placeholder="手机号"
                maxLength={11}
                className="merchant-input"
              />
            </Form.Item>

            <Form.Item
              name="code"
              rules={[{ required: true, message: '请输入验证码' }]}
            >
              <div className="merchant-code-wrapper">
                <Input
                  prefix={<SafetyOutlined className="merchant-input-icon" />}
                  placeholder="验证码"
                  maxLength={6}
                  className="merchant-input merchant-code-input"
                />
                <Button
                  type="default"
                  disabled={countdown > 0}
                  onClick={handleSendCode}
                  className="merchant-code-button"
                >
                  {countdown > 0 ? `${countdown}s` : '获取验证码'}
                </Button>
              </div>
            </Form.Item>

            <Form.Item>
              <Button
                type="primary"
                htmlType="submit"
                loading={loading}
                block
                className="merchant-login-button"
              >
                登录
              </Button>
            </Form.Item>

            <div className="merchant-dev-tip">
              <p>开发环境验证码: 123456</p>
            </div>
          </Form>
        </div>

        {/* 特性展示 */}
        <div className="merchant-features">
          <div className="merchant-feature-item">
            <div className="merchant-feature-icon">📦</div>
            <div className="merchant-feature-text">房源管理</div>
          </div>
          <div className="merchant-feature-item">
            <div className="merchant-feature-icon">📋</div>
            <div className="merchant-feature-text">订单处理</div>
          </div>
          <div className="merchant-feature-item">
            <div className="merchant-feature-icon">💰</div>
            <div className="merchant-feature-text">财务结算</div>
          </div>
        </div>

        {/* 底部 */}
        <div className="merchant-footer">
          <p>© 2026 品住会. All rights reserved.</p>
        </div>
      </div>
    </div>
  );
};

export default Login;
