<template>
  <section class="article-container">
    <div class="loading-box" v-show="loading">
      <a-spin :spinning="loading" size="large" />
    </div>
    <template v-if="!loading">
      <template v-if="articleType != 'header'">
        <!-- 文章标题 -->
        <h1 class="article-title">{{ dataInfo ? dataInfo.title : '' }}</h1>

        <!-- 文章元信息 -->
        <!-- <div class="article-meta">
          <div class="author-info">
            <img :src="article.author.avatar" class="author-avatar" alt="作者头像" />
            <span class="author-name">{{ article.author.name }}</span>
          </div>
          <div class="publish-time" v-if="activeInfo">
            <span>{{
              $t('system["updateTimeTxt"]', {
                updateTime: formatDate(activeInfo.publishTime, locale),
              })
            }}</span>
          </div>
        </div> -->
      </template>

      <!-- 富文本内容区域 -->
      <div class="article-content" v-html="dataInfo" v-if="articleType == 'header'"></div>
      <div class="article-content" v-html="dataInfo ? dataInfo.content : ''" v-else></div>

      <!-- 标签 -->
      <!-- <div class="article-tags" v-if="activeInfo.tags && activeInfo.tags.length">
        <div class="tag" v-for="(tag, i) in activeInfo.tags" :key="i">
          <span>{{ tag }}</span>
        </div>
      </div> -->
    </template>
  </section>
</template>

<script setup lang="ts">
import { storeToRefs } from 'pinia'
import useSystemStore from '@/stores/useSystemStore'
import useData from './hooks/useData'
import type { IDataInfo } from './hooks/useData'

// system store
const systemStore = useSystemStore()
const { locale } = storeToRefs(systemStore)

const { dataInfo, articleType, loading } = useData()

// 格式化日期
const formatDate = (dateString: string, lang: string) => {
  const options = { year: 'numeric', month: 'long', day: 'numeric' }
  return new Date(dateString).toLocaleDateString(lang, options as Object)
}
</script>

<style scoped lang="less">
.article-container {
  max-width: 800px;
  max-width: 75rem;
  max-width: 50rem;
  margin: 0 auto;
  padding: 60px 15px;
  padding: 3.75rem 0.9375rem;
  font-family: 'Helvetica Neue', Arial, sans-serif;
  color: #333;
}

.loading-box {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.article-title {
  font-size: var(--title-size);
  margin-bottom: 20px;
  margin-bottom: 1.25rem;
  color: #222;
  text-align: center;
}

.article-meta {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  gap: 20px;
  gap: 1.25rem;
  margin-bottom: 30px;
  margin-bottom: 1.875rem;
  padding-bottom: 20px;
  padding-bottom: 1.25rem;
  border-bottom: 1px solid #eee;
  color: #666;
  font-size: 0.9rem;
}

.author-info {
  display: flex;
  align-items: center;
  gap: 10px;
  gap: 0.625rem;
}

.author-avatar {
  width: 40px;
  width: 2.5rem;
  height: 40px;
  height: 2.5rem;
  border-radius: 50%;
  object-fit: cover;
}

.author-name {
  font-weight: bold;
}

.article-content {
  margin-bottom: 40px;
  margin-bottom: 2.5rem;
  font-size: 1rem;
  line-height: 1.6;
}

.article-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  gap: 0.625rem;
  margin-top: 30px;
  margin-top: 1.875rem;
  padding-top: 20px;
  padding-top: 1.25rem;
  border-top: 1px solid #eee;
}

.article-tags .tag {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 4px 12px;
  padding: 0.25rem 0.75rem;
  background-color: #f0f0f0;
  border-radius: 20px;
  border-radius: 1.25rem;
  font-size: 0.8rem;
  color: #555;
}
</style>

<style>
/* 全局样式用于富文本内容 */
.article-content h2 {
  font-size: 1.8rem;
  margin: 30px 0 15px;
  margin: 1.875rem 0 0.9375rem;
  color: #222;
  border-bottom: 1px solid #eee;
  padding-bottom: 10px;
  padding-bottom: 0.625rem;
}

.article-content h3 {
  font-size: 1.4rem;
  margin: 25px 0 12px;
  margin: 1.5625rem 0 0.75rem;
  color: #333;
}

.article-content p {
  margin: 15px 0;
  margin: 0.9375rem 0;
  font-size: 1rem;
}

.article-content ul,
.article-content ol {
  margin: 15px 0;
  margin: 0.9375rem 0;
  padding-left: 30px;
  padding-left: 1.875rem;
}

.article-content li {
  margin: 8px 0;
  margin: 0.5rem 0;
}

.article-content a {
  color: var(--color-active);
  text-decoration: none;
}

.article-content a:hover {
  text-decoration: underline;
}

.article-content img {
  width: 100%;
  max-width: 100%;
  height: auto;
  margin: 20px 0;
  margin: 1.25rem 0;
  border-radius: 4px;
  border-radius: 0.25rem;
}

.article-content pre {
  background-color: #f6f8fa;
  padding: 16px;
  padding: 1rem;
  border-radius: 6px;
  border-radius: 0.375rem;
  overflow: auto;
  margin: 20px 0;
  margin: 1.25rem 0;
}
</style>