import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
import { rules } from '/@/utils/helper/validator';
import { render } from '/@/utils/common/renderUtils';
import { getFileAccessHttpUrl } from '/@/utils/common/compUtils';

// 列表页面公共参数、方法
export const columns: BasicColumn[] = [
  {
    title: '分类名称',
    align: 'left',
    dataIndex: 'categoryName',
    width: 200,
  },
  {
    title: '分类编码',
    align: 'center',
    dataIndex: 'categoryCode',
    width: 150,
  },
  {
    title: '分类图标',
    align: 'center',
    dataIndex: 'icon',
    width: 100,
    customRender: ({ text }) => {
      if (!text) return '';
      const imgUrl = getFileAccessHttpUrl(text);
      return render.renderImage(imgUrl, {
        style: { width: '40px', height: '40px', objectFit: 'cover' },
        preview: true,
        fallback: '/src/assets/images/no-image.png'
      });
    },
  },
  {
    title: '排序',
    align: 'center',
    dataIndex: 'sortOrder',
    width: 80,
  },
  {
    title: '状态',
    align: 'center',
    dataIndex: 'status',
    width: 100,
    customRender: ({ text }) => {
      const statusMap = {
        '1': { text: '启用', color: 'green' },
        '0': { text: '禁用', color: 'red' }
      };
      const status = statusMap[text] || { text: '未知', color: 'gray' };
      return render.renderTag(status.text, status.color);
    },
  },
  {
    title: '描述',
    align: 'center',
    dataIndex: 'description',
    width: 200,
    ellipsis: true,
  },
  {
    title: '创建时间',
    align: 'center',
    dataIndex: 'createTime',
    width: 150,
  },
];

// 查询数据
export const searchFormSchema: FormSchema[] = [
  {
    label: '分类名称',
    field: 'categoryName',
    component: 'Input',
    colProps: { span: 6 },
  },
  {
    label: '分类编码',
    field: 'categoryCode',
    component: 'Input',
    colProps: { span: 6 },
  },
  {
    label: '状态',
    field: 'status',
    component: 'JDictSelectTag',
    componentProps: {
      dictCode: 'valid_status',
      placeholder: '请选择状态',
    },
    colProps: { span: 6 },
  },
];

// 表单数据
export const formSchema: FormSchema[] = [
  {
    label: '',
    field: 'id',
    component: 'Input',
    show: false,
  },
  {
    label: '上级分类',
    field: 'parentId',
    component: 'JTreeSelect',
    componentProps: {
      dict: 'oroqen_product_category,category_name,id',
      pidField: 'parent_id',
      pidValue: '',
      hasChildField: 'has_child',
      placeholder: '请选择上级分类',
    },
  },
  {
    label: '分类名称',
    field: 'categoryName',
    component: 'Input',
    required: true,
    dynamicRules: ({ model, schema }) => {
      return [{ required: true, message: '请输入分类名称!' }];
    },
  },
  {
    label: '分类编码',
    field: 'categoryCode',
    component: 'Input',
    required: true,
    dynamicRules: ({ model, schema }) => {
      return [
        { required: true, message: '请输入分类编码!' },
        { pattern: /^[a-zA-Z0-9_]+$/, message: '编码只能包含字母、数字和下划线!' }
      ];
    },
  },
  {
    label: '分类图标',
    field: 'icon',
    component: 'JUpload',
    componentProps: {
      fileType: 'image',
      maxSize: 2,
      maxCount: 1,
      bizPath: 'product/category/icon',
      customRequest: (options) => {
        const { file, onSuccess, onError } = options;
        
        // 验证文件类型
        const isImage = file.type.startsWith('image/');
        if (!isImage) {
          onError(new Error('只能上传图片文件!'));
          return;
        }
        
        // 验证文件大小
        const isLt2M = file.size / 1024 / 1024 < 2;
        if (!isLt2M) {
          onError(new Error('图片大小不能超过2MB!'));
          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: 'sortOrder',
    component: 'InputNumber',
    defaultValue: 0,
    componentProps: {
      min: 0,
      placeholder: '请输入排序值',
    },
  },
  {
    label: '状态',
    field: 'status',
    component: 'JDictSelectTag',
    defaultValue: '1',
    componentProps: {
      dictCode: 'valid_status',
      placeholder: '请选择状态',
    },
  },
  {
    label: '描述',
    field: 'description',
    component: 'InputTextArea',
    componentProps: {
      rows: 3,
      placeholder: '请输入分类描述',
    },
  },
];