import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { MesTraceIntegrityApi } from '#/api/mes/trace/integrity';

/** 业务类型选项 */
export const BIZ_TYPE_OPTIONS = [
  { label: '生产批次', value: 'BATCH' },
  { label: '销售出库', value: 'SALES' },
  { label: '消费者扫码', value: 'SCAN' },
];

/** 业务类型中文名 */
export function getBizTypeLabel(bizType?: string): string {
  return (
    BIZ_TYPE_OPTIONS.find((opt) => opt.value === bizType)?.label || bizType || '-'
  );
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'bizType',
      label: '业务类型',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: BIZ_TYPE_OPTIONS,
        placeholder: '请选择业务类型',
      },
    },
    {
      fieldName: 'bizCode',
      label: '业务编码',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入批次号/出库单号/追溯码',
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<MesTraceIntegrityApi.Record>['columns'] {
  return [
    {
      field: 'bizType',
      title: '业务类型',
      width: 110,
      slots: { default: 'bizType' },
    },
    {
      field: 'bizId',
      title: '业务编号',
      width: 110,
    },
    {
      field: 'bizCode',
      title: '业务编码',
      minWidth: 220,
    },
    {
      field: 'contentHash',
      title: '内容摘要',
      minWidth: 300,
      slots: { default: 'contentHash' },
    },
    {
      field: 'eventTime',
      title: '摘要生成时间',
      width: 170,
      formatter: 'formatDateTime',
    },
  ];
}
