<template>
  <a-modal
    :title="modalTitle"
    :width="640"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @cancel="handleCancel"
  >
    <div class="table-operator">
      <a-button type="primary" icon="download" @click="downloadAll()">下载全部</a-button>
    </div>
    <a-table :columns="columns" :dataSource="data">
      <span slot="action" slot-scope="record">
        <template>
          <a @click="handleDownload(record)">下载</a>
        </template>
      </span>
    </a-table>
    <template slot="footer">
      <a-button :loading="confirmLoading" type="primary" @click="handleCancel()">返回</a-button>
    </template>
  </a-modal>
</template>

<script>
import { queryFile, downloadFile } from '@/api/upms/file'

export default {
  name: 'DownLoadModal',
  data () {
    return {
      confirmLoading: false,
      modalTitle: null,
      visible: false,
      data: [],
      id: null,
      module: null,
      allUrl: null,
      columns: [
        {
          title: '名称',
          dataIndex: 'name'
        },
        {
          title: '上传时间',
          dataIndex: 'createdTime'
        },
        {
          title: '操作',
          key: 'action',
          width: '200px',
          align: 'center',
          scopedSlots: { customRender: 'action' }
        }
      ]
    }
  },
  methods: {
    base (id, module, allUrl) {
      this.id = id
      this.module = module
      this.allUrl = allUrl
      queryFile({ targetId: id, module: module }).then(res => {
        const count = res.data.length
        if (count === 0) {
          this.$message.error('下载文件不存在')
        } else if (count === 1) {
          // alert(this.BaseTool.Constant.FILE_URL + res.data[0].url)
          window.open(this.BaseTool.Constant.FILE_URL + res.data[0].url)
        } else {
          this.data = res.data
          this.visible = true
        }
      })
    },
    handleCancel () {
      this.visible = false
      this.confirmLoading = false
    },
    handleDownload (record) {
      window.open(this.BaseTool.Constant.FILE_URL + record.url)
    },
    downloadAll () {
      downloadFile(this.allUrl)
      // window.open(this.allUrl)
    }

  }
}
</script>
