import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
import { h } from 'vue';

// 生产状态映射表（扩展所有状态）
const productionStatusMap: Record<string | number, { text: string; color: string }> = {
  0: { text: '正常生产矿井', color: 'green' },
  1: { text: '拟建矿井', color: 'blue' },
  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' },
};

// 颜色映射
const colorHexMap: Record<string, string> = {
  blue: '#1890ff',
  green: '#208840',
  gold: '#faad14',
  red: '#f5222d',
  gray: '#8c8c8c',
  black: '#000000',
};

export const columns: BasicColumn[] = [
  {
    title: '煤矿编号',
    dataIndex: 'mineCode',
  },
  {
    title: '所属执法处',
    dataIndex: 'managementName',
  },
  {
    title: '煤矿名称',
    dataIndex: 'mineName',
  },
  {
    title: '煤矿简称',
    dataIndex: 'mineNameAbbr',
  },
  {
    title: '生产状态',
    dataIndex: 'productionStatus',
    customRender: ({ record }) => {
      // 空值/异常值处理
      const status = String(record.productionStatus || '');
      const { text, color } = productionStatusMap[status] || { text: '未知状态', color: 'balck' };
      // 渲染普通span，仅设置文字颜色
      return h('span', { style: { color: colorHexMap[color] || colorHexMap.gray } }, text);
    },
  },
  {
    title: '自燃情况',
    dataIndex: 'alarmLevel',
  },
  {
    title: '接入状态',
    dataIndex: 'accessStatus',
    customRender: ({ record }) => {
      // 空值/异常值处理
      const status = String(record.accessStatus || '');
      if (!status || status === 'undefined' || status === 'null') {
        return h('span', { style: { color: colorHexMap.black } }, '/');
      }
      const text = status === '1' ? '接入' : '未接入';
      const textColor = status === '1' ? colorHexMap.green : colorHexMap.gold;
      return h('span', { style: { color: textColor } }, text);
    },
  },
  {
    title: '在线状态',
    dataIndex: 'status',
    customRender: ({ record }) => {
      const status = String(record.status || '');
      if (!status || status === 'undefined' || status === 'null') {
        return h('span', { style: { color: colorHexMap.black } }, '/');
      }
      const text = status === '1' ? '在线' : '离线';
      const textColor = status === '1' ? colorHexMap.green : colorHexMap.red;
      return h('span', { style: { color: textColor } }, text);
    },
  },
  {
    title: '应接数量',
    dataIndex: 'yingjieNum',
  },
  {
    title: '已接数量',
    dataIndex: 'accessGoafNum',
  },
  {
    title: '未接数量',
    dataIndex: 'notAccessGoafNum',
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'mineName',
    label: '煤矿名称',
    component: 'MineCascader',
    colProps: { span: 6 },
  },
  {
    field: 'mineNameAbbr',
    label: '煤矿简称',
    component: 'Input',
    colProps: { span: 6 },
  },
  {
    field: 'productionStatus',
    label: '生产状态',
    component: 'Select',
    componentProps: {
      options: [
        { label: '拟建矿井', value: '0' },
        { label: '正常生产矿井', value: '1' },
        { label: '长期停产矿井', value: '8' },
      ],
    },
    colProps: { span: 6 },
  },
  {
    field: 'accessStatus',
    label: '接入状态',
    component: 'Select',
    componentProps: {
      options: [
        { label: '接入', value: 1 },
        { label: '未接入', value: 0 },
      ],
    },
    colProps: { span: 6 },
  },
  {
    field: 'status',
    label: '在线状态',
    component: 'Select',
    componentProps: {
      options: [
        { label: '离线', value: '0' },
        { label: '在线', value: '1' },
      ],
    },
    colProps: { span: 6 },
  },
  {
    field: 'riskLevel',
    label: '风险等级',
    component: 'Select',
    componentProps: {
      options: [
        { label: 'Ⅰ类容易自燃', value: '0' },
        { label: 'Ⅱ类自燃', value: '1' },
        { label: 'Ⅲ类不易自燃', value: '2' },
      ],
    },
    colProps: { span: 6 },
  },
];
