import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';

export function useGridColumns(): VxeTableGridOptions['columns'] {
  return [
    { field: 'id', title: 'ID', width: 60 },
    { field: 'name', title: '规则名称', width: 150 },
    {
      field: 'severity', title: '严重级别', width: 90,
      slots: { default: 'severitySlot' },
    },
    { field: 'comparisonOperator', title: '运算符', width: 70 },
    { field: 'thresholdValue', title: '阈值', width: 100 },
    {
      field: 'enabled', title: '启用', width: 70,
      slots: { default: 'enabledSlot' },
    },
    {
      title: '操作', width: 160,
      slots: { default: 'actionSlot' },
      fixed: 'right',
    },
  ];
}

export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'enabled',
      label: '状态',
      component: 'Select',
      componentProps: {
        options: [
          { label: '全部', value: '' },
          { label: '启用', value: 1 },
          { label: '禁用', value: 0 },
        ],
        allowClear: true,
      },
    },
  ];
}

export function useFormSchema(kpiOptions: { label: string; value: number }[]): VbenFormSchema[] {
  return [
    {
      fieldName: 'kpiId',
      label: '关联KPI',
      component: 'Select',
      componentProps: { options: kpiOptions },
      rules: 'required',
    },
    {
      fieldName: 'name',
      label: '规则名称',
      component: 'Input',
      componentProps: { placeholder: '请输入规则名称' },
      rules: 'required',
    },
    {
      fieldName: 'severity',
      label: '严重级别',
      component: 'Select',
      componentProps: {
        options: [
          { label: '警告', value: 1 },
          { label: '严重', value: 2 },
          { label: '紧急', value: 3 },
        ],
      },
      rules: 'required',
    },
    {
      fieldName: 'comparisonOperator',
      label: '比较运算符',
      component: 'Select',
      componentProps: {
        options: [
          { label: '大于 >', value: '>' },
          { label: '小于 <', value: '<' },
          { label: '大于等于 >=', value: '>=' },
          { label: '小于等于 <=', value: '<=' },
        ],
      },
      rules: 'required',
    },
    {
      fieldName: 'thresholdValue', label: '阈值', component: 'InputNumber',
      componentProps: { placeholder: '阈值', style: 'width:100%' },
      rules: 'required',
    },
    {
      fieldName: 'enabled',
      label: '启用',
      component: 'Switch',
      componentProps: { checkedValue: 1, unCheckedValue: 0 },
    },
    {
      fieldName: 'notificationType', label: '通知方式', component: 'Input',
      componentProps: { placeholder: 'system/message/email' },
    },
    {
      fieldName: 'remark', label: '备注', component: 'InputTextArea',
      componentProps: { placeholder: '备注', rows: 2 },
    },
  ];
}

export function severityColor(severity: number): string {
  if (severity === 3) return 'red';
  if (severity === 2) return 'orange';
  return 'blue';
}

export function severityText(severity: number): string {
  if (severity === 3) return '紧急';
  if (severity === 2) return '严重';
  return '警告';
}
