<template>
  <a-modal
    :title="modalTitle"
    :width="640"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @cancel="handleCancel"
  >
    <a-table :columns="columns" :dataSource="data">
      <span slot="action" slot-scope="record">
        <template>
          <a @click="handleView(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 } from '@/api/upms/file'

export default {
  name: 'PreviewModal',
  data () {
    return {
      confirmLoading: false,
      modalTitle: null,
      visible: false,
      data: [],
      id: null,
      module: null,
      columns: [
        {
          title: '名称',
          dataIndex: 'name'
        },
        {
          title: '上传时间',
          dataIndex: 'createdTime'
        },
        {
          title: '操作',
          key: 'action',
          width: '200px',
          align: 'center',
          scopedSlots: { customRender: 'action' }
        }
      ],
      host: null
    }
  },
  methods: {
    base (id, module) {
      this.host = window.location.protocol + '//' + window.location.host
      this.id = id
      this.module = module
      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(process.env.VUE_APP_PREVIEW_URL + this.host + this.BaseTool.Constant.FILE_URL + res.data[0].url)
        } else {
          this.data = res.data
          this.visible = true
        }
      })
    },
    handleCancel () {
      this.visible = false
      this.confirmLoading = false
    },
    handleView (record) {
      window.open(process.env.VUE_APP_PREVIEW_URL + this.host + this.BaseTool.Constant.FILE_URL + record.url)
    }

  }
}
</script>
