import dayjs from 'dayjs';
import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
import { TreeItem } from '/@/components/Tree/index';
import { ModuleDataChart } from '/@/components/Configurable/types';
import { h } from 'vue';
import { StatusColorEnum } from '/@/enums/jeecgEnum';
import { map } from 'lodash-es';
import { DEFAULT_DISPLAY_MAP, DEFAULT_OPERAND_SET, evaluateExpressionLite } from '../../system/algorithm/utils';
import { Tag } from 'ant-design-vue';
// import { getDictItemsByCode } from '/@/utils/dict';
// import { get } from 'lodash-es';

const riskMap = {
  '1': '低风险',
  '2': '一般风险',
  '3': '较高风险',
  '4': '高风险',
};
const linkMap = {
  '-1': '断线',
  '0': '正常',
  '1': '超限报警',
  '2': '断电报警',
  '4': '标校',
  '5': '标校报警',
  '8': '超量程',
  '16': '分站故障',
  '32': '不巡检',
  '64': '暂停',
  '128': '传感器故障',
};

// const dictMap = getDictItemsByCode('mineProStatus');

// 实时数据相关
export const columns: BasicColumn[] = [
  // {
  //   title: '序号',
  //   dataIndex: 'orderNo',
  //   width: 60,
  // },
  // {
  //   title: '区域',
  //   dataIndex: 'coalSeamName',
  //   width: 80,
  // },
  {
    title: '煤矿名称',
    dataIndex: 'mineName',
    fixed: 'left',
    width: 200,
  },
  {
    title: '老空区永久密闭名称',
    dataIndex: 'devicePos',
    fixed: 'left',
    width: 200,
  },
  {
    title: '所属煤层',
    dataIndex: 'coalSeamName',
    fixed: 'left',
    width: 100,
  },
  {
    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,
  },
];

export const searchFormSchema: FormSchema[] = [
  // {
  //   field: 'mineNameAbbr',
  //   label: '煤矿简称',
  //   component: 'Input',
  //   colProps: { span: 6 },
  // },
  {
    field: 'gjMineStatus',
    label: '生产状态',
    component: 'JDictSelectTag',
    componentProps: {
      dictCode: 'mineProStatus',
      placeholder: '请选择生产状态',
    },
    colProps: { span: 6 },
  },
  {
    field: 'coalSeamLevel',
    label: '自燃倾向性',
    component: 'Select',
    componentProps: {
      options: [
        { label: 'Ⅰ类容易自燃', value: 'Ⅰ类容易自燃' },
        { label: 'Ⅱ类自燃', value: 'Ⅱ类自燃' },
        { label: 'Ⅲ类不易自燃', value: 'Ⅲ类不易自燃' },
      ],
    },
    colProps: { span: 6 },
  },
  {
    field: 'gasLevelName',
    label: '瓦斯等级',
    component: 'Select',
    componentProps: {
      options: [
        { label: '低瓦斯', value: '低瓦斯' },
        { label: '高瓦斯', value: '高瓦斯' },
        { label: '突出', value: '突出' },
      ],
    },
    colProps: { span: 6 },
  },
  {
    field: 'alarmLevel',
    label: '预警等级',
    component: 'Select',
    componentProps: {
      options: map(riskMap, (val, key) => ({ label: val, value: key })),
    },
    colProps: { span: 6 },
  },
  {
    field: 'status',
    label: '设备状态',
    component: 'Select',
    componentProps: {
      options: map(linkMap, (val, key) => ({ label: val, value: key })),
    },
    colProps: { span: 6 },
  },
];

