import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
import { rules } from '/@/utils/helper/validator';
import { render } from '/@/utils/common/renderUtils';
import { JVxeTypes, JVxeColumn } from '/@/components/jeecg/JVxeTable/types';
import { getFileAccessHttpUrl } from '/@/utils/common/compUtils';

// 列表页面公共参数、方法
export const columns: BasicColumn[] = [
  {
    title: '标题',
    align: 'center',
    dataIndex: 'title',
    width: 200,
  },
  {
    title: '分类',
    align: 'center',
    dataIndex: 'categoryName',
    width: 120,
  },
  {
    title: '封面图片',
    align: 'center',
    dataIndex: 'coverImage',
    width: 120,
    customRender: ({ text }) => {
      if (!text) return '';
      const imgUrl = getFileAccessHttpUrl(text);
      return render.renderImage(imgUrl, {
        style: { width: '60px', height: '40px', objectFit: 'cover' },
        preview: true,
        fallback: '/src/assets/images/no-image.png'
      });
    },
  },
  {
    title: '作者',
    align: 'center',
    dataIndex: 'author',
    width: 100,
  },
  {
    title: '发布状态',
    align: 'center',
    dataIndex: 'status',
    width: 100,
    customRender: ({ text }) => {
      const statusMap = {
        '0': { text: '草稿', color: 'orange' },
        '1': { text: '已发布', color: 'green' },
        '2': { text: '已下线', color: 'red' }
      };
      const status = statusMap[text] || { text: '未知', color: 'gray' };
      return render.renderTag(status.text, status.color);
    },
  },
  {
    title: '是否推荐',
    align: 'center',
    dataIndex: 'isRecommended',
    width: 100,
    customRender: ({ text }) => {
      return render.renderSwitch(text, [
        { text: '是', value: '1' },
        { text: '否', value: '0' }
      ]);
    },
  },
  {
    title: '浏览量',
    align: 'center',
    dataIndex: 'viewCount',
    width: 100,
  },
  {
    title: '点赞数',
    align: 'center',
    dataIndex: 'likeCount',
    width: 100,
  },
  {
    title: '创建时间',
    align: 'center',
    dataIndex: 'createTime',
    width: 150,
  },
];

// 查询数据
export const searchFormSchema: FormSchema[] = [
  {
    label: '标题',
    field: 'title',
    component: 'Input',
    colProps: { span: 6 },
  },
  {
    label: '分类',
    field: 'categoryId',
    component: 'JTreeSelect',
    componentProps: {
      dict: 'oroqen_culture_category,category_name,id',
      pidField: 'parent_id',
      pidValue: '0',
      hasChildField: 'has_child',
      placeholder: '请选择分类',
    },
    colProps: { span: 6 },
  },
  {
    label: '发布状态',
    field: 'status',
    component: 'JDictSelectTag',
    componentProps: {
      dictCode: 'content_status',
      placeholder: '请选择状态',
    },
    colProps: { span: 6 },
  },
  {
    label: '是否推荐',
    field: 'isRecommended',
    component: 'JDictSelectTag',
    componentProps: {
      dictCode: 'yes_no',
      placeholder: '请选择',
    },
    colProps: { span: 6 },
  },
];

// 表单数据
export const formSchema: FormSchema[] = [
  {
    label: '',
    field: 'id',
    component: 'Input',
    show: false,
  },
  {
    label: '标题',
    field: 'title',
    component: 'Input',
    required: true,
    dynamicRules: ({ model, schema }) => {
      return [{ required: true, message: '请输入标题!' }];
    },
  },
  {
    label: '分类',
    field: 'categoryId',
    component: 'JTreeSelect',
    required: true,
    componentProps: {
      dict: 'oroqen_culture_category,category_name,id',
      pidField: 'parent_id',
      pidValue: '0',
      hasChildField: 'has_child',
      placeholder: '请选择分类',
    },
    dynamicRules: ({ model, schema }) => {
      return [{ required: true, message: '请选择分类!' }];
    },
  },
  {
    label: '封面图片',
    field: 'coverImage',
    component: 'JUpload',
    componentProps: {
      fileType: 'image',
      maxSize: 5,
      maxCount: 1,
      bizPath: 'culture/cover',
      customRequest: (options) => {
        const { file, onSuccess, onError } = options;
        
        // 验证文件类型
        const isImage = file.type.startsWith('image/');
        if (!isImage) {
          onError(new Error('只能上传图片文件!'));
          return;
        }
        
        // 验证文件大小
        const isLt5M = file.size / 1024 / 1024 < 5;
        if (!isLt5M) {
          onError(new Error('图片大小不能超过5MB!'));
          return;
        }
        
        // 创建本地预览URL
        const blobUrl = URL.createObjectURL(file);
        
        // 模拟上传成功响应
        setTimeout(() => {
          onSuccess({
            status: 'done',
            url: blobUrl,
            message: blobUrl,
            response: {
              success: true,
              message: '上传成功',
              result: {
                url: blobUrl
              }
            }
          });
        }, 100);
      }
    },
  },
  {
    label: '作者',
    field: 'author',
    component: 'Input',
  },
  {
    label: '摘要',
    field: 'summary',
    component: 'InputTextArea',
    componentProps: {
      rows: 3,
      placeholder: '请输入内容摘要',
    },
  },
  {
    label: '内容',
    field: 'content',
    component: 'JEditor',
    required: true,
    componentProps: {
      height: 400,
      placeholder: '请输入内容',
    },
    dynamicRules: ({ model, schema }) => {
      return [{ required: true, message: '请输入内容!' }];
    },
  },
  {
    label: '发布状态',
    field: 'status',
    component: 'JDictSelectTag',
    defaultValue: '0',
    componentProps: {
      dictCode: 'content_status',
      placeholder: '请选择状态',
    },
  },
  {
    label: '是否推荐',
    field: 'isRecommended',
    component: 'JSwitch',
    defaultValue: '0',
    componentProps: {
      options: ['0', '1'],
    },
  },
  {
    label: '排序',
    field: 'sortOrder',
    component: 'InputNumber',
    defaultValue: 0,
    componentProps: {
      min: 0,
      placeholder: '请输入排序值',
    },
  },
  {
    label: '标签',
    field: 'tags',
    component: 'Input',
    componentProps: {
      placeholder: '多个标签用逗号分隔',
    },
  },
  {
    label: '关键词',
    field: 'keywords',
    component: 'Input',
    componentProps: {
      placeholder: '多个关键词用逗号分隔',
    },
  },
  {
    label: '备注',
    field: 'remark',
    component: 'InputTextArea',
    componentProps: {
      rows: 2,
      placeholder: '请输入备注',
    },
  },
];