<template>
<div class="glp-file-link">
  <glp-file-viewer :url="row.url" :name="row.name" :title="row.name" v-for="(row, i) in rows" :key="i" class="glp-file-link__item"></glp-file-viewer>

  <el-button plain size="mini" v-if="downloadFn" @click="onDownload" :loading="downloading">批量下载</el-button>
</div>
</template>

<script lang="ts">

import { Vue, Component, Prop, Ref, Emit } from 'vue-property-decorator'

export class FileLinkProps {
  name = 'name'
  url = 'url'
}

@Component({
  components: { }
})
export default class GlpFileLink extends Vue {
  @Prop(Array) dataSource!: any[]

  @Prop({ default: () => new FileLinkProps() }) props!: FileLinkProps

  @Prop(Function) downloadFn!: () => Promise<any>

  downloading = false

  get rows () {
    return (this.dataSource || []).map((v: any) => {
      return {
        name: v[this.props.name],
        url: v[this.props.url]
      }
    })
  }

  onDownload () {
    if (this.downloadFn) {
      this.downloading = true
      const ret = this.downloadFn()
      if (ret instanceof Promise) {
        ret.finally(() => {
          this.downloading = false
        })
      } else {
        this.downloading = false
      }
    }
  }
}
</script>

<style lang="scss">
@import "./style/index.scss";

</style>
