<template>
  <div v-if="visible" class="modal-overlay" @click.self="closeModal">
    <div class="modal-wrapper">
      <!-- 独立的标题栏区域 -->
      <div class="modal-header">
        <h3 class="modal-title">作品详情</h3>
        <button class="close-button" @click="closeModal">
          X
        </button>
      </div>

      <!-- 内容区域 -->
      <div class="modal-content">
        <div v-if="task" class="task-detail-container">
          <!-- 左侧媒体区 -->
          <div class="media-container">
            <div class="video-wrapper">
              <video v-if="task.coverUrl" :src="task.coverUrl" controls autoplay class="task-video"></video>
              <img 
                v-else-if="task.referenceImageUrl && !task.coverUrl" 
                :src="task.referenceImageUrl" 
                alt="作品参考图" 
                class="task-image" 
                @click="openImagePreview(task.referenceImageUrl)"
              >
              <div v-else class="media-placeholder">无可用媒体</div>
            </div>
            
            <!-- 参考图/分镜图展示 -->
            <div v-if="task.referenceImageUrl && task.coverUrl" class="reference-images-section">
              <div class="section-title">参考图</div>
              <div class="thumbnail-wrapper">
                <img 
                  :src="task.referenceImageUrl" 
                  alt="参考图" 
                  class="reference-thumbnail" 
                  @click="openImagePreview(task.referenceImageUrl)"
                >
              </div>
            </div>

            <div v-if="task.storyboardImages && task.storyboardImages.length > 0" class="reference-images-section">
              <div class="section-title">分镜图</div>
              <div class="storyboard-grid">
                <img 
                  v-for="(img, idx) in task.storyboardImages" 
                  :key="idx"
                  :src="img" 
                  alt="分镜图" 
                  class="storyboard-thumbnail" 
                  @click="openImagePreview(img)"
                >
              </div>
            </div>
          </div>

          <!-- 右侧信息区 -->
          <div class="info-container">
            <div class="info-item prompt-section">
              <div class="prompt-header">
                <strong>提示词</strong>
                <button class="copy-btn" @click="handleCopyPrompt" :disabled="copyLoading">
                  <svg v-if="!copied" width="14" height="14" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                    <rect x="8" y="8" width="12" height="12" rx="2" stroke="currentColor" stroke-width="2"/>
                    <path d="M16 8V6a2 2 0 00-2-2H6a2 2 0 00-2 2v8a2 2 0 002 2h2" stroke="currentColor" stroke-width="2"/>
                  </svg>
                  <svg v-else width="14" height="14" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                    <path d="M20 6L9 17l-5-5" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                  </svg>
                  <span>{{ copied ? '已复制' : '复制' }}</span>
                </button>
              </div>
              <div class="prompt-container">
                <div class="prompt-content">{{ formatPrompt(task.title) || '无' }}</div>
              </div>
            </div>
            
            <div class="details-grid">
              <div class="info-card">
                <div class="info-label">作品类型</div>
                <div class="info-value">{{ task.description || '未知' }}</div>
              </div>
              <div class="info-card">
                <div class="info-label">任务编号</div>
                <div class="info-value">{{ task.id || '未知' }}</div>
              </div>
              <div v-if="task.views !== undefined && task.views !== null" class="info-card">
                <div class="info-label">浏览量</div>
                <div class="info-value">{{ task.views }}</div>
              </div>
              <div v-if="task.likes !== undefined && task.likes !== null" class="info-card">
                <div class="info-label">点赞量</div>
                <div class="info-value">{{ task.likes }}</div>
              </div>
              <div class="info-card full-width">
                <div class="info-label">创建时间</div>
                <div class="info-value">{{ formatDate(task.createdAt) || '未知' }}</div>
              </div>
            </div>
          </div>
        </div>
        <div v-else class="loading-state">
          <p>正在加载任务详情...</p>
        </div>
      </div>
    </div>

    <!-- 图片预览层 -->
    <div 
      v-if="imagePreviewVisible" 
      class="image-preview-overlay" 
      @click="closeImagePreview"
    >
      <button class="preview-close-btn" @click="closeImagePreview">
        X
      </button>
      <img :src="imagePreviewSrc" class="image-preview-img" alt="preview" @click.stop />
    </div>
  </div>
