<template>
  <el-dialog 
    title="查看详情" 
    v-model="visible"
    :close-on-click-modal="false" 
    draggable
    width="1000px">
    <div v-loading="loading" class="article-container" v-if="detailData">
      <!-- 报纸文章样式 -->
      <div class="article-header">
        <h1 class="article-title">{{ detailData.title || '-' }}</h1>
        <div class="article-meta">
          <div class="meta-left">
            <span class="meta-item">类型：{{ formatType(detailData.type) }}</span>
            <span class="meta-item">发表刊物：{{ detailData.journal || '-' }}</span>
            <span class="meta-item">发表页码：{{ detailData.page || '-' }}</span>
          </div>
          <div class="meta-right">
            <span class="meta-item">班号：{{ detailData.classNo || '-' }}</span>
            <span class="meta-item">班主任：{{ detailData.teacherRealName || '-' }}</span>
          </div>
        </div>
      </div>
      <div class="article-content" v-html="detailData.description || '-'"></div>
      
      <!-- 附件区域 -->
      <div v-if="attachmentList.length > 0" class="article-attachment">
        <h3 class="attachment-title">附件</h3>
        <div class="attachment-list">
          <div
            v-for="(url, index) in attachmentList"
            :key="index"
            class="attachment-item">
            <el-icon class="attachment-icon"><Document /></el-icon>
            <span class="attachment-name">{{ getFileName(url) }}</span>
            <el-button 
              icon="Download" 
              text 
              type="primary" 
              size="small"
              @click="handleDownload(url)">
              下载
            </el-button>
          </div>
        </div>
      </div>
    </div>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="visible = false">关 闭</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts" name="ClassPaperDetailDialog">
import { ref, computed, onMounted } from 'vue'
import { getDetail } from '/@/api/stuwork/classpaper'
import { getDicts } from '/@/api/admin/dict'
import { Document, Download } from '@element-plus/icons-vue'
import other from '/@/utils/other'

// 定义变量内容
const visible = ref(false)
const loading = ref(false)
const detailData = ref<any>(null)
const schoolTermList = ref<any[]>([])
const typeList = ref<any[]>([
  { label: '德育论文', value: '0' },
  { label: '教案', value: '1' }
])

// 附件列表
const attachmentList = computed(() => {
  if (!detailData.value || !detailData.value.attachment) return []
  const urls = typeof detailData.value.attachment === 'string' 
    ? detailData.value.attachment.split(',').filter((url: string) => url.trim())
    : []
  return urls
})

// 格式化类型
const formatType = (value: string) => {
  if (!value) return '-'
  const item = typeList.value.find((item: any) => item.value === value)
  return item ? item.label : value
}

// 获取文件名
const getFileName = (url: string): string => {
  if (!url) return '未知文件'
  return other.getQueryString(url, 'fileName') || other.getQueryString(url, 'originalFileName') || url.split('/').pop() || '未知文件'
}

// 下载文件
const handleDownload = (url: string) => {
  if (!url) return
  window.open(url, '_blank')
}

// 打开弹窗
const openDialog = async (id: string) => {
  visible.value = true
  detailData.value = null
  loading.value = true
  try {
    const res = await getDetail(id)
    if (res.data) {
      detailData.value = res.data
    }
  } catch (err) {
  } finally {
    loading.value = false
  }
}

// 获取学期字典
const getSchoolTermDict = async () => {
  try {
    const res = await getDicts('school_term')
    if (res.data && Array.isArray(res.data)) {
      schoolTermList.value = res.data.map((item: any) => ({
        label: item.label || item.dictLabel || item.name,
        value: item.value || item.dictValue || item.code
      }))
    }
  } catch (err) {
  }
}

// 初始化
onMounted(() => {
  getSchoolTermDict()
})

// 暴露方法
defineExpose({
  openDialog
})
</script>

<style scoped>
.article-container {
  padding: 20px;
  max-width: 100%;
}

.article-header {
  border-bottom: 2px solid #e4e7ed;
  padding-bottom: 20px;
  margin-bottom: 30px;
}

.article-title {
  font-size: 28px;
  font-weight: bold;
  color: #303133;
  margin: 0 0 15px 0;
  line-height: 1.5;
  text-align: center;
}

.article-meta {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  font-size: 14px;
  color: #909399;
  padding-top: 10px;
  flex-wrap: wrap;
  gap: 10px;
}

.meta-left,
.meta-right {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.meta-item {
  font-weight: 500;
}

.article-content {
  font-size: 16px;
  line-height: 1.8;
  color: #606266;
  text-align: justify;
  word-wrap: break-word;
  min-height: 200px;
}

.article-content :deep(p) {
  margin: 0 0 15px 0;
}

.article-content :deep(img) {
  max-width: 100%;
  height: auto;
  display: block;
  margin: 20px auto;
}

.article-content :deep(h1),
.article-content :deep(h2),
.article-content :deep(h3),
.article-content :deep(h4),
.article-content :deep(h5),
.article-content :deep(h6) {
  margin: 20px 0 15px 0;
  font-weight: bold;
}

.article-content :deep(ul),
.article-content :deep(ol) {
  margin: 15px 0;
  padding-left: 30px;
}

.article-content :deep(blockquote) {
  border-left: 4px solid #409eff;
  padding-left: 15px;
  margin: 15px 0;
  color: #909399;
  font-style: italic;
}

.article-attachment {
  margin-top: 30px;
  padding-top: 20px;
  border-top: 1px solid #e4e7ed;
}

.attachment-title {
  font-size: 18px;
  font-weight: bold;
  color: #303133;
  margin: 0 0 15px 0;
}

.attachment-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.attachment-item {
  display: flex;
  align-items: center;
  padding: 12px 16px;
  background-color: #f5f7fa;
  border-radius: 4px;
  transition: background-color 0.3s;
}

.attachment-item:hover {
  background-color: #ecf5ff;
}

.attachment-icon {
  font-size: 20px;
  color: #409eff;
  margin-right: 12px;
}

.attachment-name {
  flex: 1;
  color: #606266;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
</style>
