<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 { getTaskPhotoList } from '#/api/archive/operationsManagement/operationsManagement';

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

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

const router = useRouter();
const route = useRoute();

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 = {
          pageNum: page.currentPage,
          pageSize: page.pageSize,
          ...formValues,
        };
        Object.keys(route.query).forEach((key) => {
          apiParams[key] = route.query[key];
        });
        // 调用API
        const response = await getTaskPhotoList(apiParams);

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

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

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

// 查看任务详情
function viewDetail(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="viewDetail(row)">
            查看
          </ElButton>
        </ElSpace>
      </template>
    </BasicTable>
  </Page>
</template>
