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

import {Space} from 'ant-design-vue';

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

import type {InspectionTaskForm} from '#/api/property/inspectionManagement/inspectionTask/model';
import {commonDownloadExcel} from '#/utils/file/download';

import inspectionDetailsModal from './inspectionDetails-modal.vue';
import {columns, querySchema} from './data';
import {
  taskDetailExport,
  taskDetailList
} from "#/api/property/inspectionManagement/inspectionDetail";

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

const gridOptions: VxeGridProps = {
  checkboxConfig: {
    // 高亮
    highlight: true,
    // 翻页时保留选中状态
    reserve: true,
    // 点击行选中
    // trigger: 'row',
  },

  columns,
  height: 'auto',
  keepSource: true,
  pagerConfig: {},
  proxyConfig: {
    ajax: {
      query: async ({page}, formValues = {}) => {
        return await taskDetailList({
          pageNum: page.currentPage,
          pageSize: page.pageSize,
          ...formValues,
        });
      },
    },
  },

  rowConfig: {
    keyField: 'id',
  },
  // 表格全局唯一表示 保存列配置需要用到
  id: 'property-inspectionDetails-index'
};

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

const [InspectionDetailsModal, modalApi] = useVbenModal({
  connectedComponent: inspectionDetailsModal,
});

async function handInfo(row: Required<InspectionTaskForm>) {
  detailApi.setData({id: row.id});
  detailApi.open();
}

function handleDownloadExcel() {
  commonDownloadExcel(taskDetailExport, '巡检明细数据', tableApi.formApi.form.values, {
    fieldMappingTime: formOptions.fieldMappingTime,
  });
}
</script>

<template>
  <Page :auto-content-height="true">
    <BasicTable table-title="巡检明细列表">
      <template #toolbar-tools>
        <Space>
          <a-button
            v-access:code="['property:inspectionTask:export']"
            @click="handleDownloadExcel"
          >
            {{ $t('pages.common.export') }}
          </a-button>

        </Space>
      </template>
<!--      <template #action="{ row }">-->
<!--        <Space>-->
<!--          <ghost-button-->
<!--            v-access:code="['property:inspectionTask:info']"-->
<!--            @click.stop="handInfo(row)"-->
<!--          >-->
<!--            {{ $t('pages.common.info') }}-->
<!--          </ghost-button>-->
<!--        </Space>-->
<!--      </template>-->
    </BasicTable>
    <InspectionDetailsModal @reload="tableApi.query()"/>
  </Page>
</template>
