import { downLoad2 } from '@/utils'
export const File = {
  methods: {
    downLoad(item) {
      downLoad2(item.guid)
      // downLoad2(item.url)
    },
    // 预览
    preview(data) {
      let guid = data.guid
      var item = JSON.parse(JSON.stringify(data))

      var arr = ['png', 'jpeg', 'bmp', 'jpg']
      if (arr.includes(item.type)) {
        var FileUrl = this.$commonJS.checkViewer(guid)
        var isPicture = 1
      } else {
        let http = this.$c.staticURL
        var url = this.$commonJS.checkViewer(guid)
        var FileUrl = `http://${http}:8012/onlinePreview?url=` + btoa(encodeURIComponent(url + '&fullfilename=test.' + item.type))
        var isPicture = 0
      }
      // return
      const router = this.$router.resolve({
        path: '/checkFile',
        query: {
          row: JSON.stringify(item),
          FileUrl: FileUrl,
          isPicture: isPicture
        }
      })
      window.open(router.href, '_blank');
    },
    // preview(row){
    //     var item = JSON.parse(JSON.stringify(row))
    //     var arr = ['png','jpeg','bmp','jpg']
    //     if(arr.includes(item.suffix)){
    //         var FileUrl = this.$p + item.url
    //         var isPicture = 1
    //     }else if(item.suffix == 'pdf'){
    //         var FileUrl = this.$p + item.url
    //         var isPicture = 0
    //     }else{
    //         var FileUrl =  `http://${this.$c.hostname}:8012/onlinePreview?url=`+ btoa(encodeURIComponent(this.$p + item.url))
    //         var isPicture = 0
    //     }
    //     const router = this.$router.resolve({
    //         path: '/checkFile',
    //         query: {
    //             row: JSON.stringify(item),
    //             FileUrl: FileUrl,
    //             isPicture:isPicture
    //         }
    //     })
    //     window.open(router.href, '_blank');
    // },
    delFile(item) {
      this.$emit('delFile', item.id)
    }
  },
}

export const File2 = {
  methods: {
    downLoad(row) {
      var item = JSON.parse(JSON.stringify(row))
      var href = ''
      if (item.originalName) {
        href = `http://${this.$c.hostname}:8801/fileManager/downloadSystemFile?fileId=${item.id}`
      } else {
        href = `/api/report/api/download/downloadFile?url=${encodeURIComponent(item.url)}`
      }
      const anchor = document.createElement('a');
      const fileName = 'download';
      if ('download' in anchor) {
        anchor.href = href;
        anchor.setAttribute("download", fileName);
        anchor.className = "download-js-link";
        anchor.innerHTML = "downloading...";
        anchor.style.display = "none";
        document.body.appendChild(anchor);
        setTimeout(function () {
          anchor.click();
          document.body.removeChild(anchor);
        }, 66);
        return true;
      }
    },
    preview(row) {
      var item = JSON.parse(JSON.stringify(row))
      if (item.originalName) {
        item.url = `http://${this.$c.hostname}:8801/fileManager/downloadSystemFile?fileId=${item.id}`
        var arr = ['png', 'jpeg', 'bmp', 'jpg']
        if (arr.includes(item.suffix)) {
          var FileUrl = item.url
          var isPicture = 1
        } else if (item.suffix == 'pdf') {
          var FileUrl = item.url
          var isPicture = 0
        } else {
          var FileUrl = `http://${this.$c.hostname}:8012/onlinePreview?url=` + btoa(encodeURIComponent(item.url + '&fullfilename=test.' + item.suffix))
          var isPicture = 0
        }
        const router = this.$router.resolve({
          path: '/checkFile',
          query: {
            row: JSON.stringify(item),
            FileUrl: FileUrl,
            isPicture: isPicture
          }
        })
        window.open(router.href, '_blank');
        return false
      }
      var arr = ['png', 'jpeg', 'bmp', 'jpg']
      if (arr.includes(item.suffix)) {
        var FileUrl = this.$rImg + item.url
        var isPicture = 1
      } else if (item.suffix == 'pdf') {
        var FileUrl = this.$rImg + item.url
        var isPicture = 0
      } else {
        var FileUrl = `http://${this.$c.hostname}:8012/onlinePreview?url=` + btoa(encodeURIComponent(this.$rImg + item.url))
        var isPicture = 0
      }
      const router = this.$router.resolve({
        path: '/checkFile',
        query: {
          row: JSON.stringify(item),
          FileUrl: FileUrl,
          isPicture: isPicture,
          reportId: true
        }
      })
      window.open(router.href, '_blank');
    },
    delFile(item) {
      this.$emit('delFile', item.id)
    }
  },
}