import { BasicColumn, FormSchema } from '/@/components/Table';
import { h } from 'vue';
import { Tag } from 'ant-design-vue';

// 生产状态映射表（扩展所有状态）
const productionStatusMap: Record<string | number, { text: string; color: string }> = {
  0: { text: '拟建矿井', color: 'blue' },
  1: { text: '正常生产矿井', color: 'green' },
  2: { text: '正常建设煤矿', color: 'green' },
  3: { text: '自行停产矿井', color: 'green' },
  4: { text: '正在关闭', color: 'green' },
  5: { text: '责令停产整顿', color: 'green' },
  6: { text: '责令停止建设', color: 'green' },
  7: { text: '已关闭矿井', color: 'green' },
  8: { text: '长期停产矿井', color: 'green' },
  9: { text: '长期停建矿井', color: 'green' },
  10: { text: '停产整改矿井', color: 'green' },
  11: { text: '长期停建无法联系', color: 'green' },
  12: { text: '停建整改', color: 'green' },
  13: { text: '自行停建', color: 'green' },
  14: { text: '正在实施关闭', color: 'green' },
  15: { text: '已关闭矿井', color: 'green' },
};
// 解析queJson并拼接orderNum+queCon的辅助函数
const formatQueJson = (queJsonStr: string) => {
  // 空值处理
  if (!queJsonStr) return '无质量问题';
  try {
    const queList = JSON.parse(queJsonStr);
    // 非数组格式处理
    if (!Array.isArray(queList)) return '问题格式异常';
    // 拼接orderNum + queCon，多个问题用分号分隔
    return queList.map((item) => `${item.orderNum || ''}:${item.queCon || ''}`).join('; ');
  } catch (error) {
    console.error('解析质量问题JSON失败:', error);
    return '问题数据解析失败';
  }
};

// 通用空值处理函数（非在线状态的空值统一显示「—」）
const formatEmptyValue = (value: any) => {
  return value === null || value === undefined || value === '' || /^\s*$/.test(String(value));
};

