import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { MesDvTelemetryApi } from '#/api/mes/dv/telemetry';

/** 是否异常选项（布尔值，后端直接接收） */
export const ANOMALY_OPTIONS = [
  { label: '正常', value: false },
  { label: '异常', value: true },
];

/** 历史记录搜索表单 */
export function useHistoryGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'deviceName',
      label: '设备名称',
      component: 'Input',
      componentProps: { allowClear: true, placeholder: '请输入设备名称' },
    },
    {
      fieldName: 'paramName',
      label: '信号名称',
      component: 'Input',
      componentProps: { allowClear: true, placeholder: '请输入信号名称' },
    },
    {
      fieldName: 'whetherAnomaly',
      label: '是否异常',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: ANOMALY_OPTIONS,
        placeholder: '请选择是否异常',
      },
    },
    {
      fieldName: 'telemetryDataTime',
      label: '遥测时间',
      component: 'RangePicker',
      componentProps: { class: '!w-full', valueFormat: 'YYYY-MM-DD HH:mm:ss' },
    },
  ];
}

/** 实时数据字段 */
export function useLatestColumns(): VxeTableGridOptions<MesDvTelemetryApi.Telemetry>['columns'] {
  return [
    { field: 'deviceName', title: '设备名称', minWidth: 140 },
    { field: 'paramName', title: '信号名称', minWidth: 140 },
    { field: 'timelyValue', title: '信号值(实时)', width: 120 },
    {
      field: 'whetherAnomaly',
      title: '是否异常',
      width: 100,
      slots: { default: 'anomaly' },
    },
    { field: 'shiftName', title: '班次', width: 100 },
    {
      field: 'telemetryDataTime',
      title: '遥测时间',
      width: 180,
      formatter: 'formatDateTime',
    },
    { field: 'standardValue', title: '标准值', width: 110 },
    { field: 'avgValue', title: '均值', width: 110 },
    { field: 'maxValue', title: '最大值', width: 110 },
    { field: 'minValue', title: '最小值', width: 110 },
  ];
}

/** 历史记录字段 */
export function useHistoryColumns(): VxeTableGridOptions<MesDvTelemetryApi.Telemetry>['columns'] {
  return [
    { field: 'deviceName', title: '设备名称', minWidth: 140 },
    { field: 'paramName', title: '信号名称', minWidth: 140 },
    { field: 'timelyValue', title: '信号值', width: 110 },
    {
      field: 'whetherAnomaly',
      title: '是否异常',
      width: 100,
      slots: { default: 'anomaly' },
    },
    { field: 'shiftName', title: '班次', width: 100 },
    { field: 'billNo', title: '单据号', minWidth: 120 },
    {
      field: 'telemetryDataTime',
      title: '遥测时间',
      width: 180,
      formatter: 'formatDateTime',
    },
    {
      field: 'pullTime',
      title: '拉取时间',
      width: 180,
      formatter: 'formatDateTime',
    },
  ];
}
