<script setup lang="ts">
import type { VbenFormProps } from '@vben/common-ui';

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

import { useRoute, useRouter } from 'vue-router';

import { Page } from '@vben/common-ui';

import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { getTaskCheckList } from '#/api/archive/operationsManagement/operationsManagement';

import { queryFormSchema, tableColumns } from './visitorCheckTask-data';

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 || '',
    ...formValues,
  };
  Object.keys(route.query).forEach((key) => {
    apiParams[key] = route.query[key];
  });
  return apiParams;
}

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

const gridOptions: VxeGridProps = {
  checkboxConfig: {
    highlight: true,
    reserve: true,
    trigger: 'default',
  },
  columns: tableColumns,
  size: 'medium',
  height: 'auto',
  proxyConfig: {
    ajax: {
      query: async ({ page }, formValues = {}) => {
        // 映射表单字段到 API 字段
        const apiParams = mapFormToApi(formValues, page);

        // 调用API
        const response = await getTaskCheckList(apiParams);

        // 映射 API 响应数据到页面显示字段
        const items = response.rows;

        return {
          items,
          total: response.total,
        };
      },
    },
  },
  rowConfig: {
    keyField: 'id',
  },
  toolbarConfig: {
    custom: true,
    refresh: true,
    zoom: true,
  },
  id: 'system-checklis-visitorCheckTask-index',
};

const [BasicTable] = useVbenVxeGrid({
  formOptions,
  gridOptions,
});

// 查看任务详情
function handleView(row: any) {
  // 跳转到任务详情页面
  // const queryParams = new URLSearchParams();
  // queryParams.append('id', row.id);
  // Object.keys(route.query).forEach((key) => {
  //   queryParams.append(key, route.query[key] as string);
  // });
  router.push(`/schedule/detail/${row.taskId}`);
}
</script>

<template>
  <Page :auto-content-height="true">
    <BasicTable table-title="检查任务列表">
      <template #action="{ row }">
        <ElSpace>
          <ElButton size="small" type="primary" plain @click="handleView(row)">
            查看
          </ElButton>
        </ElSpace>
      </template>
    </BasicTable>
  </Page>
</template>

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