</template>

<script setup>
import { defineProps, defineEmits, ref } from 'vue'
import { ElMessage } from 'element-plus'

const props = defineProps({
  visible: {
    type: Boolean,
    required: true,
  },
  task: {
    type: Object,
    default: null,
  },
});

const emit = defineEmits(['close']);

const copied = ref(false)
const copyLoading = ref(false)
const imagePreviewVisible = ref(false)
const imagePreviewSrc = ref('')

const closeModal = () => {
  emit('close');
  // 重置复制状态
  copied.value = false
};

const formatDate = (dateString) => {
  if (!dateString) return '';
  return new Date(dateString).toLocaleString('zh-CN', {
    year: 'numeric',
    month: 'long',
    day: 'numeric',
    hour: '2-digit',
    minute: '2-digit',
  });
};

// 格式化并截断提示词
const formatPrompt = (text) => {
  if (!text) return ''
  // 截断到200字
  let formatted = text.length <= 200 ? text : text.substring(0, 200) + '...'
  return formatted
}

// 复制提示词
const handleCopyPrompt = async () => {
  if (!props.task?.title) {
    ElMessage.warning('无提示词可复制')
    return
  }

  copyLoading.value = true
  
  try {
    // 尝试使用现代 Clipboard API
    if (navigator.clipboard && navigator.clipboard.writeText) {
      await navigator.clipboard.writeText(props.task.title)
      copied.value = true
      ElMessage.success('提示词已复制到剪贴板')
    } else {
      // 降级方案：使用 textarea
      const textarea = document.createElement('textarea')
      textarea.value = props.task.title
      textarea.style.position = 'fixed'
      textarea.style.opacity = '0'
      document.body.appendChild(textarea)
      textarea.select()
      
      const successful = document.execCommand('copy')
      document.body.removeChild(textarea)
      
      if (successful) {
        copied.value = true
        ElMessage.success('提示词已复制到剪贴板')
      } else {
        throw new Error('复制失败')
      }
    }
    
    // 3秒后重置复制状态
    setTimeout(() => {
      copied.value = false
    }, 3000)
  } catch (error) {
    console.error('复制失败:', error)
    ElMessage.error('复制失败，请手动复制')
  } finally {
    copyLoading.value = false
  }
}

// 预览图片（分镜图/参考图）
const openImagePreview = (src) => {
  if (!src) return
  imagePreviewSrc.value = src
  imagePreviewVisible.value = true
}

// 关闭图片预览
const closeImagePreview = () => {
  imagePreviewVisible.value = false
  imagePreviewSrc.value = ''
}
</script>

<style scoped>
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.75);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
  backdrop-filter: blur(8px);
  padding: 20px;
}

.modal-wrapper {
  width: 90%;
  max-width: 1000px;
  max-height: 90vh;
  background: #1a1a2e;
  border-radius: 16px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.4);
  border: 1px solid rgba(255, 255, 255, 0.1);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* 独立的标题栏 - 关闭按钮在这里不会重叠 */
.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 24px;
  background: linear-gradient(135deg, rgba(26, 195, 255, 0.08) 0%, rgba(15, 23, 42, 0.9) 100%);
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  flex-shrink: 0;
}

.modal-title {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
  color: #fff;
  letter-spacing: 0.5px;
}

.close-button {
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.15);
  color: white;
  cursor: pointer;
  width: 36px;
  height: 36px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s ease;
  outline: none;
  flex-shrink: 0;
}

.close-button:hover {
  background: rgba(255, 59, 48, 0.15);
  border-color: rgba(255, 59, 48, 0.4);
  transform: scale(1.05);
}

.close-button svg {
  stroke: currentColor;
}

/* 内容区域 */
.modal-content {
  flex: 1;
  overflow-y: auto;
  padding: 24px;
  background: #1a1a2e;
}