/** 表格列配置 */
export const columns: BasicColumn[] = [
  {
    title: '煤矿名称',
    dataIndex: 'mineName',
    width: 250,
  },
  {
    title: '煤矿简称',
    dataIndex: 'mineNameAbbr',
    width: 150,
  },
  {
    title: '生产状态',
    dataIndex: 'mineProStatus',
    width: 120,
    customRender: ({ record }) => {
      // 空值处理
      // 状态映射 + 标签渲染
      const status = String(record.mineProStatus);
      const { text, color } = productionStatusMap[status] || { text: '未知状态', color: 'gray' };
      return h(Tag, { color }, () => text);
    },
  },
  {
    title: '在线状态',
    dataIndex: 'mineLinkStatus',
    width: 100,
    customRender: ({ record }) => {
      // 未连接/空值 显示「/」
      // 状态映射 + 标签渲染
      const status = String(record.mineLinkStatus);
      const text = status === '1' ? '在线' : '离线';
      const color = status === '1' ? 'green' : 'red';
      return h(Tag, { color }, () => text);
    },
  },
  {
    title: '质量问题详情',
    dataIndex: 'queJson',
    width: 400,
    ellipsis: true,
    customRender: ({ record, text }) => {
      // 核心修改：使用辅助函数格式化显示文本
      const displayText = formatQueJson(text);
      return h('div', { style: { display: 'flex', alignItems: 'center', gap: '8px' } }, [
        h('span', { style: { flex: 1, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' } }, displayText),
        h(
          'button',
          {
            onClick: () => {
              record.onViewDetail?.(record);
            },
            title: '查看详情',
            style: {
              padding: '5px',
            },
          },
          [
            h('div', {
              style: {
                background: `url('/@/assets/images/basicInfo/dataQuality/icon-view.svg')`,
                width: '16px',
                height: '16px',
                backgroundSize: 'contain',
                backgroundRepeat: 'no-repeat',
              },
              class: 'action-icon',
              alt: '查看详情',
            }),
          ]
        ),
      ]);
    },
  },
  {
    title: '当前状态',
    dataIndex: 'isOk',
    width: 100,
  },
  {
    title: '处理时间',
    dataIndex: 'updateTime',
    width: 180,
  },
];

/** 查询表单配置 */
export const searchFormSchema: FormSchema[] = [
  {
    field: 'mineName',
    label: '煤矿名称',
    component: 'Input',
    colProps: { span: 6 },
    // groupName: '常规查询',
  },
  {
    field: 'mineNameAbbr',
    label: '煤矿简称',
    component: 'Input',
    colProps: { span: 6 },
    groupName: '常规查询',
  },
  {
    field: 'productionStatus',
    label: '生产状态',
    component: 'Select',
    componentProps: {
      options: [
        { label: '拟建矿井', value: '0' },
        { label: '正常生产矿井', value: '1' },
        { label: '长期停产矿井', value: '1' },
      ],
    },
    colProps: { span: 6 },
    groupName: '高级查询',
  },
  {
    field: 'isConnected',
    label: '是否需要接入',
    component: 'Select',
    componentProps: {
      options: [
        { label: '否', value: '0' },
        { label: '是', value: '1' },
      ],
    },
    colProps: { span: 6 },
    groupName: '高级查询',
  },

  {
    field: 'status',
    label: '在线状态',
    component: 'Select',
    componentProps: {
      options: [
        { label: '离线', value: '0' },
        { label: '在线', value: '1' },
      ],
    },
    colProps: { span: 6 },
    groupName: '高级查询',
  },
  {
    field: 'riskLevel',
    label: '风险等级',
    component: 'Select',
    componentProps: {
      options: [
        { label: 'Ⅰ类容易自燃', value: '0' },
        { label: 'Ⅱ类自燃', value: '1' },
        { label: 'Ⅲ类不易自燃', value: '2' },
      ],
    },
    colProps: { span: 6 },
    groupName: '高级查询',
  },
  {
    field: 'statusChange',
    label: '状态变化',
    component: 'Select',
    componentProps: {
      options: [
        { label: '否', value: '0' },
        { label: '是', value: '1' },
      ],
    },
    colProps: { span: 6 },
    groupName: '高级查询',
  },
];

// 模拟煤矿选项数据
export const mineOptions = [
  { label: '神东集团布尔台煤矿', value: '神东集团布尔台煤矿' },
  { label: '府谷县后安能源有限公司古城一号煤矿', value: '府谷县后安能源有限公司古城一号煤矿' },
];
// 模拟煤矿在线状态选项数据
export const mineLinkStatusOptions = [
  { label: '在线', value: '1' },
  { label: '离线', value: '0' },
];
export const topFormSchema = [
  {
    field: 'mineName',
    label: '煤矿名称',
    component: 'Select',
    componentProps: {
      options: mineOptions,
    },
    required: true,
  },
  {
    field: 'mineLinkStatus',
    label: '当前状态',
    component: 'Select',
    componentProps: {
      options: mineLinkStatusOptions,
    },
    required: true,
  },
];

/** 弹框表单配置 */
export const formSchema = [
  {
    field: 'goafName',
    label: '工作面名称',
    component: 'Input',
  },
  {
    field: 'queCon',
    label: '问题描述',
    component: 'Input',
  },
  {
    field: 'startTime',
    label: '开始时间',
    component: 'DatePicker',
    componentProps: {
      showTime: true,
      format: 'YYYY-MM-DD HH:mm:ss',
    },
    required: true,
  },
  {
    field: 'endTime',
    label: '结束时间',
    component: 'DatePicker',
    componentProps: {
      showTime: true,
      format: 'YYYY-MM-DD HH:mm:ss',
    },
    required: true,
  },
  {
    field: 'param',
    label: '参数',
    component: 'Input',
    required: true,
  },
];
