<template>
  <div>
    <div class="uploadBtn">
      <el-button
        size="small"
        @click="handleUpload"
        icon="el-icon-upload"
        type="primary"
        plain
        >附件上传</el-button
      >
    </div>
    <el-table :data="data" height="180" border style="width: 100%">
      <el-table-column
        prop="name"
        label="文件名称"
        align="center"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="type"
        label="文件类型"
        align="center"
        :show-overflow-tooltip="true"
      >
        <template v-slot="{row}">
          <span v-if="row.type == 1">图片</span>
          <span v-if="row.type == 2">文档</span>
          <span v-if="row.type == 3">压缩文件</span>
          <span v-if="row.type == 4">视频</span>
          <span v-if="row.type == 5">其它</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="fileSize"
        label="文件大小"
        align="center"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="createName"
        label="上传人"
        align="center"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="createTime"
        label="上传时间"
        align="center"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column label="操作" width="120" align="center">
        <template slot-scope="scope">
          <el-link type="primary" @click="preview(scope.row)" size="small"
            >查看</el-link
          >
          <el-link
            type="primary"
            @click="download(scope.row)"
            size="small"
            style="margin-left: 10px"
            >下载</el-link
          >
          <el-link
            type="primary"
            @click.native.prevent="deleteTableData(scope.$index, tableData)"
            size="small"
            style="margin-left: 10px"
            >删除</el-link
          >
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      title="预览"
      class="dialog"
      append-to-body
      :close-on-click-modal="false"
      :visible.sync="dialogVisible"
      v-if="dialogVisible"
      destroy-on-close
      @close="closeDialog"
      width="100%"
    >
      <div>
        <iframe
          :src="url"
          frameborder="0"
          style="z-index: 1000; height: 800px; width: 100%"
        ></iframe>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import req from '@/request.js'
export default {
  props: {
    tableData: {
      type: Array,
      default: () => {
        return []
      },
    },
  },
  data() {
    return {
      data: this.tableData,
      dialogVisible: false,
      url: '',
    }
  },
  watch: {
    tableData() {
      return (this.data = this.tableData)
    },
  },
  mounted() {
    // 文件选择后的回调
    this.$bus.$on('fileAdded', () => {
      console.log('文件已选择')
    })
    // 文件上传成功的回调
    this.$bus.$on('fileSuccess', (file) => {
      let length = this.data.length
      this.data.push({
        key: length + 1,
        ...file,
      })
      this.$emit('accessoryInfo', this.data)
    })
  },
  methods: {
    closeDialog() {
      this.dialogVisible = false
    },
    //查看
    preview(row) {
      console.log(row)
      let type = row.suffix
      if (
        type.indexOf('xsl') !== -1 ||
        type.indexOf('xslx') !== -1 ||
        type.indexOf('xls') !== -1 ||
        type.indexOf('docx') !== -1 ||
        type.indexOf('doc') !== -1 ||
        type.indexOf('pdf') !== -1 ||
        type.indexOf('jpg') !== -1 ||
        type.indexOf('png') !== -1 ||
        type.indexOf('jpeg') !== -1 ||
        type.indexOf('pptx') !== -1 ||
        type.indexOf('ppt') !== -1
      ) {
        this.dialogVisible = true
        // this.url = encodeURIComponent(row.url)
        this.url = row.url
      } else {
        this.$message('当前文件暂不支持预览，请下载查看')
      }
    },
  
    //下载
    download(row) {
      console.log(row)
      if (!row.id) {
        this.$message.warning('请保存后再下载！')
      } else {
        let ids = row.id
        // let name = url.substring(url.lastIndexOf('/') + 1)
        // let index = name.indexOf('.')
        // let ids = name.slice(0, index) //参数
        let bizPath = 'default' //参数
        let formData = new FormData()
        formData.append('bizPath', bizPath)
        formData.append('ids', ids)
        req
          .downloadPostCopy(
            '${cqxy}/biz/universal/accessory/v1/minIoAllDownFile',
            formData
          )
          .then((res) => {
            if (res.status == 200) {
              this.$message.success('下载成功')
            } else {
              this.$$message.error('下载失败')
            }
          })
      }
    },
    //上传发送事件
    handleUpload() {
      this.$bus.$emit('openUploader')
    },
    deleteTableData(index, rows) {
      rows.splice(index, 1)
    },
  },
  destroyed() {
    this.$bus.$off('fileAdded')
    this.$bus.$off('fileSuccess')
  },
}
</script>
<style lang="scss" scoped>
.uploadBtn {
  display: flex;
  justify-content: start;
  margin-bottom: 10px;
}
/deep/ .el-dialog {
  margin-top: 0 !important;
  height: 100%;
  margin-bottom: 0;
}
/deep/ .el-dialog__body {
  padding: 0;
}
.uploadBtn::before {
  content: '*';
  color: #f56c6c;
  margin-right: 4px;
}
</style>
