<script setup>
import { ref, watch, computed } from 'vue'
import {
  NModal,
  NCard,
  NButton,
  NUpload,
  NText,
  NImage,
  NImageGroup
} from 'naive-ui'
// 临时移除图标导入以解决模块解析问题
// import { DownloadIcon } from '@vicons/tabler'

const props = defineProps({
  visible: {
    type: Boolean,
    default: false,
  },
  certificateData: {
    type: Object,
    default: () => ({}),
  },
  mode: {
    type: String,
    default: 'upload', // 'upload' 或 'view'
  },
})

const emit = defineEmits(['update:visible', 'confirm'])

const formData = ref({
  reportFiles: [],
  certificateFiles: [],
})

const reportFileList = ref([])
const certificateFileList = ref([])

// 监听弹窗打开，初始化数据
watch(
  () => props.visible,
  (val) => {
    if (val) {
      if (props.mode === 'view') {
        // 查看模式，加载已有数据
        reportFileList.value = props.certificateData?.reportFiles || []
        certificateFileList.value = props.certificateData?.certificateFiles || []
      } else {
        // 上传模式，清空数据
        formData.value = {
          reportFiles: [],
          certificateFiles: [],
        }
        reportFileList.value = []
        certificateFileList.value = []
      }
    }
  }
)

// 关闭弹窗
const handleClose = () => {
  emit('update:visible', false)
}

// 确认操作
const handleConfirm = () => {
  emit('confirm', {
    reportFiles: formData.value.reportFiles,
    certificateFiles: formData.value.certificateFiles,
  })
  handleClose()
}

// 文件上传前的处理
const beforeUpload = (data) => {
  const { file } = data
  const isImage = file.type.startsWith('image/')
  const isPdf = file.type === 'application/pdf'
  const isWord = file.type.includes('word') || file.type.includes('document')
  const isVideo = file.type.startsWith('video/')

  if (!isImage && !isPdf && !isWord && !isVideo) {
    $message.error('只能上传图片、PDF、Word文档或视频文件')
    return false
  }

  const isLt50M = file.size / 1024 / 1024 < 50
  if (!isLt50M) {
    $message.error('文件大小不能超过50MB')
    return false
  }

  return true
}

// 报告文件上传变化
const handleReportUploadChange = ({ fileList: newFileList }) => {
  reportFileList.value = newFileList
  formData.value.reportFiles = newFileList.map(file => ({
    id: file.id,
    name: file.name,
    url: file.url,
    type: file.type
  }))
}

// 证书文件上传变化
const handleCertificateUploadChange = ({ fileList: newFileList }) => {
  certificateFileList.value = newFileList
  formData.value.certificateFiles = newFileList.map(file => ({
    id: file.id,
    name: file.name,
    url: file.url,
    type: file.type
  }))
}

// 移除文件
const handleRemove = () => {
  return true
}

// 移除证书文件
const removeCertificateFile = (index) => {
  certificateFileList.value.splice(index, 1)
  formData.value.certificateFiles = certificateFileList.value.map(file => ({
    id: file.id,
    name: file.name,
    url: file.url || '',
    type: file.type || ''
  }))
}


// 下载报告
const handleDownloadReport = () => {
  // 这里实现下载原版报告的逻辑
  console.log('下载原版报告')
  // TODO: 实现实际的下载功能
}

// 文件预览
const handlePreview = (file) => {
  // 对于非图片文件，显示提示
  if (!file.type?.startsWith('image/')) {
    $message.info('此文件类型不支持预览，请下载查看')
    return false
  }
  // 图片文件返回 true，让 NUpload 使用内置预览
  return true
}

// 文件下载
const handleDownload = (file) => {
  const link = document.createElement('a')
  link.href = file.url || ''
  link.download = file.name || 'download'
  document.body.appendChild(link)
  link.click()
  document.body.removeChild(link)
}

// 自定义上传预览
const customRequest = ({ file, onFinish, onError }) => {
  // 这里可以实现自定义上传逻辑
  // 暂时模拟上传成功
  setTimeout(() => {
    onFinish()
  }, 1000)
}

const modalTitle = computed(() => {
  return props.mode === 'upload' ? '上传' : '查看'
})

const isUploadMode = computed(() => props.mode === 'upload')
</script>

