<template>
  <div>
    <vxe-modal
      :width="defaultModalSize.width"
      :height="defaultModalSize.height"
      show-zoom
      resize
      transfer
      esc-closable
      :fullscreen="$store.getters.device === 'mobile'"
      :z-index="zIndex"
      v-model="showModal"
      :footer-hide="true"
      title="预览"
      :padding="false"
    >
      <template>
        <iframe
          ref="iframe"
          style="display: block;width: 100%;height: 100%;border: 0;"
          frameborder="0"
        ></iframe>
      </template>
    </vxe-modal>
  </div>
</template>

<script>
import { PopupManager } from "element-ui/lib/utils/popup";
import {defaultModalSize} from "@/settings";
import {downLoadFile} from "@/api/common.js"
import { getToken } from '@/utils/auth'
export default {
  data() {
    return {
      defaultModalSize,
      zIndex: PopupManager.nextZIndex(),
      showModal:false,
      codeUri: "",
    }
  },
  methods: {
    open(url) {
      // this.codeUri = `${window.location.origin}/admin/#/preview?url=${url}`;
      this.showModal = true;
      this.$nextTick(() => {
        this.$refs.iframe.contentWindow.location.replace(`${window.location.origin}/admin/#/preview?url=${url}`)
      })
    },
    async download(id){
      // const res = await downLoadFile({
      //   id:id
      // })
      let url = `${window.location.origin}/api/download-file?id=${id}&token=${window.encodeURIComponent(getToken())}`
      window.open(url,'_blank')
    }
  },
  computed: {},
  watch: {
    showModal(newVal) {
      if (!newVal) {
        this.codeUri = ''
      } else {
        this.zIndex = PopupManager.nextZIndex()
      }
    }
  },
  created() {
    this.$bus.$on('online-file',(url) => this.open(url))
    this.$bus.$on('online-download',(id) => this.download(id))
  },
  beforeDestroy() {
    this.$bus.$off('online-file')
    this.$bus.$off('online-download')
  }
}
</script>

<style scoped lang="scss">
</style>
