import { BasicColumn } from '/@/components/Table';
import { h } from 'vue';
import { StatusColorEnum } from '/@/enums/jeecgEnum';

const riskMap = {
  '1': '低风险',
  '2': '一般风险',
  '3': '较高风险',
  '4': '高风险',
};
const linkMap = {
  '-1': '断线',
  '0': '正常',
  '1': '超限报警',
  '2': '断电报警',
  '4': '标校',
  '5': '标校报警',
  '8': '超量程',
  '16': '分站故障',
  '32': '不巡检',
  '64': '暂停',
  '128': '传感器故障',
};
// 实时数据相关
export const columns: BasicColumn[] = [
  // {
  //   title: '序号',
  //   dataIndex: 'orderNo',
  //   width: 60,
  // },
  // {
  //   title: '区域',
  //   dataIndex: 'coalSeamName',
  //   width: 80,
  // },
  {
    title: '矿井名称',
    dataIndex: 'mineName',
    width: 200,
    fixed: true,
  },
  {
    title: '老空区永久密闭名称',
    dataIndex: 'devicePos',
    width: 200,
    fixed: true,
  },
  {
    title: '所属煤层',
    dataIndex: 'coalSeamName',
    width: 100,
    fixed: true,
  },
  {
    title: '自燃倾向性',
    dataIndex: 'coalSeamLevel',
    width: 100,
  },
  {
    title: '瓦斯等级',
    dataIndex: 'gasLevelName',
    width: 100,
  },
  {
    title: '生产状态',
    dataIndex: 'gjMineStatus',
    width: 100,
    // customRender({ text }) {
    //   // 从动态映射取值，兜底未知状态
    //   const label = dictMap.find((item) => item.value == text)?.label || '-';
    //   // 渲染带颜色的文字
    //   return h(
    //     'span',
    //     {
    //       color: get({ '0': StatusColorEnum.green, '1': StatusColorEnum.blue }, text),
    //     },
    //     label
    //   );
    // },
  },
  {
    title: '预警等级',
    dataIndex: 'alarmLevel',
    width: 100,
    customRender({ record }) {
      return alarmCellRender(record, (r) => riskMap[r.alarmLevel] || r.alarmLevel);
    },
  },
  {
    title: '闭内自燃发火隐患等级',
    dataIndex: 'fireAlarm',
    width: 100,
    customRender({ record }) {
      return alarmCellRender(record.fireAlarm);
    },
  },
  {
    title: '闭外自燃发火隐患等级',
    dataIndex: 'fireAlarmOut',
    width: 100,
    customRender({ record }) {
      return alarmCellRender(record.fireAlarmOut);
    },
  },
  {
    title: '爆炸预警等级',
    dataIndex: 'alarmLevel',
    width: 100,
    customRender({ record }) {
      return alarmCellRender(record.explosionAlarm);
    },
  },
  {
    title: '压差隐患等级',
    dataIndex: 'sourcePressureAlarm',
    width: 100,
    customRender({ record }) {
      return alarmCellRender(record.sourcePressureAlarm);
    },
  },
  {
    title: '是否漏风',
    dataIndex: 'leakageAlarm',
    width: 100,
    customRender({ record }) {
      // return alarmTextRender(record.leakageAlarm?.alarmLevel, record.leakageAlarm?.split('-')[0]);
      return alarmCellRender(record.leakageAlarm, (r) => r.alarmName.split('-')[1]);
    },
  },
  {
    title: '密闭启封判定',
    dataIndex: 'unsealAlarm',
    width: 100,
    customRender({ record }) {
      return alarmCellRender(record.unsealAlarm);
    },
  },
  {
    title: '设备状态',
    dataIndex: 'status',
    width: 100,
    customRender({ record }) {
      return alarmCellRender(
        String(record.status),
        (r) => linkMap[r],
        () => ''
      );
    },
  },
  {
    title: '压差(Pa)',
    dataIndex: 'sourcePressure',
    width: 100,
  },
  {
    title: 'CO(ppm)',
    width: 100,
    children: [
      {
        title: '闭内',
        dataIndex: 'coVal',
        width: 100,
      },
      {
        title: '闭外',
        dataIndex: 'coValOut',
        width: 100,
      },
    ],
  },
  {
    title: 'CO2(%)',
    width: 100,
    children: [
      {
        title: '闭内',
        dataIndex: 'co2Val',
        width: 100,
      },
      {
        title: '闭外',
        dataIndex: 'co2ValOut',
        width: 100,
      },
    ],
  },
  {
    title: 'CH4(%)',
    width: 100,
    children: [
      {
        title: '闭内',
        dataIndex: 'ch4Val',
        width: 100,
      },
      {
        title: '闭外',
        dataIndex: 'ch4ValOut',
        width: 100,
      },
    ],
  },
  {
    title: 'C2H2(ppm)',
    width: 100,
    children: [
      {
        title: '闭内',
        dataIndex: 'c2h2Val',
        width: 100,
      },
      {
        title: '闭外',
        dataIndex: 'c2h2ValOut',
        width: 100,
      },
    ],
  },
  {
    title: 'C2H4(ppm)',
    width: 100,
    children: [
      {
        title: '闭内',
        dataIndex: 'c2h4Val',
        width: 100,
      },
      {
        title: '闭外',
        dataIndex: 'c2h4ValOut',
        width: 100,
      },
    ],
  },
  {
    title: 'O2(%)',
    width: 100,
    children: [
      {
        title: '闭内',
        dataIndex: 'o2Val',
        width: 100,
      },
      {
        title: '闭外',
        dataIndex: 'o2ValOut',
        width: 100,
      },
    ],
  },
  {
    title: '温度(℃)',
    width: 100,
    children: [
      {
        title: '闭内',
        dataIndex: 'temperature',
        width: 100,
      },
      {
        title: '闭外',
        dataIndex: 'temperatureOut',
        width: 100,
      },
    ],
  },
  // {
  //   title: '爆炸危险性',
  //   dataIndex: 'explosionHazard',
  //   width: 80,
  // },
  {
    title: '更新时间',
    dataIndex: 'readTime',
    width: 100,
  },
];

function getTagColor(level: string) {
  switch (level) {
    case '4':
      return StatusColorEnum.red;
    case '3':
      return StatusColorEnum.gold;
    case '2':
      return StatusColorEnum.yellow;
    case '1':
      return StatusColorEnum.blue;
    case '0':
      return StatusColorEnum.red;
    case '-1':
      return StatusColorEnum.red;
    default:
      return 'inhert';
  }
}

export function alarmCellRender(
  record: any,
  valFn: (record: any) => string = (r) => r.alarmName,
  lvFn: (record: any) => string = (r) => r.alarmLevel
) {
  // if (!record) return h(Tag, { bordered: false, color: 'default' }, () => '-');
  // return h(Tag, { bordered: false, color: getTagColor(record.alarmLevel) }, () => record.alarmName);
  if (!record) return h('span', '-');
  const val = valFn(record);
  const lv = lvFn(record);
  return h('span', { style: { color: getTagColor(lv) } }, val);
}
