<template>
  <Page :auto-content-height="true">
    <BasicTable table-title="油轮检查列表">
      <template #action="{ row }">
        <el-button
          size="small"
          type="primary"
          plain
          @click="() => handleTaskClick(row.id)"
          >查看</el-button
        >
        <el-button size="small" type="primary" plain>打印</el-button>
      </template>
    </BasicTable>
  </Page>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import { Page } from '@vben/common-ui';
import { useVbenVxeGrid, type VxeGridProps } from '#/adapter/vxe-table';
import { columns, querySchema } from './tankerInspect-data';
import type { VbenFormProps } from '@vben/common-ui';
import { getParticipationRecordList } from '#/api/archive/operationsManagement/operationsManagement';

const showSearchForm = ref(true) as any;
const router = useRouter();
const route = useRoute();

// 表单字段到 API 字段的映射
function mapFormToApi(formValues: any, page: any) {
  const apiParams = {
    pageNum: page.currentPage,
    pageSize: page.pageSize,
    type: route.query.type || '',
    directoryType: route.query.directoryType || '',
  };
  
  // 处理处理时间
  if (formValues.processTime) {
    apiParams.processStartTime = formValues.processTime[0];
    apiParams.processEndTime = formValues.processTime[1];
  }
  
  // 其他字段
  Object.keys(formValues).forEach(key => {
    if (key !== 'processTime') {
      apiParams[key] = formValues[key];
    }
  });
  
  return apiParams;
}


const formOptions: VbenFormProps = {
  commonConfig: {
    labelWidth: 80,
    componentProps: {
      allowClear: true,
    },
  },
  schema: querySchema(),
  wrapperClass: 'grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4',
};

// 列表中显示配置
const gridOptions: VxeGridProps = {
  checkboxConfig: {
    // 高亮
    highlight: true,
    // 翻页时保留选中状态
    reserve: true,
    // 点击行选中
    trigger: 'default',
  },
  columns,
  size: 'medium',
  height: 'auto',
  proxyConfig: {
    ajax: {
      query: async ({ page }, formValues = {}) => {
        // 映射表单字段到 API 字段
        const apiParams = mapFormToApi(formValues, page);
        
        // 调用API
        const response = await getParticipationRecordList(apiParams);
        
        // 映射 API 响应数据到页面显示字段
        const items = response.rows;
        
        return {
          items,
          total: response.total,
        };
      },
    },
  },
  rowConfig: {
    keyField: 'id',
  },
  toolbarConfig: {
    custom: true,
    refresh: true,
    zoom: true,
  },
  id: 'tanker-inspect-index',
};
const [BasicTable] = useVbenVxeGrid({
  showSearchForm,
  formOptions,
  gridOptions,
});
const handleTaskClick = (taskId: number) => {
  // 跳转到任务详情页面
  router.push(`/schedule/detail?id=${taskId}&type=${route.query.type || ''}&directoryType=${route.query.directoryType || ''}`);
};
</script>

<style scoped lang="scss">
:deep(.el-tooltip__trigger:focus) {
  outline: none;
}
</style>
