<template>
  <div class="preview-submit">
    <div class="upload-header">
      <h3>预览与提交</h3>
      <p>确认以下信息无误后提交审核</p>
    </div>

    <div class="preview-section">
      <!-- 课程信息卡片 -->
      <div class="course-preview-card">
        <div class="course-preview-layout">
          <!-- 左侧：封面和基本信息 -->
          <div class="course-left">
            <div class="course-cover" v-if="courseCoverPreviewUrl">
              <img :src="courseCoverPreviewUrl" alt="课程封面" />
            </div>
            <div class="course-basic-info">
              <h3 class="course-title">{{ courseForm.title || '未填写' }}</h3>
              <div class="course-meta">
                <span class="course-category">{{ courseForm.cateId || '未选择' }}</span>
                <span class="course-price">{{ courseForm.isFree ? '免费' : '付费' }}</span>
               
              </div>
            </div>
          </div>
          
          <!-- 右侧：课程描述 -->
          <div class="course-right">
            <div class="rich-text-content" v-html="courseForm.description || '未填写描述'"></div>
          </div>
        </div>
      </div>

      <!-- 章节与视频 -->
      <div class="chapters-preview-card">
        <h4 class="section-title">I 章节与视频</h4>
        <div class="chapters-list">
          <div class="chapter-item" v-for="(chapter, cIndex) in chapters" :key="chapter.id">
            <div class="chapter-header">
              <el-icon class="expand-icon"><ArrowDown /></el-icon>
              <span class="chapter-title">{{ chapter.title || `这里是第${cIndex + 1}的名称` }} ({{ chapter.videos?.length || 0 }}小节)</span>
            </div>
             <div class="videos-list">
               <div class="video-item" v-for="(video, vIndex) in chapter.videos" :key="video.id">
                 <div class="video-left">
                   <span class="video-number">{{ String(vIndex + 1).padStart(2, '0') }}.{{ video.title || `这里是第${vIndex + 1}小节的名称.MP4` }}</span>
                   <span class="video-title"></span>
                 </div>
                 <span class="video-size" v-if="video.file">{{ formatFileSize(video.file.size) }}</span>
               </div>
             </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 上传进度与结果 -->
    <div v-if="isUploading || uploadResult || errorMessage" class="upload-status-box">
      <div v-if="isUploading" class="upload-progress">
        <div class="progress-header">
          <h4><el-icon><Upload /></el-icon> 正在上传课程视频</h4>
          <div class="upload-status">{{ uploadStatus }}</div>
        </div>
        <el-progress 
          :percentage="uploadProgress" 
          :stroke-width="8"
          :show-text="true"
          :color="getProgressColor(uploadProgress)"
          class="progress-bar"
        />
        <div class="progress-details">
          <span>当前第 {{ currentUploadIndex + 1 }} / {{ uploadTotalCount }} 个视频</span>
        </div>
      </div>
      <div v-else-if="uploadResult" class="success-message">
        <div class="success-content">
          <el-icon class="success-icon"><CircleCheck /></el-icon>
          <h4>提交成功！</h4>
          <p>课程集合已提交审核</p>
        </div>
      </div>
      <div v-else-if="errorMessage" class="error-message">
        <el-alert :title="errorMessage" type="error" show-icon />
      </div>
    </div>

    <!-- 全屏遮罩：上传期间提示用户请耐心等待 -->
    <div v-if="isUploading" class="upload-overlay">
      <div class="overlay-content">
        <h3>正在提交课程，请稍候</h3>
        <p>当前为课程合集提交流程，包含多个视频与图片上传，时间可能较长，请不要关闭或离开此页面。</p>
        <div class="overlay-progress">
          <el-progress :percentage="uploadProgress" :stroke-width="10" :show-text="true" :color="getProgressColor(uploadProgress)" />
        </div>
        <div class="overlay-stats">
          <span>进度：第 {{ currentUploadIndex + 1 }} / {{ uploadTotalCount }} 个视频</span>
          <span v-if="uploadStatus"> ｜ {{ uploadStatus }}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { Upload, CircleCheck, ArrowDown } from '@element-plus/icons-vue'

defineProps({
  courseForm: {
    type: Object,
    required: true
  },
  chapters: {
    type: Array,
    required: true
  },
  courseCoverPreviewUrl: {
    type: String,
    default: ''
  },
  levelText: {
    type: String,
    required: true
  },
  totalVideos: {
    type: Number,
    required: true
  },
  formatFileSize: {
    type: Function,
    required: true
  },
  isUploading: {
    type: Boolean,
    default: false
  },
  uploadProgress: {
    type: Number,
    default: 0
  },
  uploadStatus: {
    type: String,
    default: ''
  },
  uploadResult: {
    type: Object,
    default: null
  },
  errorMessage: {
    type: String,
    default: ''
  },
  currentUploadIndex: {
    type: Number,
    default: 0
  },
  uploadTotalCount: {
    type: Number,
    required: true
  },
  getProgressColor: {
    type: Function,
    required: true
  }
})
</script>

