import React from 'react';
import { Card, Typography } from 'antd';
import { useNavigate, useSearchParams,useParams } from 'react-router-dom';
import QuotationTemplate from './components/QuotationTemplate';
import ProjectTemplate from './components/ProjectTemplate';
import TaskTemplate from './components/TaskTemplate';

const { Title } = Typography;

// 模板类型配置
const TEMPLATE_CONFIG = {
  quotation: {
    title: '报价单模板',
    component: QuotationTemplate,
  },
  project: {
    title: '专案模板',
    component: ProjectTemplate,
  },
  task: {
    title: '任务模板',
    component: TaskTemplate,
  }
};

const ServiceForm = () => {
  const navigate = useNavigate();
  const [searchParams] = useSearchParams();
  const type = searchParams.get('type') || 'quotation';
  const { id } = useParams();
  const isView = searchParams.get('isView') === 'true';

  const currentTemplate = TEMPLATE_CONFIG[type];
  const TemplateComponent = currentTemplate?.component;

  if (!currentTemplate) {
    return <div>无效的模板类型</div>;
  }

  return (
    <div className="bg-gradient-to-b from-gray-50 to-white dark:from-gray-800 dark:to-gray-900/90 min-h-screen p-2" >
      <Card
        className="shadow-lg rounded-lg border-0"
        title={
          <div className="flex justify-between items-center py-2">
            <div className="flex items-center space-x-3">
              <Title level={4} className="mb-0 text-gray-800 dark:text-gray-200">
                {id ? (isView ? "查看" : "编辑") : "新建"}{currentTemplate.title}
              </Title>
              <span className="text-gray-400 text-sm">
                {id
                  ? isView
                    ? `${currentTemplate.title}详情`
                    : `请修改${currentTemplate.title}信息`
                  : `请填写${currentTemplate.title}信息`}
              </span>
            </div>
          </div>
        }
      >
        <TemplateComponent
          id={id}
          isView={isView}
          onCancel={() => navigate("/company/serviceTemplate")}
        />
      </Card>
    </div>
  );
};

export default ServiceForm;