<template>
  <a-modal
    title="附件"
    v-model="AttachmentModVis"
    :confirmLoading="confirmLoading"
    width="90%"
    :footer="null"
  >
  <div>
    
      <ul >
          <li v-for="(item,index) in attachmentData" :key="index">
              <img :src="item.src" class="logo" v-if="item.status == '1'" style="width: 100%;"/>       
              <a v-if="item.status == '0'" style="text-decoration:underline" @click="accessoryDownload(item)">
                {{item.name}}
              </a>       
          </li>
      </ul>
  </div>
  </a-modal>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import JEllipsis from '@/components/jeecg/JEllipsis'
import { downFile } from '@/api/manage'
export default {
  name: 'AttachmentDisplay', // 明细 弹框
  mixins: [JeecgListMixin],
  components: { JEllipsis},
  data() {
    return {      
       AttachmentModVis : false,
       loading: false, // 表格加载
       confirmLoading: false,
       attachmentData:[],
       cc:'',
    }
  },
  watch:{
      attachmentData(newValue){
        this.attachmentData.map(item => {
          var index= item.name.lastIndexOf('.'), // 获取指定字符串最后一次出现的位置
              ext = item.name.substr(index+1),
              suffix = this.isAssetTypeAnImage(ext)
          if(suffix){
            item.status = 1
            item.src = `${window._CONFIG['domianURL']}/${item.src}`
          }else{
            item.status = 0
          }
        })
      }
  },
 
  methods: {
      backDetailTable(){
          this.AttachmentModVis = false
      },
      isAssetTypeAnImage(ext) {
        return [
           'png', 'jpg', 'jpeg', 'bmp', 'gif', 'webp', 'psd', 'svg', 'tiff'].
            indexOf(ext.toLowerCase()) !== -1;
       },

      //  点击附件下载
      accessoryDownload(item){
       downFile(item.src).then(data => {
        if (!data) {
          this.$message.warning('文件下载失败')
          return
        }
        if (typeof window.navigator.msSaveBlob !== 'undefined') {
          window.navigator.msSaveBlob(new Blob([data], { type: 'application/vnd.ms-excel' }), fileName + '.xls')
        } else {
          let url = window.URL.createObjectURL(new Blob([data], { type: 'application/vnd.ms-excel' }))
          let link = document.createElement('a')
          link.style.display = 'none'
          link.href = url
          link.setAttribute('download', item.name)
          document.body.appendChild(link)
          link.click()
          document.body.removeChild(link) // 下载完成移除元素
          window.URL.revokeObjectURL(url) // 释放掉blob对象
        }
      })
    },
 },
}
</script>
<style lang="less" scoped>
@import '~@assets/less/common.less';
@import '~@assets/less/overwriter.less';
// /deep/ .ant-table-thead > tr > th {
//   text-align: center;
//   // font-weight: 700;
// }

// /deep/ .ant-table-tbody {
//   text-align: center;
// }
//  /deep/ .ant-table-footer .ant-table-body {
//     overflow: hidden !important;
//   }
// /deep/ th.replacecolor {
//   background-color: #ccc;
// }
/deep/ .ant-modal-body {
  height: 100% !important;
}
/deep/ .ant-modal-content{
  overflow: auto;
}
</style>