<style scoped>
.upload-header { 
  text-align: center; 
  margin-bottom: 30px; 
}

.upload-header h3 { 
  color: #333; 
  margin-bottom: 10px; 
  font-size: 24px; 
  font-weight: 600; 
}

.upload-header p { 
  color: #666; 
  font-size: 14px; 
}

/* 预览 */
.preview-section { 
  display: flex;
  flex-direction: column;
  gap: 24px;
}

/* 课程信息卡片 */
.course-preview-card {
  border: 1px solid #ebeef5;
  border-radius: 12px;
  padding: 24px;
  background: #fff;
}

.course-preview-layout {
  display: grid;
  grid-template-columns: 300px 1fr;
  gap: 24px;
  align-items: flex-start;
}

.course-left {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.course-cover {
  width: 100%;
  aspect-ratio: 16/9;
  border-radius: 8px;
  overflow: hidden;
  background: #000;
  display: flex;
  align-items: center;
  justify-content: center;
}

.course-cover img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.course-basic-info {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.course-title {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
  color: #333;
  line-height: 1.4;
}

.course-meta {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
}

.course-category,
.course-price,
.course-level {
  padding: 4px 12px;
  background: #f0f0f0;
  border-radius: 16px;
  font-size: 12px;
  color: #666;
  white-space: nowrap;
}

.course-right {
  min-height: 200px;
}

/* 章节与视频卡片 */
.chapters-preview-card {
  border: 1px solid #ebeef5;
  border-radius: 12px;
  padding: 24px;
  background: #fff;
}

.section-title {
  margin: 0 0 20px 0;
  font-size: 16px;
  font-weight: 600;
  color: #333;
  text-align: left;
}

.chapters-list {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.chapter-item {
  border: 1px solid #f0f0f0;
  border-radius: 8px;
  overflow: hidden;
}

.chapter-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 16px;
  background: #fafafa;
  border-bottom: 1px solid #f0f0f0;
  cursor: pointer;
}

.expand-icon {
  font-size: 14px;
  color: #666;
  transition: transform 0.2s;
}

.chapter-title {
  font-size: 14px;
  font-weight: 500;
  color: #333;
}

.videos-list {
  padding: 0;
}

.video-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid #f8f8f8;
  transition: background-color 0.2s;
}

.video-item:hover {
  background-color: #f8f9fa;
}

.video-item:last-child {
  border-bottom: none;
}

.video-left {
  display: flex;
  align-items: center;
  flex: 1;
}

.video-number {
  font-size: 14px;
  color: #666;
  margin-right: 12px;
  min-width: 30px;
}

.video-title {
  font-size: 14px;
  color: #333;
  flex: 1;
}

.video-size {
  font-size: 12px;
  color: #999;
  flex-shrink: 0;
}

/* 富文本内容样式 */
.rich-text-content {
  line-height: 1.6;
  color: #555;
  background: #fafafa;
  padding: 16px;
  border-radius: 8px;
  border: 1px solid #e4e7ed;
  max-height: 400px;
  overflow-y: auto;
  font-size: 14px;
  text-align: left; /* 默认左对齐，但不覆盖内部元素的对齐设置 */
}

/* 保留富文本编辑器中设置的对齐样式 */
/* .rich-text-content :deep([style*="text-align"]) 会自动保持原有样式 */

/* Quill编辑器的对齐类 */
.rich-text-content :deep(.ql-align-left) {
  text-align: left !important;
}

.rich-text-content :deep(.ql-align-center) {
  text-align: center !important;
}

.rich-text-content :deep(.ql-align-right) {
  text-align: right !important;
}

.rich-text-content :deep(.ql-align-justify) {
  text-align: justify !important;
}

/* 通用对齐类支持 */
.rich-text-content :deep(.text-left) {
  text-align: left !important;
}

.rich-text-content :deep(.text-center) {
  text-align: center !important;
}

.rich-text-content :deep(.text-right) {
  text-align: right !important;
}

.rich-text-content :deep(.text-justify) {
  text-align: justify !important;
}

/* 支持行内样式的对齐 */
.rich-text-content :deep(p[style*="text-align: left"]),
.rich-text-content :deep(div[style*="text-align: left"]) {
  text-align: left !important;
}

.rich-text-content :deep(p[style*="text-align: center"]),
.rich-text-content :deep(div[style*="text-align: center"]) {
  text-align: center !important;
}

.rich-text-content :deep(p[style*="text-align: right"]),
.rich-text-content :deep(div[style*="text-align: right"]) {
  text-align: right !important;
}

.rich-text-content :deep(p[style*="text-align: justify"]),
.rich-text-content :deep(div[style*="text-align: justify"]) {
  text-align: justify !important;
}

.rich-text-content :deep(h1),
.rich-text-content :deep(h2),
.rich-text-content :deep(h3),
.rich-text-content :deep(h4),
.rich-text-content :deep(h5),
.rich-text-content :deep(h6) {
  margin: 16px 0 8px 0;
  color: #333;
  font-weight: 600;
}

.rich-text-content :deep(h1) { font-size: 24px; }
.rich-text-content :deep(h2) { font-size: 20px; }
.rich-text-content :deep(h3) { font-size: 18px; }
.rich-text-content :deep(h4) { font-size: 16px; }
.rich-text-content :deep(h5) { font-size: 14px; }
.rich-text-content :deep(h6) { font-size: 12px; }

.rich-text-content :deep(p) {
  margin: 8px 0;
  line-height: 1.6;
  /* 不设置 text-align，保留原有的对齐样式 */
}

.rich-text-content :deep(ul),
.rich-text-content :deep(ol) {
  margin: 8px 0;
  padding-left: 24px;
}

.rich-text-content :deep(li) {
  margin: 4px 0;
  line-height: 1.5;
}

.rich-text-content :deep(blockquote) {
  margin: 12px 0;
  padding: 8px 16px;
  background: #f5f5f5;
  border-left: 4px solid #409eff;
  color: #666;
  font-style: italic;
}

.rich-text-content :deep(code) {
  background: #f1f1f1;
  padding: 2px 4px;
  border-radius: 3px;
  font-family: 'Courier New', monospace;
  font-size: 13px;
  color: #e74c3c;
}

.rich-text-content :deep(pre) {
  background: #f8f8f8;
  padding: 12px;
  border-radius: 4px;
  overflow-x: auto;
  margin: 12px 0;
}

.rich-text-content :deep(pre code) {
  background: none;
  padding: 0;
  color: #333;
}

.rich-text-content :deep(strong),
.rich-text-content :deep(b) {
  font-weight: 600;
  color: #333;
}

.rich-text-content :deep(em),
.rich-text-content :deep(i) {
  font-style: italic;
  color: #666;
}

.rich-text-content :deep(a) {
  color: #409eff;
  text-decoration: none;
}

.rich-text-content :deep(a:hover) {
  text-decoration: underline;
}

.rich-text-content :deep(img) {
  max-width: 100%;
  height: auto;
  border-radius: 4px;
  margin: 8px 0;
}

.rich-text-content :deep(table) {
  width: 100%;
  border-collapse: collapse;
  margin: 12px 0;
  border: 1px solid #e4e7ed;
}

.rich-text-content :deep(th),
.rich-text-content :deep(td) {
  padding: 8px 12px;
  border: 1px solid #e4e7ed;
  text-align: left;
}

.rich-text-content :deep(th) {
  background: #f5f7fa;
  font-weight: 600;
  color: #333;
}

.rich-text-content :deep(hr) {
  border: none;
  border-top: 1px solid #e4e7ed;
  margin: 16px 0;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .course-preview-layout {
    grid-template-columns: 1fr;
    gap: 16px;
  }
  
  .course-left {
    order: 1;
  }
  
  .course-right {
    order: 2;
    min-height: auto;
  }
  
  .video-item {
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
  }
  
  .video-left {
    width: 100%;
  }
  
  .video-size {
    align-self: flex-end;
  }
}

/* 进度与结果 */
.upload-status-box { 
  margin-top: 24px; 
}

.progress-header { 
  display: flex; 
  justify-content: space-between; 
  align-items: center; 
  margin-bottom: 8px; 
}

.success-message { 
  padding: 16px; 
  background: #f0f9eb; 
  border: 1px solid #e1f3d8; 
  border-radius: 8px; 
}

.success-icon { 
  color: #67c23a; 
  font-size: 24px; 
  margin-right: 8px; 
}

/* 全屏上传遮罩层 */
.upload-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.75);
  z-index: 2000;
  display: flex;
  align-items: center;
  justify-content: center;
}

.overlay-content {
  width: min(560px, 90%);
  background: rgba(20, 20, 20, 0.9);
  border: 1px solid rgba(255, 255, 255, 0.12);
  box-shadow: 0 10px 40px rgba(0,0,0,0.5);
  border-radius: 16px;
  padding: 28px 24px;
  color: #fff;
  text-align: center;
}

.overlay-icon { 
  font-size: 36px; 
  color: #8cc5ff; 
  margin-bottom: 8px; 
}

.overlay-content h3 { 
  margin: 6px 0 10px; 
  font-size: 20px; 
  font-weight: 700; 
  color: #fff; 
}

.overlay-content p { 
  margin: 0 0 14px; 
  color: #ddd; 
  line-height: 1.6; 
}

.overlay-progress { 
  margin: 14px 0; 
}

.overlay-stats { 
  margin-top: 6px; 
  color: #cfd8dc; 
  font-size: 13px; 
}
</style>
