import React, { useState } from 'react';
import { motion, AnimatePresence } from 'framer-motion';
import { X, Sparkles, FileText, Zap, Building2 } from 'lucide-react';

interface RequirementModalProps {
  isOpen: boolean;
  onClose: () => void;
  onSubmit: (requirement: string) => void;
}

const requirementTemplates = [
  {
    id: 1,
    title: '新能源汽车展',
    icon: <Zap className="w-5 h-5" />,
    content: `展会名称：2024长三角国际新能源汽车与智能交通产业博览会
地点：上海国家会展中心
时间：2024年10月18日-20日
规模：50,000平方米，预计350家展商，50,000人次观众
主题：双碳目标下的新能源汽车产业创新与发展
特色：整车展示、核心零部件、充电设施、智能交通解决方案`
  }
];

const RequirementModal: React.FC<RequirementModalProps> = ({ isOpen, onClose, onSubmit }) => {
  const [requirement, setRequirement] = useState('');
  const [selectedTemplate, setSelectedTemplate] = useState<number | null>(null);

  const handleTemplateSelect = (template: typeof requirementTemplates[0]) => {
    setRequirement(template.content);
    setSelectedTemplate(template.id);
  };

  const handleSubmit = () => {
    if (requirement.trim()) {
      onSubmit(requirement);
      setRequirement('');
      setSelectedTemplate(null);
    }
  };

  return (
    <AnimatePresence>
      {isOpen && (
        <>
          {/* 背景遮罩 */}
          <motion.div
            initial={{ opacity: 0 }}
            animate={{ opacity: 1 }}
            exit={{ opacity: 0 }}
            className="fixed inset-0 bg-black/60 backdrop-blur-sm z-50"
            onClick={onClose}
          />

          {/* 弹窗内容 */}
          <motion.div
            initial={{ opacity: 0, scale: 0.9, y: 20 }}
            animate={{ opacity: 1, scale: 1, y: 0 }}
            exit={{ opacity: 0, scale: 0.9, y: 20 }}
            transition={{ type: 'spring', damping: 25, stiffness: 300 }}
            className="fixed inset-0 flex items-center justify-center z-50 p-4"
            onClick={(e) => e.stopPropagation()}
          >
            <div className="bg-white rounded-2xl shadow-2xl max-w-4xl w-full max-h-[90vh] overflow-hidden">
              {/* 头部 */}
              <div className="bg-gradient-to-r from-blue-600 to-purple-600 p-6 text-white">
                <div className="flex items-center justify-between">
                  <div>
                    <h2 className="text-2xl font-bold mb-2">输入展会策划需求</h2>
                    <p className="text-blue-100">请详细描述您的展会策划需求，AI将为您生成完整方案</p>
                  </div>
                  <button
                    onClick={onClose}
                    className="p-2 hover:bg-white/20 rounded-lg transition-colors"
                  >
                    <X className="w-6 h-6" />
                  </button>
                </div>
              </div>

              <div className="p-6">
                {/* 需求模板 */}
                <div className="mb-6">
                  <h3 className="text-sm font-semibold text-gray-700 mb-3 flex items-center gap-2">
                    <FileText className="w-4 h-4" />
                    快速选择需求模板
                  </h3>
                  <div className="grid grid-cols-1 gap-3">
                    {requirementTemplates.map((template) => (
                      <button
                        key={template.id}
                        onClick={() => handleTemplateSelect(template)}
                        className={`p-4 rounded-xl border-2 transition-all hover:shadow-lg ${
                          selectedTemplate === template.id
                            ? 'border-blue-500 bg-blue-50'
                            : 'border-gray-200 hover:border-gray-300'
                        }`}
                      >
                        <div className="flex items-center gap-3 mb-2">
                          <div className={`p-2 rounded-lg ${
                            selectedTemplate === template.id
                              ? 'bg-blue-500 text-white'
                              : 'bg-gray-100 text-gray-600'
                          }`}>
                            {template.icon}
                          </div>
                          <span className="font-medium text-gray-900">{template.title}</span>
                        </div>
                        <p className="text-xs text-gray-500 text-left">点击快速填充模板内容</p>
                      </button>
                    ))}
                  </div>
                </div>

                {/* 输入区域 */}
                <div className="mb-6">
                  <label className="block text-sm font-semibold text-gray-700 mb-2">
                    需求描述
                  </label>
                  <textarea
                    value={requirement}
                    onChange={(e) => setRequirement(e.target.value)}
                    placeholder="请输入展会的名称、规模、时间、地点、主题等信息..."
                    className="w-full h-48 px-4 py-3 border border-gray-300 rounded-xl text-gray-900 placeholder-gray-400 focus:ring-2 focus:ring-blue-500 focus:border-transparent resize-none transition-all"
                    style={{ fontFamily: 'system-ui, -apple-system, sans-serif' }}
                  />
                  <div className="mt-2 text-right">
                    <span className="text-xs text-gray-500">{requirement.length} 字符</span>
                  </div>
                </div>

                {/* 底部按钮 */}
                <div className="flex items-center justify-between">
                  <div className="text-sm text-gray-500">
                    <span className="inline-flex items-center gap-1">
                      <Sparkles className="w-4 h-4 text-yellow-500" />
                      AI将基于您的需求生成专业的展会策划方案
                    </span>
                  </div>
                  <div className="flex gap-3">
                    <button
                      onClick={onClose}
                      className="px-6 py-2.5 text-gray-700 bg-gray-100 hover:bg-gray-200 rounded-lg transition-colors font-medium"
                    >
                      取消
                    </button>
                    <button
                      onClick={handleSubmit}
                      disabled={!requirement.trim()}
                      className={`px-6 py-2.5 rounded-lg font-medium transition-all flex items-center gap-2 ${
                        requirement.trim()
                          ? 'bg-gradient-to-r from-blue-600 to-purple-600 text-white hover:shadow-lg transform hover:scale-105'
                          : 'bg-gray-300 text-gray-500 cursor-not-allowed'
                      }`}
                    >
                      <Zap className="w-4 h-4" />
                      开始生成方案
                    </button>
                  </div>
                </div>
              </div>
            </div>
          </motion.div>
        </>
      )}
    </AnimatePresence>
  );
};

export default RequirementModal;