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

const showSearchForm = ref(true) as any;

// 表单字段到 API 字段的映射
function mapFormToApi(formValues: any, page: any) {
  const apiParams = {
    pageNum: page.currentPage,
    pageSize: page.pageSize,
    type: route.query.type || '',
    directoryType: route.query.directoryType || '',
    ...formValues,
  };
  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 oilGunTementGridOptions: VxeGridProps = {
  columns: tableColumns,
  height: '800px',
  proxyConfig: {
    autoLoad: true,
    ajax: {
      query: async ({ page }, formValues = {}) => {
        // 映射表单字段到 API 字段
        const apiParams = mapFormToApi(formValues, page);
        
        // 调用API
        const response = await getGunSelfCheckList(apiParams);
        
        // 映射 API 响应数据到页面显示字段
        const items = response.rows;
        
        return {
          items,
          total: response.total,
        };
      },
    },
  },
  rowConfig: { keyField: 'id' },
  pagerConfig: { enabled: true, pageSize: 100, pageSizes: [10, 20, 50, 100] },
};

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

// 查看按钮点击事件
const handleView = (row: any) => {
  console.log('查看数据:', row);
  router.push(`/schedule/detail/${row.id}?type=${route.query.type || ''}&directoryType=${route.query.directoryType || ''}`);
};

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

// 损益报表按钮点击事件
const handleProfitLossReport = (row: any) => {
  console.log('生成损益报表:', row);
  // TODO: 添加损益报表逻辑
};
</script>

<template>
  <Page :auto-content-height="true">
    <div class="oil-gun-tement-container">
      <!-- 油枪自检损益表表格 -->
      <OilGunTementTable table-title="油枪自检损益表列表">
        <template #toolbar-tools>
          <ElSpace>
            <!-- <ElButton 
              @click="handleExport"
              v-access:code="['system:oilGunTement:export']"
            >
              导出
            </ElButton> -->
          </ElSpace>
        </template>
        <template #action="{ row }">
          <ElSpace>
            <ElButton size="small" @click="handleView(row)">查看</ElButton>
            <!-- <ElButton size="small" @click="handleExport(row)">导出</ElButton> -->
            <!-- <ElButton size="small" type="primary" @click="handleProfitLossReport(row)">损益报表</ElButton> -->
          </ElSpace>
        </template>
      </OilGunTementTable>
    </div>
  </Page>
</template>

<style scoped lang="scss">
.oil-gun-tement-container {
  min-height: calc(100vh - 120px);
  padding: 16px;
  background-color: #fff;
}
</style>
