<script lang="ts" setup>
// 导入部分
import { ref } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import type { VbenFormProps } from '@vben/common-ui';
import type { VxeGridProps } from '#/adapter/vxe-table';
import { Page } from '@vben/common-ui';
import { ElButton, ElSpace } from 'element-plus';
import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { queryFormSchema, tableColumns } from './addInventory-data';
import { getScheduleTasksList } from '#/api/archive/figuresManage/figuresManage';

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,
  };
  
  // 获取所有的query字段
  Object.keys(route.query).forEach(key => {
    apiParams[key] = route.query[key];
  });
  
  // 处理处理时间
  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: queryFormSchema(),
  wrapperClass: 'grid-cols-1 md:grid-cols-2 lg:grid-cols-4',
};

// 报工数据统计表格配置
const workReportGridOptions: VxeGridProps = {
  checkboxConfig: {
    // 高亮
    highlight: true,
    // 翻页时保留选中状态
    reserve: true,
    // 点击行选中
    trigger: 'default',
  },
  columns: tableColumns,
  height: '800px',
  proxyConfig: {
    autoLoad: true,
    ajax: {
      query: async ({ page }, formValues = {}) => {
        // 映射表单字段到 API 字段
        const apiParams = mapFormToApi(formValues, page);
        
        // 调用API
        const response = await getScheduleTasksList(apiParams);
        
        // 映射 API 响应数据到页面显示字段
        const items = response.rows;
        
        return {
          items,
          total: response.total,
        };
      },
    },
  },
  rowConfig: { keyField: 'id' },
  pagerConfig: { enabled: true, pageSize: 10, pageSizes: [10, 20, 50, 100] },
  toolbarConfig: {
    custom: true,
    refresh: true,
    zoom: true,
  },
  id: 'add-inventory-index',
};

// 创建表格实例
const [WorkReportTable] = useVbenVxeGrid({
  showSearchForm,
  formOptions,
  gridOptions: workReportGridOptions,
});

// 导出按钮点击事件
const handleExport = () => {
  console.log('导出数据');
  // TODO: 添加导出逻辑
};

// 查看按钮点击事件
const handleView = (row: any) => {
  // 构建查询参数
  const queryParams = new URLSearchParams();
  queryParams.append('id', row.id);
  
  // 获取所有的query字段
  Object.keys(route.query).forEach(key => {
    queryParams.append(key, route.query[key] as string);
  });
  // 跳转到任务详情页面

  router.push(`/schedule/detail/${row.id}`);
};
</script>
<template>
  <Page :auto-content-height="true">
    <div class="task-report-container">
      <!-- 报工数据统计表格 -->
      <div class="table-container">
        <WorkReportTable table-title="场站新增任务列表">
          <template #toolbar-tools>
            <!-- <ElSpace>
              <ElButton
                @click="handleExport"
                v-access:code="['system:workDataStatistics:export']"
              >
                导出
              </ElButton>
            </ElSpace> -->
          </template>
          <!-- 操作按钮插槽 -->
          <template #action="{ row }">
            <ElSpace>
              <ElButton type="primary" size="small" @click="handleView(row)">
                查看
              </ElButton>
            </ElSpace>
          </template>
        </WorkReportTable>
      </div>
    </div>
  </Page>
</template>
<style scoped lang="scss">
.task-report-container {
  min-height: calc(100vh - 120px);
  padding: 16px;
  background-color: #fff;
}
</style>
