<template>
  <div class="output-item" :class="{ clickable: output.status === 'completed' }" @click="$emit('click')">
    <div class="output-icon">
      <FileMarkdownOutlined />
    </div>
    <div class="output-info">
      <div class="output-title">{{ output.title || typeLabel }}</div>
      <div class="output-meta">
        <span>{{ typeLabel }}</span>
        <template v-if="sourceCount">
          <span class="meta-sep">&middot;</span>
          <span>{{ sourceCount }}个来源</span>
        </template>
        <template v-if="output.created_at">
          <span class="meta-sep">&middot;</span>
          <span>{{ formatTime(output.created_at) }}</span>
        </template>
        <a-tag v-if="output.status !== 'completed'" :color="statusColor" size="small" style="margin-left: 4px">{{ statusLabel }}</a-tag>
      </div>
    </div>
    <a-dropdown :trigger="['click']" placement="bottomRight" @click.stop>
      <MoreOutlined class="output-more" />
      <template #overlay>
        <a-menu>
          <a-menu-item danger @click.stop="$emit('delete')">
            <DeleteOutlined /> 删除
          </a-menu-item>
        </a-menu>
      </template>
    </a-dropdown>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import {
  FileMarkdownOutlined,
  MoreOutlined,
  DeleteOutlined,
} from '@ant-design/icons-vue'
import type { NotebookOutput } from '@/types/notebook'

const props = defineProps<{
  output: NotebookOutput
}>()

defineEmits<{
  click: []
  delete: []
}>()

const OUTPUT_TYPE_LABELS: Record<string, string> = {
  briefing: '简报',
  study_guide: '学习指南',
  blog_post: '博文',
  science_handbook: '科普手册',
  implementation_plan: '实施方案',
  speech_draft: '讲话稿',
  policy_brief: '政策简报',
  summary_report: '综合报告',
  custom: '自定义',
}

const typeLabel = computed(() => OUTPUT_TYPE_LABELS[props.output.output_type] || props.output.output_type)

const sourceCount = computed(() => {
  const ctx = props.output.context_json
  return ctx?.source_count || 0
})

const statusColor = computed(() => {
  switch (props.output.status) {
    case 'completed': return 'success'
    case 'generating': return 'processing'
    case 'failed': return 'error'
    default: return 'default'
  }
})

const statusLabel = computed(() => {
  switch (props.output.status) {
    case 'completed': return '已完成'
    case 'generating': return '生成中'
    case 'failed': return '失败'
    default: return props.output.status
  }
})

function formatTime(ts: string): string {
  const d = new Date(ts)
  return `${d.getMonth() + 1}-${d.getDate()} ${String(d.getHours()).padStart(2, '0')}:${String(d.getMinutes()).padStart(2, '0')}`
}
</script>

<style scoped lang="less">
.output-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border: 1px solid #f0f0f0;
  border-radius: 6px;
  margin-bottom: 8px;
  background: #fff;
  transition: all 0.2s;

  &.clickable {
    cursor: pointer;

    &:hover {
      border-color: #d9d9d9;
      box-shadow: 0 1px 4px rgba(0, 0, 0, 0.06);
    }
  }
}

.output-icon {
  font-size: 20px;
  color: #1890ff;
}

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

  .output-title {
    font-size: 13px;
    font-weight: 500;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .output-meta {
    display: flex;
    align-items: center;
    gap: 4px;
    margin-top: 4px;
    font-size: 12px;
    color: #8c8c8c;

    .meta-sep {
      color: #d9d9d9;
    }
  }
}

.output-more {
  cursor: pointer;
  color: #8c8c8c;
  padding: 2px;

  &:hover {
    color: #1890ff;
  }
}
</style>
