<template>
  <div class="document-previewer">
    <!-- PDF预览 -->
    <pdf-viewer
      v-if="fileType === 'pdf'"
      :url="previewUrl"
      :webUrl="webUrl"
    />

    <!-- Office文档预览（DOC/DOCX） -->
    <office-viewer
      v-else-if="['doc','docx','xlsx','xls'].includes(fileType)"
      :url="previewUrl"
      :webUrl="webUrl"
    />

    <!-- 纯文本预览（TXT/MD） -->
    <text-viewer
      v-else-if="['txt','md'].includes(fileType)"
      :url="previewUrl"
      :webUrl="webUrl"
      :file-type="fileType"
    />

    <!-- 不支持的类型提示 -->
    <el-empty
      v-else
      description="暂不支持该格式预览"
    >
      <el-button
        type="primary"
        @click="handleDownload"
      >
        <i class="el-icon-download" /> 下载文件
      </el-button>
    </el-empty>
  </div>
</template>

<script>
import PdfViewer from '@/views/business/aimodel/viewer/PdfViewer.vue'
import OfficeViewer from '@/views/business/aimodel/viewer/OfficeViewer.vue'
import TextViewer from '@/views/business/aimodel/viewer/TextViewer.vue'

export default {
  components: { PdfViewer, OfficeViewer, TextViewer },
  props: {
    previewUrl: {
      type: String,
      required: true
    },
    webUrl: {
      type: String,
      required: true
    }
  },
  computed: {
    fileType() {
      if (!this.previewUrl) return ''
      const match = this.previewUrl.match(/\.([a-z0-9]+)(\?|$)/i)
      console.log("match====>> ",JSON.stringify(match));
      return match ? match[1].toLowerCase() : ''
    }
  },
  methods: {
    handleDownload() {
      window.open(this.webUrl, '_blank')
    }
  }
}
</script>

<style scoped>
.document-previewer {
  width: 100%;
  height: 100%;
  position: relative;
}
</style>
