<script setup lang="ts">
import { computed, onMounted, ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { showToast } from 'vant'
import { fetchAnnouncementDetail, type Announcement } from '@/features/announcement'
import { renderAnnouncementMarkdown } from '@/features/announcement/utils/markdown'
import { formatDateTime } from '@/shared/utils/time'

const route = useRoute()
const router = useRouter()
const loading = ref(false)
const announcement = ref<Announcement | null>(null)

const categories = [
  { value: 'notice', label: '通知公告' },
  { value: 'tutorial', label: '使用教程' },
  { value: 'rule', label: '规则说明' },
  { value: 'faq', label: '常见问题' },
]

const renderedContent = computed(() => {
  if (!announcement.value?.content) return ''
  return renderAnnouncementMarkdown(announcement.value.content)
})

onMounted(loadAnnouncement)

async function loadAnnouncement() {
  const id = Number(route.params.id)
  if (!id) {
    router.replace('/m/announcements')
    return
  }

  loading.value = true
  try {
    announcement.value = await fetchAnnouncementDetail(id)
  } catch {
    showToast({ message: '公告加载失败', icon: 'cross' })
    router.replace('/m/announcements')
  } finally {
    loading.value = false
  }
}

function getCategoryLabel(category: string) {
  return categories.find(item => item.value === category)?.label || '公告'
}
</script>

<template>
  <main class="mobile-announcement-detail">
    <header class="page-header">
      <button class="back-btn" type="button" @click="router.back()">
        <van-icon name="arrow-left" :size="20" />
      </button>
      <h1>公告详情</h1>
      <span class="header-spacer"></span>
    </header>

    <van-loading v-if="loading" class="state-loading" size="24px" vertical>
      正在加载公告...
    </van-loading>

    <article v-else-if="announcement" class="detail-card">
      <div class="detail-tags">
        <span class="category-tag">{{ getCategoryLabel(announcement.category) }}</span>
        <span v-if="announcement.is_pinned" class="state-tag pinned">置顶</span>
        <span v-if="announcement.is_important" class="state-tag important">重要</span>
      </div>

      <h2>{{ announcement.title }}</h2>

      <div class="detail-meta">
        <span>{{ formatDateTime(announcement.published_at || announcement.created_at) }}</span>
        <span>浏览 {{ announcement.view_count }}</span>
      </div>

      <div class="detail-divider"></div>
      <div class="detail-content" v-html="renderedContent"></div>
    </article>
  </main>
</template>

<style scoped>
.mobile-announcement-detail {
  min-height: 100dvh;
  background: #f6f8fb;
  color: #17233d;
}

.page-header {
  position: sticky;
  top: 0;
  z-index: 100;
  display: grid;
  grid-template-columns: 44px 1fr 44px;
  align-items: center;
  height: 48px;
  padding: 0 8px;
  border-bottom: 1px solid #edf0f5;
  background: rgba(255, 255, 255, 0.96);
  backdrop-filter: blur(10px);
}

.page-header h1 {
  margin: 0;
  color: #111827;
  font-size: 17px;
  font-weight: 800;
  text-align: center;
}

.back-btn {
  display: grid;
  width: 40px;
  height: 40px;
  place-items: center;
  border: 0;
  background: transparent;
  color: #374151;
}

.state-loading {
  padding-top: 80px;
}

.detail-card {
  margin: 12px;
  padding: 16px;
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  background: #fff;
}

.detail-tags,
.detail-meta {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
}

.category-tag,
.state-tag {
  display: inline-flex;
  align-items: center;
  height: 22px;
  padding: 0 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 800;
}

.category-tag {
  background: #fff7ed;
  color: #c2410c;
}

.state-tag.pinned {
  background: #fffbeb;
  color: #b45309;
}

.state-tag.important {
  background: #fef2f2;
  color: #dc2626;
}

.detail-card h2 {
  margin: 12px 0 10px;
  color: #0f172a;
  font-size: 20px;
  font-weight: 900;
  line-height: 1.38;
}

.detail-meta {
  color: #94a3b8;
  font-size: 12px;
  font-weight: 700;
}

.detail-divider {
  height: 1px;
  margin: 16px 0;
  background: #edf0f5;
}

.detail-content {
  color: #334155;
  font-size: 15px;
  line-height: 1.75;
  word-break: break-word;
}

.detail-content :deep(p) {
  margin: 0 0 14px;
}

.detail-content :deep(h1),
.detail-content :deep(h2),
.detail-content :deep(h3) {
  margin: 18px 0 10px;
  color: #0f172a;
  line-height: 1.35;
}

.detail-content :deep(h1) {
  font-size: 22px;
}

.detail-content :deep(h2) {
  font-size: 19px;
}

.detail-content :deep(h3) {
  font-size: 17px;
}

.detail-content :deep(ul),
.detail-content :deep(ol) {
  margin: 0 0 14px;
  padding-left: 20px;
}

.detail-content :deep(blockquote) {
  margin: 0 0 14px;
  padding: 8px 10px;
  border-left: 3px solid #ff6a00;
  background: #fff7ed;
  color: #7c2d12;
}

.detail-content :deep(img) {
  max-width: 100%;
  border-radius: 8px;
}
</style>
