import type { VxeGridProps } from '#/adapter/vxe-table';

import { getQueueJobList } from '../api';
import { normalizeQueueJobFilters } from './search';

/**
 * 按分类生成表格配置（业务/基础设施共用列）
 * 标题可点开操作记录；执行时间列展示延迟标记
 */
export function createGridOptions(category: number): VxeGridProps<any> {
  return {
    checkboxConfig: {
      highlight: true,
      labelField: '',
    },
    columns: [
      { field: 'id', title: 'ID', width: 80 },
      {
        field: 'title',
        title: '标题',
        minWidth: 160,
        slots: { default: 'title' },
      },
      { field: 'job_name_txt', title: '任务类型', minWidth: 140 },
      {
        field: 'delay_type_txt',
        title: '队列类型',
        width: 100,
        slots: { default: 'delay_type' },
      },
      {
        field: 'status_txt',
        title: '状态',
        width: 100,
        slots: { default: 'status' },
      },
      { field: 'time_difference', title: '耗时', width: 90 },
      { field: 'user_name', title: '操作人', width: 100 },
      {
        field: 'error_message',
        title: '错误摘要',
        minWidth: 160,
        slots: { default: 'error' },
      },
      { field: 'retry_from_id', title: '重跑自', width: 80 },
      { field: 'created_at', title: '创建时间', width: 170 },
      {
        field: 'available_at',
        title: '执行时间',
        width: 200,
        slots: { default: 'available_at' },
      },
      {
        title: '操作',
        width: 180,
        fixed: 'right',
        slots: { default: 'action' },
      },
    ],
    keepSource: true,
    pagerConfig: {},
    proxyConfig: {
      ajax: {
        query: async ({ page }, formValues) => {
          return await getQueueJobList({
            page: page.currentPage,
            pageSize: page.pageSize,
            category,
            ...normalizeQueueJobFilters(formValues || {}),
          });
        },
      },
    },
    height: 'auto',
    toolbarConfig: {
      search: true,
      refresh: true,
      zoom: true,
      slots: {
        buttons: 'toolbar-actions',
      },
    },
  };
}