.task-detail-container {
  display: grid;
  grid-template-columns: 380px 1fr;
  gap: 24px;
}

.media-container {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.video-wrapper {
  width: 100%;
  height: 380px;
  background: linear-gradient(135deg, #0f1629 0%, #1a1a2e 100%);
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  border: 1px solid rgba(255, 255, 255, 0.08);
}

.task-video, .task-image {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  border-radius: 8px;

}

.task-image {
  cursor: zoom-in;
  transition: transform 0.3s ease;
}

.task-image:hover {
  transform: scale(1.02);
}

.media-placeholder {
  color: rgba(255, 255, 255, 0.3);
  font-size: 14px;
}

.info-container {
  color: #fff;
  text-align: left;
  overflow-y: auto; /* 信息区独立滚动 */
  padding-right: 15px; /* 为滚动条留出空间 */
}

.info-item {
  margin-bottom: 20px;
}

.info-item strong {
  display: block;
  margin-bottom: 8px;
  color: #a9a9d4;
  font-size: 14px;
  font-weight: 500;
}

/* 提示词区域样式 */
.prompt-section {
  background: rgba(15, 23, 42, 0.6);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 12px;
  padding: 0;
  margin-bottom: 24px;
}

.prompt-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  background: rgba(255, 255, 255, 0.02);
  border-radius: 12px 12px 0 0;
}

.prompt-header strong {
  margin: 0;
  color: #e2e8f0;
  font-size: 13px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.copy-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  background: rgba(26, 195, 255, 0.1);
  border: 1px solid rgba(26, 195, 255, 0.25);
  border-radius: 6px;
  color: #1AC3FF;
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
}

.copy-btn:hover:not(:disabled) {
  background: rgba(26, 195, 255, 0.2);
  border-color: #1AC3FF;
  transform: translateY(-1px);
  box-shadow: 0 2px 8px rgba(26, 195, 255, 0.3);
}

.copy-btn:active:not(:disabled) {
  transform: translateY(0);
}

.copy-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.copy-btn svg {
  flex-shrink: 0;
}

.copy-btn span {
  white-space: nowrap;
}

.prompt-container {
  padding: 16px;
  max-height: 300px;
  overflow-y: auto;
}

.prompt-container::-webkit-scrollbar {
  width: 6px;
}

.prompt-container::-webkit-scrollbar-track {
  background: rgba(255, 255, 255, 0.05);
  border-radius: 3px;
}

.prompt-container::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.15);
  border-radius: 3px;
}

.prompt-container::-webkit-scrollbar-thumb:hover {
  background: rgba(255, 255, 255, 0.25);
}

.prompt-content {
  color: #cbd5e1;
  font-size: 14px;
  line-height: 1.8;
  white-space: pre-wrap;
  word-wrap: break-word;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', sans-serif;
}

.details-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
}

.info-card {
  background: linear-gradient(135deg, rgba(26, 195, 255, 0.08) 0%, rgba(15, 156, 255, 0.05) 100%);
  border: 1px solid rgba(26, 195, 255, 0.15);
  border-radius: 12px;
  padding: 16px;
  transition: all 0.3s ease;
}

.info-card:hover {
  background: linear-gradient(135deg, rgba(26, 195, 255, 0.12) 0%, rgba(15, 156, 255, 0.08) 100%);
  border-color: rgba(26, 195, 255, 0.25);
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(26, 195, 255, 0.15);
}

.info-card.full-width {
  grid-column: 1 / -1;
}

.info-label {
  font-size: 11px;
  font-weight: 600;
  color: rgba(255, 255, 255, 0.5);
  text-transform: uppercase;
  letter-spacing: 0.8px;
  margin-bottom: 8px;
}

.info-value {
  font-size: 14px;
  font-weight: 500;
  color: #1AC3FF;
  word-wrap: break-word;
}

/* 参考图/分镜图区域 */
.reference-images-section {
  background: rgba(255, 255, 255, 0.03);
  padding: 12px;
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.06);
}

