<template>
  <div class="announcements-card">
    <div class="card-header">
      <div class="title-container">
        <h2 class="card-title">市场公告</h2>
        <div class="title-tibetan">ཚོང་འདུས་བརྡ་ཐོ།</div>
      </div>
    </div>
    
    <div class="announcements-content">
      <div v-if="loading" class="loading-message">
        <div class="loading-spinner"></div>
        <div>正在加载公告内容...</div>
      </div>
      
      <div v-else-if="error" class="error-message">
        <div>{{ error }}</div>
      </div>
      
      <div v-else class="announcement-list">
        <div 
          v-for="announcement in announcements" 
          :key="announcement.id" 
          class="announcement-item"
          :class="{ important: announcement.important }"
        >
          <div class="announcement-content-chinese">
            {{ announcement.content }}
          </div>
          <div class="announcement-content-tibetan">
            {{ announcement.content_tibetan }}
          </div>
          <!-- <div v-if="announcement.important" class="important-badge">
            重要公告
          </div> -->
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'

const announcements = ref([])
const loading = ref(true)
const error = ref('')

// 从JSON文件加载公告内容
const loadAnnouncements = async () => {
  try {
    loading.value = true
    error.value = ''
    
    const response = await fetch('/tv86-announcements.json')
    if (!response.ok) {
      throw new Error('无法加载公告内容')
    }
    
    const data = await response.json()
    announcements.value = data.announcements || []
    
    console.log('公告内容加载成功:', announcements.value)
  } catch (err) {
    console.error('加载公告失败:', err)
    error.value = '加载公告内容失败，请稍后重试'
    
    // 使用默认公告内容作为后备
    announcements.value = [
      {
        id: 1,
        content: '各位牧民、客商请注意，交易时请保管好财物，遵守市场秩序，公平议价，禁止强买强卖；运输牦牛需确保车辆牢固，避免牲畜受惊。遇纠纷可联系市场管理方协调，共同维护安全、诚信的交易环境。',
        content_tibetan: 'འབྲོག་པ་དང་ཚོང་པ་ཚོས་གཟིགས་ནས། ཚོང་འདུས་སྐབས་རང་གི་དངུལ་རྫས་ལེགས་པར་སྲུང་སྐྱོབ་བྱེད་པ་དང་། ཚོང་འདུས་ཀྱི་རིམ་པ་བསྲུང་བ། འགྲིག་པའི་གོང་གི་གླེང་སྟེགས་བྱེད་པ། ཁས་ལེན་འདེད་སྐུལ་བྱེད་རྒྱུ་བཀག་འགོག་བྱེད་དགོས། གཡག་འདྲེན་སྐབས་མོ་ཊར་བརྟན་པོ་ཡོད་དགོས་པ་དང་། སེམས་ཅན་སྐྲག་པ་སྲིད་རྒྱུ་ཚར་གཅོད་བྱེད་དགོས། རྩོད་རྙོག་བྱུང་ཚེ་ཚོང་འདུས་འཛིན་སྐྱོང་བ་དང་རྒྱུས་ལོན་གླེང་སླུང་བྱེད་ཆོག ཉམས་ལེན་གྱིས་བདེ་འཇགས་དང་ཡིད་ཆེས་ཀྱི་ཚོང་འདུས་ཁོར་ཡུག་ཡར་རྒྱས་གཏོང་བ།',
        important: true,
        created_at: '2025-01-13'
      }
    ]
  } finally {
    loading.value = false
  }
}

onMounted(() => {
  // 初始加载
  loadAnnouncements()
  
  // 每5分钟重新加载一次，检查是否有更新
  setInterval(() => {
    loadAnnouncements()
  }, 300000) // 5分钟
})
</script>

<style scoped>
.announcements-card {
  height: 100%;
  background: rgba(15, 25, 45, 0.4);
  backdrop-filter: blur(20px) saturate(180%);
  border-radius: 32px;
  padding: 48px;
  display: flex;
  flex-direction: column;
  box-shadow: 
    0 16px 64px rgba(0, 0, 0, 0.3),
    0 8px 32px rgba(64, 158, 255, 0.1),
    inset 0 2px 0 rgba(255, 255, 255, 0.1);
}

.card-header {
  margin-bottom: 40px;
}

.title-container {
  display: flex;
  align-items: center;
  gap: 32px;
}

.card-title {
  font-size: 48px;
  font-weight: 700;
  background: linear-gradient(135deg, #409EFF 0%, #67C23A 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  margin: 0;
  text-align: left;
}

.title-tibetan {
  font-size: 36px;
  color: #67C23A;
  font-weight: 600;
  opacity: 0.8;
}

.announcements-content {
  flex: 1;
  overflow: hidden;
}

.announcement-list {
  height: 100%;
  overflow-y: auto;
  padding-right: 8px;
}

.announcement-item {
  background: rgba(255, 255, 255, 0.05);
  border-radius: 24px;
  padding: 48px;
  margin-bottom: 40px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  position: relative;
  transition: all 0.3s ease;
}

.announcement-item:hover {
  background: rgba(255, 255, 255, 0.08);
  transform: translateY(-2px);
  box-shadow: 0 4px 16px rgba(64, 158, 255, 0.1);
}

.announcement-item.important {
  border-color: rgba(245, 108, 108, 0.4);
  background: rgba(245, 108, 108, 0.08);
}

.announcement-content-chinese {
  font-size: 42px;
  line-height: 1.8;
  color: #ffffff;
  text-align: justify;
  margin-bottom: 32px;
  font-weight: 500;
}

.announcement-content-tibetan {
  font-size: 42px;
  line-height: 1.8;
  color: #67C23A;
  text-align: justify;
  opacity: 0.9;
  font-weight: 500;
}

.important-badge {
  position: absolute;
  top: 24px;
  right: 24px;
  background: linear-gradient(135deg, #F56C6C, #E6A23C);
  color: #ffffff;
  font-size: 24px;
  font-weight: 600;
  padding: 8px 16px;
  border-radius: 24px;
  box-shadow: 0 4px 16px rgba(245, 108, 108, 0.3);
}

/* 加载和错误状态样式 */
.loading-message, .error-message {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: #a0a8b8;
  font-size: 32px;
}

.loading-spinner {
  width: 48px;
  height: 48px;
  border: 4px solid rgba(64, 158, 255, 0.3);
  border-top: 4px solid #409EFF;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 24px;
}

.error-message {
  color: #F56C6C;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

/* 滚动条样式 */
.announcement-list::-webkit-scrollbar {
  width: 12px;
}

.announcement-list::-webkit-scrollbar-track {
  background: rgba(255, 255, 255, 0.1);
  border-radius: 6px;
}

.announcement-list::-webkit-scrollbar-thumb {
  background: rgba(64, 158, 255, 0.5);
  border-radius: 6px;
}

.announcement-list::-webkit-scrollbar-thumb:hover {
  background: rgba(64, 158, 255, 0.7);
}

/* 动画效果 */
@keyframes slideIn {
  from {
    opacity: 0;
    transform: translateX(20px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

.announcement-item {
  animation: slideIn 0.5s ease-out;
}
</style> 