import moment from 'moment';
import { Tag } from 'ant-design-vue';
import { h } from 'vue';
import { findDictItemByCode } from '/@/api/system/dict';
import { BasicColumn, FormSchema } from '/@/components/Table';
import { formatToDateTime } from '/@/utils/dateUtil';

export enum EventType {
  ERROR = 'ERROR',
  INFO = 'INFO',
  ALERT = 'ALERT',
}

export enum EventTypeColor {
  ERROR = 'error',
  INFO = 'success',
  ALERT = 'warning',
}

export enum EventTypeName {
  ERROR = '故障',
  INFO = '信息',
  ALERT = '告警',
}

export const columnSchema: BasicColumn[] = [
  {
    title: '时间',
    dataIndex: 'eventTime',
    format(text) {
      return formatToDateTime(text, 'YYYY-MM-DD HH:mm:ss');
    },
  },
  {
    title: '标识符',
    dataIndex: 'eventIdentifier',
    helpMessage: ['标识符格式为模块标识符: 功能定义标识符'],
  },
  {
    title: '事件名称',
    dataIndex: 'eventName',
  },
  {
    title: '事件类型',
    dataIndex: 'eventType',
    customRender({ text }) {
      return h(
        Tag,
        {
          color: EventTypeColor[text as EventType],
        },
        () => EventTypeName[text as EventType]
      );
    },
  },
  {
    title: '输出参数',
    dataIndex: 'outputParams',
    slots: { customRender: 'outputParams' },
  },
];

export const formSchemas: FormSchema[] = [
  {
    field: 'eventIdentifier',
    label: '标识符',
    component: 'Input',
    componentProps: {
      placeholder: '请输入标识符',
    },
    colProps: { span: 6 },
  },
  {
    field: 'eventType',
    label: '告警类型',
    component: 'ApiSelect',
    componentProps: {
      placeholder: '请选择事件类型',
      api: findDictItemByCode,
      params: {
        dictCode: 'event_type',
      },
      labelField: 'itemText',
      valueField: 'itemValue',
    },
    colProps: { span: 6 },
  },
  {
    field: 'dateRange',
    label: '时间范围',
    component: 'RangePicker',
    componentProps: {
      showTime: {
        defaultValue: [moment('00:00:00', 'HH:mm:ss'), moment('23:59:59', 'HH:mm:ss')],
      },
    },
    colProps: { span: 6 },
  },
];