interface ModalDetailsData {
  basicInfo: Record<string, any>[];
  board: Record<string, any>[];
  gasConcentrationConfig?: ModuleDataChart;
  pressureConfig?: ModuleDataChart;
  blastDeltaConfig?: Record<string, any>;
  boardOut: Record<string, any>[];
  gasConcentrationOutConfig?: ModuleDataChart;
  pressureOutConfig?: ModuleDataChart;
  blastDeltaOutConfig?: Record<string, any>;
}
export const modalDetailsData: ModalDetailsData = {
  basicInfo: [
    {
      label: '煤矿名称',
      value: 'mineName',
    },
    {
      label: '设备位置',
      value: 'devicePos',
    },
    {
      label: '预警等级',
      value: 'alarmLevel',
      customRender({ record }) {
        return alarmCellRender(record, (r) => riskMap[r.alarmLevel] || r.alarmLevel);
      },
    },
    {
      label: '密闭墙内自燃发火隐患等级',
      value: 'fireAlarm',
      customRender({ record }) {
        return alarmCellRender(record.fireAlarm);
      },
    },
    {
      label: '密闭墙外自燃发火隐患等级',
      value: 'fireAlarmOut',
      customRender({ record }) {
        return alarmCellRender(record.fireAlarmOut);
      },
    },
    {
      label: '爆炸预警等级',
      value: 'alarmLevel',
      customRender({ record }) {
        return alarmCellRender(record.explosionAlarm);
      },
    },
    {
      label: '压差隐患等级',
      value: 'sourcePressureAlarm',
      customRender({ record }) {
        return alarmCellRender(record.sourcePressureAlarm);
      },
    },
    {
      label: '是否漏风',
      value: 'leakageAlarm',
      customRender({ record }) {
        // return alarmTextRender(record.leakageAlarm?.alarmLevel, record.leakageAlarm?.split('-')[0]);
        return alarmCellRender(record.leakageAlarm, (r) => r.alarmName.split('-')[1]);
      },
    },
    {
      label: '密闭启封判定',
      value: 'unsealAlarm',
      customRender({ record }) {
        return alarmCellRender(record.unsealAlarm);
      },
    },
    {
      label: '内外压差变化预警原因',
      value: 'sourcePressure',
      style: 'grid-column: span 3',
      customRender({ record }) {
        const { coalSeamAlarmRule, sourcePressureAlarm } = record;
        if (!coalSeamAlarmRule || !sourcePressureAlarm) return h('span', '-');
        const result = evaluateExpressionLite(
          coalSeamAlarmRule[`ycWarn${sourcePressureAlarm.alarmLevel}`],
          ['sourcePressureChange'],
          DEFAULT_OPERAND_SET,
          DEFAULT_DISPLAY_MAP
        );

        return h(
          'span',
          result.map((r) => h(Tag, { color: 'warning' }, r))
        );
      },
    },
    {
      label: '密闭墙内自燃发火预警原因',
      value: 'sourcePressure',
      style: 'grid-column: span 3',
      customRender({ record }) {
        const { coalSeamAlarmRule, fireAlarm, fireAlarmReson } = record;
        if (!coalSeamAlarmRule || !fireAlarm) return h('span', '-');
        const result = evaluateExpressionLite(
          coalSeamAlarmRule[`fireWarn${fireAlarm.alarmLevel}`],
          fireAlarmReson,
          DEFAULT_OPERAND_SET,
          DEFAULT_DISPLAY_MAP
        );

        return h(
          'span',
          result.map((r) => h(Tag, { color: 'warning' }, r))
        );
      },
    },
    {
      label: '密闭墙外自燃发火预警原因',
      value: 'sourcePressure',
      style: 'grid-column: span 3',
      customRender({ record }) {
        const { coalSeamAlarmRule, fireAlarmOut, fireAlarmOutReson } = record;
        if (!coalSeamAlarmRule || !fireAlarmOut) return h('span', '-');

        const result = evaluateExpressionLite(
          coalSeamAlarmRule[`fireOutWarn${fireAlarmOut.alarmLevel}`],
          fireAlarmOutReson,
          DEFAULT_OPERAND_SET,
          DEFAULT_DISPLAY_MAP
        );

        return h(
          'span',
          result.map((r) => h(Tag, { color: 'warning' }, r))
        );
      },
    },
  ],
  board: [
    {
      label: 'CO(ppm)',
      value: 'coVal',
    },
    {
      label: 'CO2(%)',
      value: 'co2Val',
    },
    {
      label: 'O2(%)',
      value: 'o2Val',
    },
    {
      label: 'CH4(%)',
      value: 'ch4Val',
    },
    {
      label: 'C2H4(ppm)',
      value: 'c2h4Val',
    },
    {
      label: 'C2H2(ppm)',
      value: 'c2h2Val',
    },
    {
      label: '温度(℃)',
      value: 'temperature',
    },
    {
      label: '压差(Pa)',
      value: 'sourcePressure',
    },
  ],
  gasConcentrationConfig: {
    type: 'line_smooth', // 使用光滑曲线类型
    legend: { show: true },
    xAxis: [
      {
        show: true,
        name: '时间',
        axisLabel: { color: '#000000' },
      },
    ],
    yAxis: [
      {
        show: true,
        name: '浓度(%)',
        position: 'left',
        splitLine: { show: false },
        axisLine: {
          show: true,
          lineStyle: { color: '#333' },
        },
        axisLabel: { color: '#000000' },
        nameTextStyle: { color: '#000' },
      },
    ],
    series: [
      {
        label: '氧气浓度',
        readFrom: 'chartData',
        xprop: 'createTime',
        yprop: 'o2Val',
      },
      {
        label: '一氧化碳浓度',
        readFrom: 'chartData',
        xprop: 'createTime',
        yprop: 'coVal',
      },
      {
        label: '二氧化碳浓度',
        readFrom: 'chartData',
        xprop: 'createTime',
        yprop: 'co2Val',
      },
    ],
    readFrom: '',
  },
  pressureConfig: {
    type: 'line', // 使用普通折线图类型
    legend: { show: true },
    xAxis: [
      {
        show: true,
        name: '时间',
        axisLabel: { color: '#000000' },
      },
    ],
    yAxis: [
      {
        show: true,
        position: 'left',
        name: '压力(kPa)',
        axisLabel: { color: '#000000' },
      },
    ],
    series: [
      {
        label: '压差',
        readFrom: 'chartData',
        xprop: 'createTime',
        yprop: 'sourcePressure',
      },
    ],
    readFrom: '',
  },
  blastDeltaConfig: {},
  boardOut: [
    {
      label: 'CO(ppm)',
      value: 'coValOut',
    },
    {
      label: 'CO2(%)',
      value: 'co2ValOut',
    },
    {
      label: 'O2(%)',
      value: 'o2ValOut',
    },
    {
      label: 'CH4(%)',
      value: 'ch4ValOut',
    },
    {
      label: 'C2H4(ppm)',
      value: 'c2h4ValOut',
    },
    {
      label: 'C2H2(ppm)',
      value: 'c2h2ValOut',
    },
    {
      label: '温度(℃)',
      value: 'temperatureOut',
    },
    {
      label: '压差(Pa)',
      value: 'sourcePressureOut',
    },
  ],
  gasConcentrationOutConfig: {
    type: 'line_smooth', // 使用光滑曲线类型
    legend: { show: true },
    xAxis: [
      {
        show: true,
        name: '时间',
        axisLabel: { color: '#000000' },
      },
    ],
    yAxis: [
      {
        show: true,
        name: '浓度(%)',
        position: 'left',
        splitLine: { show: false },
        axisLine: {
          show: true,
          lineStyle: { color: '#333' },
        },
        axisLabel: { color: '#000000' },
        nameTextStyle: { color: '#000' },
      },
    ],
    series: [
      {
        label: '氧气浓度',
        readFrom: 'chartData',
        xprop: 'createTime',
        yprop: 'o2ValOut',
      },
      {
        label: '一氧化碳浓度',
        readFrom: 'chartData',
        xprop: 'createTime',
        yprop: 'coValOut',
      },
      {
        label: '二氧化碳浓度',
        readFrom: 'chartData',
        xprop: 'createTime',
        yprop: 'co2ValOut',
      },
    ],
    readFrom: '',
  },
  pressureOutConfig: {
    type: 'line', // 使用普通折线图类型
    legend: { show: true },
    xAxis: [
      {
        show: true,
        name: '时间',
        axisLabel: { color: '#000000' },
      },
    ],
    yAxis: [
      {
        show: true,
        position: 'left',
        name: '压力(kPa)',
        axisLabel: { color: '#000000' },
      },
    ],
    series: [
      {
        label: '压差',
        readFrom: 'chartData',
        xprop: 'createTime',
        yprop: 'sourcePressure',
      },
    ],
    readFrom: '',
  },
  blastDeltaOutConfig: {},
};

