/**
 * 表单设计器组件配置
 */

import React from 'react';
import {
  FormOutlined,
  FontSizeOutlined,
  NumberOutlined,
  DownOutlined,
  CheckCircleOutlined,
  CheckSquareOutlined,
  CalendarOutlined,
  ClockCircleOutlined,
  FieldTimeOutlined,
  SwitcherOutlined,
  SlidersOutlined,
  StarOutlined,
  UploadOutlined,
  ApartmentOutlined,
  MinusOutlined,
  FileTextOutlined,
  BorderOutlined,
} from '@ant-design/icons';
import type { FieldType, FieldConfig } from './types';

// 🔌 属性配置组件 Props
export interface PropertyConfigProps {
  field: FieldConfig;
  onChange: (field: FieldConfig) => void;
}

// 🔌 字段渲染组件 Props
export interface FieldRendererProps {
  field: FieldConfig;
  value?: any;
  onChange?: (value: any) => void;
  isPreview?: boolean;
}

// 🔌 组件元数据（支持插件式扩展）
export interface ComponentMeta {
  type: FieldType;
  label: string;
  icon: any;
  category: '基础字段' | '高级字段' | '布局字段';
  defaultConfig: Partial<FieldConfig>;
  
  // 🆕 插件式扩展：自定义属性配置组件
  PropertyConfigComponent?: React.FC<PropertyConfigProps>;
  
  // 🆕 插件式扩展：自定义字段渲染组件
  FieldRendererComponent?: React.FC<FieldRendererProps>;
}

// 组件列表配置
export const COMPONENT_LIST: ComponentMeta[] = [
  // 布局字段
  {
    type: 'grid',
    label: '栅格布局',
    icon: BorderOutlined,
    category: '布局字段',
    defaultConfig: {
      columns: 2,
      columnSpans: [12, 12], // 默认两列，各占12格
      gutter: 16,
      children: [[], []], // 初始化两列空数组
    },
  },
  {
    type: 'divider',
    label: '分割线',
    icon: MinusOutlined,
    category: '布局字段',
    defaultConfig: {},
  },
  // 基础字段
  {
    type: 'input',
    label: '单行文本',
    icon: FormOutlined,
    category: '基础字段',
    defaultConfig: {
      placeholder: '请输入',
    },
  },
  {
    type: 'textarea',
    label: '多行文本',
    icon: FontSizeOutlined,
    category: '基础字段',
    defaultConfig: {
      placeholder: '请输入',
      rows: 4,
    },
  },
  {
    type: 'number',
    label: '数字输入',
    icon: NumberOutlined,
    category: '基础字段',
    defaultConfig: {
      placeholder: '请输入数字',
    },
  },
  {
    type: 'select',
    label: '下拉选择',
    icon: DownOutlined,
    category: '基础字段',
    defaultConfig: {
      placeholder: '请选择',
      dataSourceType: 'static',
      options: [
        { label: '选项1', value: '1' },
        { label: '选项2', value: '2' },
        { label: '选项3', value: '3' },
      ],
    },
  },
  {
    type: 'radio',
    label: '单选框组',
    icon: CheckCircleOutlined,
    category: '基础字段',
    defaultConfig: {
      dataSourceType: 'static',
      options: [
        { label: '选项1', value: '1' },
        { label: '选项2', value: '2' },
        { label: '选项3', value: '3' },
      ],
    },
  },
  {
    type: 'checkbox',
    label: '多选框组',
    icon: CheckSquareOutlined,
    category: '基础字段',
    defaultConfig: {
      dataSourceType: 'static',
      checkboxLayout: 'horizontal', // 默认行内布局
      options: [
        { label: '选项1', value: '1' },
        { label: '选项2', value: '2' },
        { label: '选项3', value: '3' },
      ],
    },
  },
  {
    type: 'date',
    label: '日期选择',
    icon: CalendarOutlined,
    category: '基础字段',
    defaultConfig: {
      placeholder: '请选择日期',
    },
  },
  {
    type: 'datetime',
    label: '日期时间',
    icon: FieldTimeOutlined,
    category: '基础字段',
    defaultConfig: {
      placeholder: '请选择日期时间',
      showTime: true,
    },
  },
  {
    type: 'time',
    label: '时间选择',
    icon: ClockCircleOutlined,
    category: '基础字段',
    defaultConfig: {
      placeholder: '请选择时间',
    },
  },
  {
    type: 'text',
    label: '文字',
    icon: FileTextOutlined,
    category: '基础字段',
    defaultConfig: {
      content: '这是一段文字',
      textAlign: 'left',  // 默认左对齐
      fontSize: 14,       // 默认字体大小 14px
      textColor: '#000000', // 默认黑色
    },
  },
  // 高级字段
  {
    type: 'switch',
    label: '开关',
    icon: SwitcherOutlined,
    category: '高级字段',
    defaultConfig: {},
  },
  {
    type: 'slider',
    label: '滑块',
    icon: SlidersOutlined,
    category: '高级字段',
    defaultConfig: {
      min: 0,
      max: 100,
    },
  },
  {
    type: 'rate',
    label: '评分',
    icon: StarOutlined,
    category: '高级字段',
    defaultConfig: {},
  },
  {
    type: 'upload',
    label: '文件上传',
    icon: UploadOutlined,
    category: '高级字段',
    defaultConfig: {
      maxCount: 1,
    },
  },
  {
    type: 'cascader',
    label: '级联选择',
    icon: ApartmentOutlined,
    category: '高级字段',
    defaultConfig: {
      placeholder: '请选择',
      dataSourceType: 'static',
      cascadeOptions: [
        {
          label: '浙江',
          value: 'zhejiang',
          children: [
            { label: '杭州', value: 'hangzhou' },
            { label: '宁波', value: 'ningbo' },
          ],
        },
        {
          label: '江苏',
          value: 'jiangsu',
          children: [
            { label: '南京', value: 'nanjing' },
            { label: '苏州', value: 'suzhou' },
          ],
        },
      ],
    },
  },
];

// 根据分类分组组件
export const getComponentsByCategory = () => {
  const grouped: Record<string, ComponentMeta[]> = {};
  
  COMPONENT_LIST.forEach((comp) => {
    if (!grouped[comp.category]) {
      grouped[comp.category] = [];
    }
    grouped[comp.category].push(comp);
  });
  
  return grouped;
};

