<template>
  <v-btn class="buttons" color="primary" small :loading="loading" @click="handleDownload">
    <v-icon left>mdi-export</v-icon>
    导出Excel
  </v-btn>
</template>

<script>
import { exportTable } from '@/api'
import { downloadFile } from '@/utils'
export default {
  name: 'unify-export',
  props: {
    query: {
      type: Object,
      default: () => ({})
    }
  },
  data () {
    return {
      loading: false
    }
  },
  methods: {
    async handleDownload () {
      this.loading = true
      const query = this.query
      if (!query.page) {
        Object.assign(query, {
          page: {
            size: 99999,
            current: 1
          }
        })
      }
      try {
        const { data, name } = await exportTable(this.query)
        downloadFile(data, name)
      } catch (error) {
        this.$snackbar.error(error)
      } finally {
        this.loading = false
      }
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
