import {BasicColumn} from '/@/components/Table';
import {FormSchema} from '/@/components/Table';
import { h } from 'vue';
import { Image } from 'ant-design-vue';
import { getInheritorOptions } from '/@/api/oroqen/OroqenHeritageInheritor';
import { getFileAccessHttpUrl } from '/@/utils/common/compUtils';

//列表数据
export const columns: BasicColumn[] = [
   {
    title: '项目名称',
    align:"left",
    dataIndex: 'projectName',
    ellipsis: true,
    fixed: 'left',
   },
   {
    title: '封面图片',
    align:"center",
    dataIndex: 'coverImage',
    customRender: ({ text, record }) => {
      if (!text) return '-';
      // 使用 getFileAccessHttpUrl 函数来正确处理图片URL
      const imageUrl = getFileAccessHttpUrl(text);
      
      return h(Image, {
        src: imageUrl,
        alt: record?.projectName || '封面图片',
        width: 60,
        height: 60,
        style: { objectFit: 'cover', borderRadius: '4px' },
        preview: true,
        fallback: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMIAAADDCAYAAADQvc6UAAABRWlDQ1BJQ0MgUHJvZmlsZQAAKJFjYGASSSwoyGFhYGDIzSspCnJ3UoiIjFJgf8LAwSDCIMogwMCcmFxc4BgQ4ANUwgCjUcG3awyMIPqyLsis7PPOq3QdDFcvjV3jOD1boQVTPQrgSkktTgbSf4A4LbmgqISBgTEFyFYuLykAsTuAbJEioKOA7DkgdjqEvQHEToKwj4DVhAQ5A9k3gGyB5IxEoBmML4BsnSQk8XQkNtReEOBxcfXxUQg1Mjc0dyHgXNJBSWpFCYh2zi+oLMpMzyhRcASGUqqCZ16yno6CkYGRAQMDKMwhqj/fAIcloxgHQqxAjIHBEugw5sUIsSQpBobtQPdLciLEVJYzMPBHMDBsayhILEqEO4DxG0txmrERhM29nYGBddr//5/DGRjYNRkY/l7////39v///y4Dmn+LgeHANwDrkl1AuO+pmgAAADhlWElmTU0AKgAAAAgAAYdpAAQAAAABAAAAGgAAAAAAAqACAAQAAAABAAAAwqADAAQAAAABAAAAwwAAAAD9b/HnAAAHlklEQVR4Ae3dP3Ik1RnG4W+FgYxN'
      });
    }
   },
   {
    title: '项目编码',
    align:"center",
    dataIndex: 'projectCode',
   },
   {
    title: '项目类别',
    align:"center",
    dataIndex: 'category',
    customRender: ({ text }) => {
      const categoryMap = {
        'TRADITIONAL_CRAFT': '传统技艺',
        'FOLK_CUSTOM': '民俗',
        'TRADITIONAL_MUSIC': '传统音乐',
        'TRADITIONAL_DANCE': '传统舞蹈',
        'TRADITIONAL_DRAMA': '传统戏剧',
        'QUYI': '曲艺',
        'TRADITIONAL_SPORTS': '传统体育',
        'TRADITIONAL_ART': '传统美术',
        'TRADITIONAL_MEDICINE': '传统医药',
        'LITERATURE': '文学'
      };
      return categoryMap[text] || text;
    }
   },
   {
    title: '非遗级别',
    align:"center",
    dataIndex: 'heritageLevel',
    customRender: ({ text }) => {
      const levelMap = {
        'NATIONAL': '国家级',
        'PROVINCIAL': '省级',
        'MUNICIPAL': '市级',
        'COUNTY': '县级'
      };
      const level = levelMap[text] || text;
      const colorMap = {
        '国家级': '#cf1322',
        '省级': '#fa541c',
        '市级': '#fa8c16',
        '县级': '#52c41a'
      };
      return h('span', {
        style: {
          color: colorMap[level] || '#666',
          fontWeight: '500'
        }
      }, level);
    }
   },
   {
    title: '批准年份',
    align:"center",
    dataIndex: 'approvalYear',
   },
   {
    title: '保护单位',
    align:"center",
    dataIndex: 'protectionUnit',
    ellipsis: true,
   },
   {
    title: '浏览次数',
    align:"center",
    dataIndex: 'viewCount',
   },
   {
    title: '状态',
    align:"center",
    dataIndex: 'status',
    customRender: ({ text }) => {
      return text === 1 ? 
        h('span', { style: { color: '#52c41a', fontWeight: '500' } }, '启用') : 
        h('span', { style: { color: '#ff4d4f', fontWeight: '500' } }, '禁用');
    }
   },
   {
    title: '推荐状态',
    align:"center",
    dataIndex: 'isRecommended',
    customRender: ({ text }) => {
      return text === 1 ? 
        h('span', { style: { color: '#1890ff', fontWeight: '500' } }, '已推荐') : 
        h('span', { style: { color: '#999', fontWeight: '500' } }, '未推荐');
    }
   },
];
//查询数据
export const searchFormSchema: FormSchema[] = [
  {
    label: '项目名称',
    field: 'projectName',
    component: 'Input',
    componentProps: {
      placeholder: '请输入项目名称',
    },
    colProps: { span: 6 },
  },
  {
    label: '项目编码',
    field: 'projectCode',
    component: 'Input',
    componentProps: {
      placeholder: '请输入项目编码',
    },
    colProps: { span: 6 },
  },
  {
    label: '项目类别',
    field: 'category',
    component: 'Select',
    componentProps: {
      placeholder: '请选择项目类别',
      options: [
        { label: '传统技艺', value: 'TRADITIONAL_CRAFT' },
        { label: '民俗', value: 'FOLK_CUSTOM' },
        { label: '传统音乐', value: 'TRADITIONAL_MUSIC' },
        { label: '传统舞蹈', value: 'TRADITIONAL_DANCE' },
        { label: '传统戏剧', value: 'TRADITIONAL_DRAMA' },
        { label: '曲艺', value: 'QUYI' },
        { label: '传统体育', value: 'TRADITIONAL_SPORTS' },
        { label: '传统美术', value: 'TRADITIONAL_ART' },
        { label: '传统医药', value: 'TRADITIONAL_MEDICINE' },
        { label: '文学', value: 'LITERATURE' },
      ]
    },
    colProps: { span: 6 },
  },
  {
    label: '非遗级别',
    field: 'heritageLevel',
    component: 'Select',
    componentProps: {
      placeholder: '请选择非遗级别',
      options: [
        { label: '国家级', value: 'NATIONAL' },
        { label: '省级', value: 'PROVINCIAL' },
        { label: '市级', value: 'MUNICIPAL' },
        { label: '县级', value: 'COUNTY' },
      ]
    },
    colProps: { span: 6 },
  },
  {
    label: '保护单位',
    field: 'protectionUnit',
    component: 'Input',
    componentProps: {
      placeholder: '请输入保护单位',
    },
    colProps: { span: 6 },
  },
  {
    label: '状态',
    field: 'status',
    component: 'Select',
    componentProps: {
      placeholder: '请选择状态',
      options: [
        { label: '启用', value: 1 },
        { label: '禁用', value: 0 },
      ]
    },
    colProps: { span: 6 },
  },
];
//表单数据
export const formSchema: FormSchema[] = [
  {
    label: '项目名称',
    field: 'projectName',
    component: 'Input',
    componentProps: {
      placeholder: '请输入项目名称',
    },
    dynamicRules: ({model,schema}) => {
      return [
        { required: true, message: '请输入项目名称!'},
      ];
    },
  },
  {
    label: '项目编码',
    field: 'projectCode',
    component: 'Input',
    componentProps: {
      placeholder: '请输入项目编码',
    },
    dynamicRules: ({model,schema}) => {
      return [
        { required: true, message: '请输入项目编码!'},
      ];
    },
  },
  {
    label: '项目类别',
    field: 'category',
    component: 'Select',
    componentProps: {
      placeholder: '请选择项目类别',
      options: [
        { label: '传统技艺', value: 'TRADITIONAL_CRAFT' },
        { label: '民俗', value: 'FOLK_CUSTOM' },
        { label: '传统音乐', value: 'TRADITIONAL_MUSIC' },
        { label: '传统舞蹈', value: 'TRADITIONAL_DANCE' },
        { label: '传统戏剧', value: 'TRADITIONAL_DRAMA' },
        { label: '曲艺', value: 'QUYI' },
        { label: '传统体育', value: 'TRADITIONAL_SPORTS' },
        { label: '传统美术', value: 'TRADITIONAL_ART' },
        { label: '传统医药', value: 'TRADITIONAL_MEDICINE' },
        { label: '文学', value: 'LITERATURE' },
      ]
    },
    dynamicRules: ({model,schema}) => {
      return [
        { required: true, message: '请选择项目类别!'},
      ];
    },
  },
  {
    label: '非遗级别',
    field: 'heritageLevel',
    component: 'Select',
    componentProps: {
      placeholder: '请选择非遗级别',
      options: [
        { label: '国家级', value: 'NATIONAL' },
        { label: '省级', value: 'PROVINCIAL' },
        { label: '市级', value: 'MUNICIPAL' },
        { label: '县级', value: 'COUNTY' },
      ]
    },
    dynamicRules: ({model,schema}) => {
      return [
        { required: true, message: '请选择非遗级别!'},
      ];
    },
  },
  {
    label: '批准年份',
    field: 'approvalYear',
    component: 'InputNumber',
    componentProps: {
      placeholder: '请输入批准年份',
      min: 1900,
      max: new Date().getFullYear(),
    },
  },
  {
    label: '批准文号',
    field: 'approvalNumber',
    component: 'Input',
    componentProps: {
      placeholder: '请输入批准文号',
    },
  },
  {
    label: '项目描述',
    field: 'description',
    component: 'InputTextArea',
    componentProps: {
      placeholder: '请输入项目描述',
      rows: 3,
      maxlength: 500,
      showCount: true,
    },
    dynamicRules: ({model,schema}) => {
      return [
        { required: true, message: '请输入项目描述!'},
      ];
    },
  },
  {
    label: '详细描述',
    field: 'fullDescription',
    component: 'InputTextArea',
    componentProps: {
      placeholder: '请输入详细描述',
      rows: 4,
      maxlength: 2000,
      showCount: true,
    },
  },
  {
    label: '历史渊源',
    field: 'history',
    component: 'InputTextArea',
    componentProps: {
      placeholder: '请输入历史渊源',
      rows: 4,
      maxlength: 2000,
      showCount: true,
    },
  },
  {
    label: '技艺特点',
    field: 'features',
    component: 'InputTextArea',
    componentProps: {
      placeholder: '请输入技艺特点',
      rows: 4,
      maxlength: 2000,
      showCount: true,
    },
  },
  {
    label: '制作流程',
    field: 'craftProcess',
    component: 'InputTextArea',
    componentProps: {
      placeholder: '请输入制作流程，多个步骤用逗号分隔，或输入JSON数组格式',
      rows: 4,
      maxlength: 2000,
      showCount: true,
    },
    helpMessage: '可以输入逗号分隔的步骤（如：步骤1,步骤2,步骤3）或JSON数组格式（如：["步骤1","步骤2","步骤3"]）',
  },
  {
    label: '传承价值',
    field: 'value',
    component: 'InputTextArea',
    componentProps: {
      placeholder: '请输入传承价值',
      rows: 4,
      maxlength: 2000,
      showCount: true,
    },
  },
  {
    label: '封面图片',
    field: 'coverImage',
    component: 'JUpload',
    componentProps: {
      fileType: 'image',
      maxCount: 1,
      maxSize: 5,
      bizPath: 'heritage-project/cover',
      customRequest: (options) => {
        // 验证文件类型和大小
        const { file } = options;
        const isImage = file.type.startsWith('image/');
        if (!isImage) {
          console.error('只能上传图片文件!');
          return;
        }
        const isLt5M = file.size / 1024 / 1024 < 5;
        if (!isLt5M) {
          console.error('图片大小不能超过5MB!');
          return;
        }
        
        // 创建本地预览URL
        const localUrl = URL.createObjectURL(file);
        
        // 直接设置文件的url为本地预览URL
        file.url = localUrl;
        
        // 模拟上传成功响应，但不返回local:前缀，避免被解析
        setTimeout(() => {
          options.onSuccess({
            success: true,
            message: file.name, // 仅使用文件名，不使用local:前缀
          }, file);
        }, 100);
      },
    },
    colProps: { lg: 12, md: 12 },
  },
  {
    label: '图片集',
    field: 'images',
    component: 'JUpload',
    componentProps: {
      fileType: 'image',
      maxCount: 10,
      maxSize: 5,
      bizPath: 'heritage-project/images',
      customRequest: (options) => {
        // 验证文件类型和大小
        const { file } = options;
        const isImage = file.type.startsWith('image/');
        if (!isImage) {
          console.error('只能上传图片文件!');
          return;
        }
        const isLt5M = file.size / 1024 / 1024 < 5;
        if (!isLt5M) {
          console.error('图片大小不能超过5MB!');
          return;
        }
        
        // 创建本地预览URL
        const localUrl = URL.createObjectURL(file);
        
        // 直接设置文件的url为本地预览URL
        file.url = localUrl;
        
        // 模拟上传成功响应，但不返回local:前缀，避免被解析
        setTimeout(() => {
          options.onSuccess({
            success: true,
            message: file.name, // 仅使用文件名，不使用local:前缀
          }, file);
        }, 100);
      },
    },
    colProps: { lg: 12, md: 12 },
  },
  {
    label: '视频链接',
    field: 'videoUrl',
    component: 'JUpload',
    componentProps: {
      fileType: 'video',
      maxCount: 1,
      maxSize: 50,
      bizPath: 'heritage-project/video',
      customRequest: (options) => {
        // 验证文件类型和大小
        const { file } = options;
        const isVideo = file.type.startsWith('video/');
        if (!isVideo) {
          console.error('只能上传视频文件!');
          return;
        }
        const isLt50M = file.size / 1024 / 1024 < 50;
        if (!isLt50M) {
          console.error('视频大小不能超过50MB!');
          return;
        }
        
        // 创建本地预览URL
        const localUrl = URL.createObjectURL(file);
        
        // 直接设置文件的url为本地预览URL
        file.url = localUrl;
        
        // 模拟上传成功响应，但不返回local:前缀，避免被解析
        setTimeout(() => {
          options.onSuccess({
            success: true,
            message: file.name, // 仅使用文件名，不使用local:前缀
          }, file);
        }, 100);
      },
    },
    colProps: { lg: 24, md: 24 },
  },
  // {
  //   label: '音频链接',
  //   field: 'audioUrl',
  //   component: 'JUpload',
  //   componentProps: {
  //     fileType: 'audio',
  //     maxCount: 1,
  //     maxSize: 20,
  //     bizPath: 'heritage-project/audio',
  //   },
  // },
  {
    label: '标签',
    field: 'tags',
    component: 'Input',
    componentProps: {
      placeholder: '请输入标签，多个标签用逗号分隔',
    },
  },
  {
    label: '关联传承人',
    field: 'inheritorIds',
    component: 'ApiSelect',
    componentProps: {
      placeholder: '请选择关联传承人',
      mode: 'multiple',
      allowClear: true,
      showSearch: true,
      api: getInheritorOptions,
      immediate: true,
      resultField: '',
      labelField: 'label',
      valueField: 'value',
    },
  },
  {
    label: '保护单位',
    field: 'protectionUnit',
    component: 'Input',
    componentProps: {
      placeholder: '请输入保护单位',
    },
  },
  {
    label: '保护措施',
    field: 'protectionMeasures',
    component: 'InputTextArea',
    componentProps: {
      placeholder: '请输入保护措施',
      rows: 3,
      maxlength: 1000,
      showCount: true,
    },
  },
  {
    label: '保护现状',
    field: 'currentStatus',
    component: 'InputTextArea',
    componentProps: {
      placeholder: '请输入保护现状',
      rows: 3,
      maxlength: 1000,
      showCount: true,
    },
  },
  {
    label: '状态',
    field: 'status',
    component: 'RadioGroup',
    componentProps: {
      options: [
        { label: '禁用', value: 0 },
        { label: '启用', value: 1 },
      ]
    },
    defaultValue: 1,
    ifShow: ({ values }) => {
      // 新增时隐藏状态字段，编辑时显示
      return !!values.id;
    },
  },
  // TODO 主键隐藏字段，目前写死为ID
  {
    label: '',
    field: 'id',
    component: 'Input',
    show: false
  },
];

