<template>
  <div class="export-list">
    <!-- 导出样式选择 -->
    <div class="export-style-grid">
      <div
        v-for="item in exportStyles"
        :key="item.type"
        class="export-style-item"
        @click="handleSelect(item)"
      >
        <div class="style-icon" :style="{ color: item.color }">
          <SvgIcon :icon-class="item.icon" size="24px" />
        </div>
        <div class="style-name">{{ item.name }}</div>
      </div>
    </div>

    <!-- 导出结果列表 -->
    <div class="export-result-section">
      <div v-if="exportResults.length > 0" class="result-list">
        <div v-for="(result, index) in exportResults" :key="index" class="result-item">
          <div class="result-icon" :style="{ color: result.color }">
            <SvgIcon :icon-class="result.icon" size="30px" />
          </div>
          <div class="result-info">
            <div class="result-name">{{ result.name }}</div>
            <div class="result-time">{{ formatTime(result.exportTime || Date.now()) }}</div>
          </div>
          <div class="result-actions">
            <el-popover
              placement="bottom-end"
              :width="120"
              :show-arrow="false"
              trigger="click"
              popper-class="action-popover"
            >
              <template #reference>
                <button class="more-btn" @click.stop>
                  <SvgIcon icon-class="more" class="more-icon" size="16px" />
                </button>
              </template>
              <div class="action-menu">
                <div class="action-item" @click="previewResult(result)">
                  <SvgIcon icon-class="YuLan" size="14px" />
                  <span>预览文件</span>
                </div>
                <div class="action-item" @click="downloadResult(result)">
                  <SvgIcon icon-class="XiaZai" size="14px" />
                  <span>下载文件</span>
                </div>
                <div class="action-item" @click="shareResult(result)">
                  <SvgIcon icon-class="FenXiang" size="14px" />
                  <span>分享文件</span>
                </div>
                <div class="action-item" @click="deleteResult(result)">
                  <SvgIcon icon-class="ShanChu" size="14px" />
                  <span>删除文件</span>
                </div>
              </div>
            </el-popover>
          </div>
        </div>
      </div>
      <div v-else class="result-empty">暂无导出记录</div>
    </div>

    <!-- 删除确认对话框 -->
    <DeleteConfirmDialog
      v-model="dialogVisible"
      title="确认删除该导出记录 ?"
      content="删除后，该记录无法恢复 !"
      @confirm="confirmDelete"
    />
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import SvgIcon from '@/components/SvgIcon/index.vue'
import DeleteConfirmDialog from '@/components/DeleteConfirmDialog/index.vue'
import { useAgentsStore } from '@/stores'

const agentsStore = useAgentsStore()

// 事件定义
const emit = defineEmits<{
  (e: 'close'): void
}>()

// 导出样式类型
interface ExportStyle {
  type: string
  name: string
  icon: string
  color: string
}

// 导出结果类型
interface ExportResult {
  name: string
  icon: string
  type: string
  color: string
  exportTime?: number // 时间戳
}

// 导出样式数据
const exportStyles = ref<ExportStyle[]>([
  { type: 'doc', name: '文本报告', icon: 'DOCX', color: '#00A2D2' },
  { type: 'markdown', name: 'Markdown', icon: 'Markdown', color: '#FF5B5B' },
  { type: 'mindmap', name: '思维导图', icon: 'SiWeiDaoTu', color: '#FF5BEE' },
  { type: 'infographic', name: '信息图', icon: 'XinXiTu', color: '#FCE460' },
  { type: 'excel', name: '数据表格', icon: 'XLSX', color: '#65AE00' },
  { type: 'ppt', name: '演示文稿', icon: 'PPT', color: '#FF7914' }
])

// 导出结果列表
const exportResults = ref<ExportResult[]>([])

// 删除对话框相关
const dialogVisible = ref(false)
const resultToDelete = ref<ExportResult | null>(null)

