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

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

// 导入部分
import { ref } from 'vue';
import { useRouter } from 'vue-router';

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

import { ElButton, ElSpace } from 'element-plus';

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

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

const router = useRouter();

// 显示搜索表单
const showSearchForm = ref(true);

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

// 量缸试水记录表格配置
const waterTestRecordGridOptions: VxeGridProps = {
  columns: tableColumns,
  height: '800px',
  proxyConfig: {
    autoLoad: true,
    ajax: {
      query: async ({ page }, formValues = {}) => {
        // 调用API
        const res = await getWaterTestRecordList({
          ...formValues,
          pageNum: page.pageNum,
          pageSize: page.pageSize,
        });

        return {
          items: res.rows || [],
          total: res.total || 0,
        };
      },
    },
  },
  rowConfig: { keyField: 'id' },
  pagerConfig: { enabled: true, pageSize: 100, pageSizes: [10, 20, 50, 100] },
  toolbarConfig: {
    custom: true,
    refresh: true,
    zoom: true,
  },
  id: 'water-test-record-index',
};

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

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

// 查看按钮点击事件
const handleView = (row) => {
  router.push(`/schedule/detail/${row.taskId}`);
};
</script>

<template>
  <Page :auto-content-height="true">
    <div class="water-test-record-container">
      <!-- 量缸试水记录表格 -->
      <div class="table-container">
        <WaterTestRecordTable table-title="量缸试水记录列表">
          <!-- <template #toolbar-tools>
            <ElSpace>
              <ElButton 
                @click="handleExport"
              >
                导出
              </ElButton>
            </ElSpace>
          </template> -->
          <template #action="{ row }">
            <ElSpace>
              <ElButton type="primary" @click="handleView(row)">
                查看
              </ElButton>
            </ElSpace>
          </template>
        </WaterTestRecordTable>
      </div>
    </div>
  </Page>
</template>

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