// 高级查询数据
export const superQuerySchema = {
  projectName: {title: '项目名称',order: 0,view: 'text', type: 'string',},
  projectCode: {title: '项目编码',order: 1,view: 'text', type: 'string',},
  category: {title: '项目类别',order: 2,view: 'text', type: 'string',},
  heritageLevel: {title: '非遗级别',order: 3,view: 'text', type: 'string',},
  approvalYear: {title: '批准年份',order: 4,view: 'number', type: 'number',},
  approvalNumber: {title: '批准文号',order: 5,view: 'text', type: 'string',},
  description: {title: '项目描述',order: 6,view: 'text', type: 'string',},
  fullDescription: {title: '详细描述',order: 7,view: 'textarea', type: 'string',},
  history: {title: '历史渊源',order: 8,view: 'textarea', type: 'string',},
  features: {title: '技艺特点',order: 9,view: 'textarea', type: 'string',},
  craftProcess: {title: '制作流程',order: 10,view: 'textarea', type: 'string',},
  value: {title: '传承价值',order: 11,view: 'textarea', type: 'string',},
  coverImage: {title: '封面图片',order: 12,view: 'text', type: 'string',},
  images: {title: '图片集',order: 13,view: 'textarea', type: 'string',},
  videoUrl: {title: '视频链接',order: 14,view: 'text', type: 'string',},
  audioUrl: {title: '音频链接',order: 15,view: 'text', type: 'string',},
  tags: {title: '标签',order: 16,view: 'text', type: 'string',},
  inheritorIds: {title: '关联传承人ID',order: 17,view: 'textarea', type: 'string',},
  protectionUnit: {title: '保护单位',order: 18,view: 'text', type: 'string',},
  protectionMeasures: {title: '保护措施',order: 19,view: 'text', type: 'string',},
  currentStatus: {title: '保护现状',order: 20,view: 'text', type: 'string',},
  isRecommended: {title: '是否推荐',order: 21,view: 'number', type: 'number',},
  viewCount: {title: '浏览次数',order: 22,view: 'number', type: 'number',},
  likeCount: {title: '点赞次数',order: 23,view: 'number', type: 'number',},
  status: {title: '状态',order: 24,view: 'number', type: 'number',},
};

/**
* 流程表单调用这个方法获取formSchema
* @param param
*/
export function getBpmFormSchema(_formData): FormSchema[]{
  // 默认和原始表单保持一致 如果流程中配置了权限数据，这里需要单独处理formSchema
  return formSchema;
}