// 格式化时间显示
const formatTime = (timestamp: number): string => {
  const now = Date.now()
  const diff = now - timestamp
  const minutes = Math.floor(diff / 60000)
  const hours = Math.floor(diff / 3600000)
  const days = Math.floor(diff / 86400000)

  if (minutes < 1) return '刚刚'
  if (minutes < 60) return `${minutes}分钟前`
  if (hours < 24) return `${hours}小时前`
  if (days < 7) return `${days}天前`

  const date = new Date(timestamp)
  return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(
    date.getDate()
  ).padStart(2, '0')}`
}

// 预览文件
const previewResult = (_result: ExportResult) => {
  ElMessage.success('预览功能开发中')
}

// 下载文件
const downloadResult = (_result: ExportResult) => {
  ElMessage.success('下载功能开发中')
}

// 分享
const shareResult = (_result: ExportResult) => {
  ElMessage.success('分享功能开发中')
}

// 删除
const deleteResult = (result: ExportResult) => {
  resultToDelete.value = result
  dialogVisible.value = true
}

// 确认删除
const confirmDelete = () => {
  if (!resultToDelete.value) return

  const index = exportResults.value.indexOf(resultToDelete.value)
  if (index > -1) {
    exportResults.value.splice(index, 1)
    ElMessage.success('删除成功')
  }
  dialogVisible.value = false
  resultToDelete.value = null
}

// 选择导出样式
const handleSelect = (item: ExportStyle) => {
  const currentTask = agentsStore.currentTask
  if (!currentTask) {
    ElMessage.warning('请先选择任务')
    return
  }

  // 生成导出名称：任务名称 + 样式名称
  const taskName = currentTask.StepName || '未知任务'
  const exportName = `${taskName}${item.name}`

  // 添加到导出结果列表（添加到最前面）
  const newItem = {
    name: exportName,
    icon: item.icon,
    type: item.type,
    color: item.color,
    exportTime: Date.now()
  }

  exportResults.value.unshift(newItem)

  console.log('导出任务:', { task: currentTask, style: item })
  ElMessage.success(`已开始导出: ${item.name}`)
}
</script>

<style scoped lang="scss">
.export-list {
  padding: 16px;
  height: 100%;
  display: flex;
  flex-direction: column;
}

.export-style-grid {
  display: grid;
  grid-template-columns: repeat(3, 152px);
  gap: 12px;
  justify-content: center;
  margin: 0 -16px 16px -16px;
  padding: 0 16px 16px 16px;
  border-bottom: 1px solid var(--color-border-separate);
}

.export-style-item {
  width: 152px;
  height: 50px;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: flex-start;
  padding: 0 12px;
  gap: 8px;
  border-radius: 12px;
  background: var(--color-bg-detail);
  cursor: pointer;
  transition: all 0.2s;

  &:hover {
    background: var(--color-bg-content-hover);
  }

  .style-icon {
    flex-shrink: 0;
    color: var(--color-text-plan-item);
  }

  .style-name {
    font-size: 14px;
    font-weight: 500;
    color: var(--color-text-plan-item);
    white-space: nowrap;
  }
}

.export-result-section {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;

  .result-title {
    font-size: 14px;
    font-weight: 500;
    color: var(--color-text-primary);
    margin-bottom: 12px;
  }

  .result-list {
    flex: 1;
    overflow-y: auto;
    max-height: calc(100vh - 430px);
  }

  .result-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px;
    margin-bottom: 8px;
    border-radius: 8px;
    background: var(--color-bg-three);
    transition: background 0.2s;

    &:hover {
      background: var(--color-bg-hover);

      .result-name {
        color: var(--color-text-plan-item-hover);
      }
    }
    .result-icon {
      color: var(--color-text-plan-item);
    }

    .result-info {
      flex: 1;
      min-width: 0;

      .result-name {
        font-size: 14px;
        color: var(--color-text-placeholder);
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        transition: color 0.2s;
      }

      .result-time {
        font-size: 12px;
        color: var(--color-text-plan-item);
        margin-top: 4px;
      }
    }

    .result-actions {
      display: flex;
      gap: 8px;
      margin-left: 12px;
    }
  }

  // 更多按钮
  .more-btn {
    background: none;
    border: none;
    cursor: pointer;
    padding: 4px;
    border-radius: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    transition: background-color 0.2s;

    .more-icon {
      color: var(--color-text-plan-item);
      transition: color 0.2s;
    }

    &:hover {
      background-color: var(--color-card-border-three);

      .more-icon {
        color: var(--color-text-plan-item-hover);
      }
    }
  }

  // 操作菜单
  .action-menu {
    display: flex;
    flex-direction: column;
  }

  .result-empty {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--color-text-placeholder);
    font-size: 14px;
  }
}
</style>

<style lang="scss">
.action-popover {
  padding: 0 !important;
  border-radius: 8px !important;
  width: 120px !important;
  min-width: 120px !important;
  background: var(--color-bg-three) !important;
  border: 1px solid var(--color-card-border-three) !important;
}

.action-menu {
  display: flex;
  flex-direction: column;

  .el-popover__content {
    padding: 0;
  }
}

.action-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
  cursor: pointer;
  font-size: 13px;
  color: var(--color-text-plan-item);
  transition: background-color 0.2s, color 0.2s;

  .svg-icon {
    color: var(--color-text-plan-item);
    transition: color 0.2s;
  }

  &:hover {
    background-color: var(--color-card-border-three);
    color: var(--color-text-plan-item-hover);

    .svg-icon {
      color: var(--color-text-plan-item-hover);
    }
  }

  &:first-child {
    border-radius: 8px 8px 0 0;
  }

  &:last-child {
    border-radius: 0 0 8px 8px;
  }
}
</style>