// 历史数据相关
export const historicalColumns: BasicColumn[] = [
  // {
  //   title: '序号',
  //   dataIndex: 'orderNo',
  //   width: 60,
  // },
  // {
  //   title: '区域',
  //   dataIndex: 'enforcement',
  //   width: 100,
  // },
  {
    title: '煤矿名称',
    dataIndex: 'mineName',
    fixed: 'left',
    width: 200,
  },
  {
    title: '老空区永久密闭名称',
    dataIndex: 'devicePos',
    fixed: 'left',
    width: 200,
  },
  {
    title: '所属煤层',
    dataIndex: 'coalSeamName',
    fixed: 'left',
    width: 100,
  },
  {
    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: 100,
  // },
  {
    title: '更新时间',
    dataIndex: 'createTime',
    width: 100,
  },
];

export const historicalFormSchema: FormSchema[] = [
  // {
  //   field: 'mineCode',
  //   label: '煤矿名称',
  //   component: 'MineCascader',
  //   slot: 'mine-cascader',
  //   colProps: { span: 6 },
  // },
  // {
  //   field: 'mineNameAbbr',
  //   label: '煤矿简称',
  //   component: 'Input',
  //   colProps: { span: 6 },
  // },
  // {
  //   field: 'productStatus',
  //   label: '生产状态',
  //   component: 'Select',
  //   componentProps: {
  //     options: [
  //       { label: '拟建矿井', value: '0' },
  //       { label: '正常生产矿井', value: '1' },
  //       { 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 },
  // },
  // {
  //   field: 'coalSeam',
  //   label: '所属煤层',
  //   component: 'Select',
  //   componentProps: {
  //     options: [
  //       { label: '煤层1', value: '0' },
  //       { label: '煤层2', value: '1' },
  //       { label: '煤层3', value: '2' },
  //     ],
  //   },
  //   colProps: { span: 6 },
  // },
  {
    field: 'goafId',
    label: '密闭名称',
    component: 'ApiSelect',
    slot: 'goaf-select',
    colProps: { span: 6 },
  },
  {
    field: 'startTime',
    label: '起始日期',
    component: 'DatePicker',
    componentProps: {
      showTime: true,
      //日期格式化，页面上显示的值
      format: 'YYYY-MM-DD HH:mm:ss',
      //返回值格式化（绑定值的格式）
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
    },
    defaultValue: dayjs().add(-30, 'day').format('YYYY-MM-DD 00:00:00'),
    colProps: { span: 6 },
  },
  {
    field: 'endTime',
    label: '结束日期',
    component: 'DatePicker',
    componentProps: {
      //日期格式化，页面上显示的值
      format: 'YYYY-MM-DD HH:mm:ss',
      //返回值格式化（绑定值的格式）
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
    },
    defaultValue: dayjs().format('YYYY-MM-DD 23:59:59'),
    colProps: { span: 6 },
  },
];

export const treeData: TreeItem[] = [
  {
    title: '气体参数 ',
    key: '0-0',
    children: [
      { title: 'CO', key: '0-0-0' },
      { title: 'CH4', key: '0-0-1' },
      { title: 'C2H4', key: '0-0-2' },
      { title: 'C2H2', key: '0-0-3' },
      { title: 'CO2', key: '0-0-4' },
      { title: 'O2', key: '0-0-5' },
    ],
  },
  {
    title: '内外压力及压差',
    key: '1-1',
    children: [
      { title: '内压力', key: '1-1-0' },
      { title: '外压力', key: '1-1-1' },
      { title: '压差', key: '1-1-2' },
    ],
  },
  {
    title: '温度',
    key: '2-2',
  },
];

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);
}
