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

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

import { getDateRangePickerDefaultProps } from '#/utils';

/** 统计的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'dateRange',
      label: '统计区间',
      component: 'RangePicker',
      componentProps: {
        ...getDateRangePickerDefaultProps(),
        allowClear: true,
      },
      help: '默认统计近 30 天',
    },
  ];
}

/** 统计表格列 */
export function useGridColumns(): VxeTableGridOptions<MesDvStatisticsApi.MachineryStatistics>['columns'] {
  return [
    { field: 'machineryCode', title: '设备编码', minWidth: 130 },
    { field: 'machineryName', title: '设备名称', minWidth: 140 },
    { field: 'machineryTypeName', title: '设备类型', minWidth: 120 },
    { field: 'workshopName', title: '所属车间', minWidth: 120 },
    { field: 'processName', title: '所属工序', minWidth: 120 },
    {
      field: 'status',
      title: '设备状态',
      width: 110,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.MES_DV_MACHINERY_STATUS },
      },
    },
    { field: 'checkCount', title: '点检次数', width: 100 },
    { field: 'maintenCount', title: '保养次数', width: 100 },
    { field: 'repairCount', title: '故障次数', width: 100 },
    { field: 'borrowCount', title: '借用次数', width: 100 },
    { field: 'repairHours', title: '维修耗时(h)', width: 120 },
    { field: 'downHours', title: '停机时长(h)', width: 120 },
    {
      field: 'usageRate',
      title: '使用率(%)',
      width: 120,
      formatter: ({ cellValue }) =>
        cellValue === null || cellValue === undefined ? '' : `${cellValue}%`,
    },
    {
      field: 'lastRepairTime',
      title: '最近报修时间',
      width: 180,
      formatter: 'formatDate',
    },
  ];
}