.section-title {
  font-size: 12px;
  font-weight: 600;
  color: rgba(255, 255, 255, 0.6);
  text-transform: uppercase;
  letter-spacing: 1px;
  margin-bottom: 10px;
}

.thumbnail-wrapper {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: center; /* 居中对齐 */
  align-items: center;
}

.reference-thumbnail {
  width: 100%;
  max-width: 160px;
  height: 120px;
  object-fit: contain; /* 改为contain以保持完整图片 */
  border-radius: 8px;
  cursor: zoom-in;
  transition: all 0.3s ease;
  border: none; /* 移除边框 */
}

.reference-thumbnail:hover {
  transform: scale(1.05);
}

.storyboard-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.storyboard-thumbnail {
  width: 100%;
  height: 80px;
  object-fit: cover;
  border-radius: 6px;
  cursor: zoom-in;
  transition: all 0.3s ease;
  border: 1px solid rgba(26, 195, 255, 0.15);
}

.storyboard-thumbnail:hover {
  transform: scale(1.08);
  border-color: rgba(26, 195, 255, 0.6);
  z-index: 10;
}

/* 图片预览层 */
.image-preview-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.92);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2000;
  backdrop-filter: blur(10px);
  animation: fadeIn 0.2s ease;
  overflow: hidden; /* 防止内容溢出 */
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

.preview-close-btn {
  position: fixed; /* 改为fixed,相对于视口定位 */
  top: max(30px, env(safe-area-inset-top)); /* 支持刘海屏 */
  right: max(50px, env(safe-area-inset-right)); /* 支持刘海屏 */
  width: 44px;
  height: 44px;
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 50%;
  color: white;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s ease;
  z-index: 2001; /* 确保在预览层之上 */
  box-sizing: border-box; /* 确保border计入宽高 */
}

.preview-close-btn:hover {
  background: rgba(255, 59, 48, 0.2);
  border-color: rgba(255, 59, 48, 0.5);
  transform: rotate(90deg) scale(1.1);
}

.image-preview-img {
  max-width: 90vw;
  max-height: 90vh;
  border-radius: 12px;
  animation: zoomIn 0.3s ease;
}

@keyframes zoomIn {
  from {
    transform: scale(0.9);
    opacity: 0;
  }
  to {
    transform: scale(1);
    opacity: 1;
  }
}

/* 加载状态 */
.loading-state {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 300px;
  color: rgba(255, 255, 255, 0.5);
}

/* 滚动条样式 */
.modal-content::-webkit-scrollbar,
.info-container::-webkit-scrollbar {
  width: 6px;
}

.modal-content::-webkit-scrollbar-track,
.info-container::-webkit-scrollbar-track {
  background: rgba(255, 255, 255, 0.05);
  border-radius: 3px;
}

.modal-content::-webkit-scrollbar-thumb,
.info-container::-webkit-scrollbar-thumb {
  background: rgba(26, 195, 255, 0.3);
  border-radius: 3px;
}

.modal-content::-webkit-scrollbar-thumb:hover,
.info-container::-webkit-scrollbar-thumb:hover {
  background: rgba(26, 195, 255, 0.5);
}

/* 响应式 */
@media (max-width: 900px) {
  .task-detail-container {
    grid-template-columns: 1fr;
  }
  
  .video-wrapper {
    height: 300px;
  }
  
  .storyboard-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media (max-width: 600px) {
  .modal-wrapper {
    width: 95%;
    max-height: 95vh;
  }
  
  .modal-header {
    padding: 12px 16px;
  }
  
  .modal-title {
    font-size: 16px;
  }
  
  .modal-content {
    padding: 16px;
  }
  
  .details-grid {
    grid-template-columns: 1fr;
  }
  
  .preview-close-btn {
    top: 10px;
    right: 20px;
    width: 40px;
    height: 40px;
  }
}

/* 确保关闭按钮始终在视口内 */
@media (max-width: 400px) {
  .preview-close-btn {
    top: 8px;
    right: 8px;
    width: 36px;
    height: 36px;
  }
}
</style>