<template>
  <NModal
    :show="visible"
    :mask-closable="false"
    preset="card"
    :title="modalTitle"
    class="certificate-modal"
    style="width: 700px"
    @update:show="handleClose"
  >
    <!-- 上传/查看模式 -->
    <div class="certificate-content">
      <!-- 报告上传部分 -->
      <div class="upload-section">
        <div class="section-header">
          <div class="section-title">报告:</div>
          <NButton text type="primary" @click="handleDownloadReport">
            点击下载原版报告
          </NButton>
        </div>
        <div class="upload-content">
          <NUpload
            v-model:file-list="reportFileList"
            :max="5"
            list-type="image-card"
            :before-upload="beforeUpload"
            @change="handleReportUploadChange"
            @remove="handleRemove"
            :custom-request="customRequest"
            :disabled="!isUploadMode"
            show-preview-button
            show-download-button
            @download="handleDownload"
          >
          </NUpload>
        </div>
      </div>

      <!-- 证书上传部分 -->
      <div class="upload-section">
        <div class="section-title">证书:</div>
        <div class="upload-content">
          <NUpload
            v-model:file-list="certificateFileList"
            :max="5"
            list-type="image-card"
            :before-upload="beforeUpload"
            @change="handleCertificateUploadChange"
            @remove="handleRemove"
            :custom-request="customRequest"
            :disabled="!isUploadMode"
            show-preview-button
            show-download-button
            @download="handleDownload"
          >
          </NUpload>
        </div>
      </div>
    </div>


    <template #footer>
      <div class="modal-footer">
        <NButton @click="handleClose">取消</NButton>
        <NButton v-if="isUploadMode" type="primary" @click="handleConfirm">
          上传并通知
        </NButton>
        <NButton v-else type="primary" @click="handleConfirm">
          确定
        </NButton>
      </div>
    </template>
  </NModal>
</template>

<style scoped>
.certificate-modal {
  max-width: 90vw;
}

.certificate-content {
  display: flex;
  flex-direction: column;
  gap: 24px;
  padding: 16px 0;
}

/* 上传模式样式 */
.upload-section {
  margin-bottom: 20px;
}

.section-header {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 12px;
}

.section-title {
  font-size: 14px;
  color: #666;
  font-weight: normal;
  margin: 0;
}

.upload-content {
  min-height: 60px;
}

.report-upload-area,
.certificate-upload-area {
  display: flex;
  align-items: center;
  gap: 16px;
  flex-wrap: wrap;
}

.download-section {
  flex: 0 0 auto;
}

.file-section {
  flex: 1;
  min-width: 200px;
}

.uploaded-file {
  padding: 6px 12px;
  background: #f5f5f5;
  border-radius: 4px;
  display: inline-block;
  margin-bottom: 8px;
}


.file-name {
  font-size: 14px;
  color: #333;
  font-weight: 500;
}

.certificate-preview {
  width: 60px;
  height: 60px;
  border-radius: 6px;
  overflow: hidden;
  position: relative;
  border: 1px solid #e8e8e8;
  flex: 0 0 auto;
}

.file-preview-icon {
  width: 60px;
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #f5f5f5;
  color: #666;
  font-size: 12px;
  font-weight: 600;
  border-radius: 6px;
}

.remove-button {
  position: absolute;
  top: -8px;
  right: -8px;
  width: 20px;
  height: 20px;
  background: #ff4d4f;
  color: white;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-size: 12px;
  line-height: 1;
  border: 2px solid white;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.remove-button:hover {
  background: #ff7875;
}

/* 查看模式样式 */
.view-section {
  margin-bottom: 24px;
}

.view-content {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.download-area {
  display: flex;
  justify-content: flex-start;
}

.file-info {
  padding: 8px 12px;
  background: #f5f5f5;
  border-radius: 4px;
  display: inline-block;
}


/* 底部按钮 */
.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
}

/* 缩略图上传组件样式 */
:deep(.n-upload) {
  width: 100%;
}

:deep(.n-upload-file-list) {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
  gap: 8px;
}

:deep(.n-upload-file-card) {
  aspect-ratio: 1;
  border-radius: 6px;
  overflow: hidden;
}

/* 响应式调整 */
@media (max-width: 768px) {
  .certificate-modal {
    width: 95vw !important;
  }

}
</style>
