<template>
  <el-button v-loading="loading" :type="type" :size="size" @click="download">
    模板下载
  </el-button>
</template>

<script>
import { get_template } from '@/api/import-data'
export default {
  props: {
    action: {
      type: String,
      default: ''
    },
    code: {
      type: String,
      default: ''
    },
    type: {
      type: String,
      default: 'success'
    },
    size: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      loading: false,
      downloadList: []
    }
  },
  mounted() {
    this.getData()
  },
  methods: {
    getData() {
      get_template().then(res => {
        console.log(res)
        this.downloadList = res.data.filter(item => item.mark === this.action)
      })
    },
    download() {
      this.loading = true
      this.downloadList.forEach((item, index) => {
        setTimeout(() => {
          const aEl = document.createElement('a')
          const e = document.createEvent('MouseEvents') // 创建鼠标事件对象
          e.initEvent('click', false, false) // 初始化事件对象
          aEl.href = item.url
          // aEl.click()
          aEl.dispatchEvent(e)
          aEl.remove()
        }, index * 500)
      })
      this.loading = false
    },
  }
}
</script>
