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

import { DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'meterName',
      label: '计量点',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入计量点名称',
      },
    },
    {
      fieldName: 'energyType',
      label: '能源类型',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.MES_EM_ENERGY_TYPE, 'number'),
        placeholder: '请选择能源类型',
      },
    },
    {
      fieldName: 'alertType',
      label: '预警类型',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.MES_EM_ALERT_TYPE, 'number'),
        placeholder: '请选择预警类型',
      },
    },
    {
      fieldName: 'alertLevel',
      label: '预警级别',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.MES_EM_ALERT_LEVEL, 'number'),
        placeholder: '请选择预警级别',
      },
    },
    {
      fieldName: 'status',
      label: '处理状态',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.MES_EM_ALERT_STATUS, 'number'),
        placeholder: '请选择处理状态',
      },
    },
    {
      fieldName: 'alertDate',
      label: '预警日期',
      component: 'RangePicker',
      componentProps: {
        class: 'w-full',
        format: 'YYYY-MM-DD',
        valueFormat: 'YYYY-MM-DD',
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<EnergyAlertApi.Alert>['columns'] {
  return [
    { field: 'alertNo', title: '预警编号', width: 170 },
    { field: 'alertDate', title: '预警日期', width: 110 },
    { field: 'meterName', title: '计量点/对象', minWidth: 170 },
    {
      field: 'energyType',
      title: '能源类型',
      width: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.MES_EM_ENERGY_TYPE },
      },
    },
    {
      field: 'alertType',
      title: '预警类型',
      width: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.MES_EM_ALERT_TYPE },
      },
    },
    {
      field: 'alertLevel',
      title: '级别',
      width: 90,
      slots: {
        default: 'alertLevel',
      },
    },
    {
      field: 'actualValue',
      title: '实际值 / 阈值',
      width: 180,
      slots: {
        default: 'valueCompare',
      },
    },
    { field: 'content', title: '预警内容', minWidth: 260, showOverflow: 'tooltip' },
    {
      field: 'status',
      title: '处理状态',
      width: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.MES_EM_ALERT_STATUS },
      },
    },
    { field: 'handleUserName', title: '处理人', width: 100 },
    {
      field: 'handleTime',
      title: '处理时间',
      width: 170,
      formatter: 'formatDateTime',
    },
    {
      title: '操作',
      width: 130,
      fixed: 'right',
      slots: {
        default: 'actions',
      },
    },
  ];
}
