<template>
  <BasicModal v-bind="$attrs" @register="registerModal" title="预览" width="70%">
    <div>
      <!-- <div class="docWrap" v-if="doc_word.indexOf(data.type) >= 0">
        <div ref="file"></div>
      </div> -->
      <div>
        <div class="mask" v-if="data.isAllowDownload == 0"></div>
        <iframe ref="pdf" :src="pdf_url" width="100%" style="border: 0; height: 80vh"></iframe>
      </div>

      <!-- <div style="text-align: center" v-if="doc_img.indexOf(data.type) >= 0">
        <img :src="pdf_url" class="w-full h-70vh" />
      </div> -->
    </div>
    <template #footer> </template>
  </BasicModal>
</template>
<script lang="ts" setup>
  import { ref } from 'vue';
  import { renderAsync } from 'docx-preview';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { getFileblob } from '/@/api/common/api';

  const [registerModal, {}] = useModalInner(async (res: any) => {
    pdf_url.value = '';
    data.value = res.record;
    GetFileData(data.value);
  });

  // const doc_pdf = ref('pdf/PDF');
  const doc_word = ref('doc/docx/DOC/DOCX');
  const doc_img = ref('jpg/jpeg/png/gif/JPG/JPEG/PNG/GIF');
  const pdf_url = ref('');
  const data = ref({
    hz: '',
    isAllowDownload: 1,
  });
  const file = ref();

  function GetFileData(data) {
    getFileblob(data.file, null).then((res) => {
      // 上传固定格式pdf的
      // if (doc_pdf.value.indexOf('pdf') >= 0 || doc_pdf.value.indexOf('PDF') >= 0) {
      pdf_url.value = window.URL.createObjectURL(new Blob([res], { type: 'application/pdf' }));
      // }
      if (doc_word.value.indexOf(data.type) >= 0) {
        renderAsync(res, file.value);
      }
      if (doc_img.value.indexOf(data.type) >= 0) {
        pdf_url.value = window.URL.createObjectURL(new Blob([res], { type: 'application/jpg' }));
      }
    });
  }
</script>
