<!-- 导出excel按钮组件 后端直接返回纯数据 前端根据数据处理生成excel -->
<script setup lang="tsx" generic="T, K">
import { exportExcel, type ExportColumn } from '@/utils'
import type { BackendServiceResult } from '@/utils/request/types'
const {
  api,
  searchParams,
  fileName = '导出数据',
  columns,
} = defineProps<{
  api: (params: T) => Promise<BackendServiceResult<K[]>>
  searchParams: T
  columns: ExportColumn<K>[]
  fileName: string
}>()

const loading = ref(false)

const handleExport = async () => {
  loading.value = true
  try {
    const { data } = await api(searchParams)
    loading.value = false
    exportExcel({
      data,
      columns,
      fileName,
    })
  } catch (error) {
    console.log(error)
  } finally {
    loading.value = false
  }
}
</script>

<template>
  <el-button :loading="loading" type="primary" @click="handleExport">
    <el-icon v-show="!loading"><IEpDownload /></el-icon>
    导出
  </el-button>
</template>
