<template>
  <div class="announcements-container">
 
    

    <!-- 主要内容区域 -->
    <div class="main-content" v-loading="loading">
      <!-- 页面标题 -->
      <div class="page-header">
        <div class="header-left">
          <h1 class="page-title">{{ $t('home.announcements') || '公告中心' }}</h1>
        </div>
        <div class="header-actions">
          <el-button-group>
            <el-button 
              :type="viewMode === 'list' ? 'primary' : 'default'"
              @click="switchMode('list')"
              icon="el-icon-menu"
            >
              查看公告
            </el-button>
            <el-button 
              :type="viewMode === 'editor' ? 'primary' : 'default'"
              @click="switchMode('editor')"
              icon="el-icon-edit"
            >
              Markdown编辑器
            </el-button>
          </el-button-group>
        </div>
      </div>

      <!-- 最新公告标题 -->
      <div class="latest-section">
        <h2 class="section-title">{{ $t('home.latestTitle') || '最新公告' }}</h2>
      </div>

      <!-- 公告列表 -->
      <div class="announcement-list" v-if="!loading && announcements.length > 0">
        <div 
          v-for="announcement in announcements" 
          :key="announcement.id"
          class="announcement-item"
          @click="handleAnnouncementClick(announcement)"
        >
          <div class="announcement-content">
            <h3 class="announcement-title">{{ announcement.title }}</h3>
            <div class="announcement-meta">
              <span class="announcement-date">{{ formatDate(announcement.createTime) }}</span>
              <span class="announcement-type" v-if="announcement.type">{{ announcement.type }}</span>
            </div>
            <p class="announcement-summary" v-if="announcement.summary">
              {{ announcement.summary }}
            </p>
          </div>
          <div class="announcement-arrow">
            <i class="el-icon-arrow-right"></i>
          </div>
        </div>
      </div>

      <!-- 空状态 -->
      <div v-else-if="!loading && announcements.length === 0" class="empty-state">
        <div class="empty-content">
          <i class="el-icon-document"></i>
          <p>{{ $t('home.noData') || '暂无公告' }}</p>
        </div>
      </div>

      <!-- 分页信息和查看更多 -->
      <div class="pagination-section" v-if="announcements.length > 0">
        <div class="view-all-button">
          <el-button 
            type="text" 
            class="view-all-link"
            @click="handleViewAll"
          >
            {{ $t('home.viewAll') || '查看所有' }} {{ totalCount }} {{ $t('home.articles') || '篇文章' }}
          </el-button>
        </div>
        
        <!-- 分页组件 -->
        <el-pagination
          v-if="totalCount > pageSize"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-size="pageSize"
          :total="totalCount"
          layout="prev, pager, next"
          class="pagination"
        />
      </div>
    </div>
  </div>
</template>

<script>
/**
 * 公告中心页面组件
 * 提供公告列表展示、搜索、分页等功能
 */
export default {
  name: 'Announcements',
  data() {
    return {
      // 加载状态
      loading: false,
      
      // 搜索关键词
      searchKeyword: '',
      
      // 分页参数
      currentPage: 1,
      pageSize: 10,
      totalCount: 275,
      
      // 公告数据
      announcements: [
        {
          id: 1,
          title: 'ZEN挖矿服务即将结束',
          summary: 'ZEN挖矿服务将于近期结束，请及时调整您的挖矿设置。',
          type: '重要通知',
          createTime: '2025-01-20T10:00:00.000Z',
          isTop: true
        },
        {
          id: 2,
          title: 'LKY即将减半',
          summary: 'LKY币种将在近期进行减半操作，请关注相关通知。',
          type: '系统公告',
          createTime: '2025-01-19T15:30:00.000Z',
          isTop: false
        },
        {
          id: 3,
          title: 'PEP即将减产',
          summary: 'PEP币种挖矿难度调整，产量将有所减少。',
          type: '市场动态',
          createTime: '2025-01-18T09:15:00.000Z',
          isTop: false
        },
        {
          id: 4,
          title: 'ETC+ZIL挖矿服务已结束',
          summary: 'ETC+ZIL双挖服务已正式结束，感谢您的支持。',
          type: '服务通知',
          createTime: '2025-01-17T14:45:00.000Z',
          isTop: false
        },
        {
          id: 5,
          title: '有关2025年06月19日SCT池异常的说明',
          summary: 'SCT矿池在指定时间出现异常情况，现已修复并提供补偿方案。',
          type: '故障说明',
          createTime: '2025-01-16T11:20:00.000Z',
          isTop: false
        },
        {
          id: 6,
          title: 'FB单挖矿池下线公告',
          summary: 'FB单挖矿池将于本月底正式下线，请及时转移算力。',
          type: '下线通知',
          createTime: '2025-01-15T16:10:00.000Z',
          isTop: false
        }
      ],
      
      // 搜索防抖定时器
      searchTimer: null
    }
  },
  
  mounted() {
    this.loadAnnouncements();
  },
  
  methods: {
    /**
     * 加载公告数据
     */
    async loadAnnouncements() {
      this.loading = true;
      try {
        // 这里应该调用API获取公告数据
        // const response = await this.$api.getAnnouncements({
        //   page: this.currentPage,
        //   pageSize: this.pageSize,
        //   keyword: this.searchKeyword
        // });
        // this.announcements = response.data;
        // this.totalCount = response.total;
        
        // 模拟API调用延迟
        await new Promise(resolve => setTimeout(resolve, 500));
        
      } catch (error) {
        console.error('加载公告失败:', error);
        this.$message.error(this.$t('announcements.loadError') || '加载公告失败');
      } finally {
        this.loading = false;
      }
    },
    
    /**
     * 处理搜索输入
     */
    handleSearchInput() {
      if (this.searchTimer) {
        clearTimeout(this.searchTimer);
      }
      
      // 防抖处理，500ms后执行搜索
      this.searchTimer = setTimeout(() => {
        this.handleSearch();
      }, 500);
    },
    
    /**
     * 执行搜索
     */
    handleSearch() {
      this.currentPage = 1;
      this.loadAnnouncements();
    },
    
    /**
     * 处理页码变化
     */
    handleCurrentChange(page) {
      this.currentPage = page;
      this.loadAnnouncements();
    },
    
    /**
     * 处理公告点击
     */
    handleAnnouncementClick(announcement) {
      // 跳转到公告详情页
      this.$router.push({
        name: 'AnnouncementDetail',
        params: { id: announcement.id }
      });
    },
    
    /**
     * 查看所有公告
     */
    handleViewAll() {
      // 可以跳转到完整的公告列表页面或展开显示更多
      this.pageSize = 20;
      this.loadAnnouncements();
    },
    
    /**
     * 返回首页
     */
    goHome() {
      this.$router.push('/');
    },
    
    /**
     * 格式化日期
     */
    formatDate(dateString) {
      if (!dateString) return '';
      
      const date = new Date(dateString);
      const now = new Date();
      const diffTime = now - date;
      const diffDays = Math.floor(diffTime / (1000 * 60 * 60 * 24));
      
      if (diffDays === 0) {
        return '今天';
      } else if (diffDays === 1) {
        return '昨天';
      } else if (diffDays < 7) {
        return `${diffDays}天前`;
      } else {
        return date.toLocaleDateString('zh-CN');
      }
    }
  },
  
  beforeDestroy() {
    // 清理定时器
    if (this.searchTimer) {
      clearTimeout(this.searchTimer);
    }
  }
}
</script>

<style lang="scss" scoped>
/**
 * 公告中心页面样式
 */
.announcements-container {
  min-height: 100vh;
  background-color: #f8f9fa;
  padding: 20px;
}

/* 面包屑导航区域 */
.breadcrumb-section {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
  background: white;
  padding: 16px 24px;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
}

.breadcrumb {
  font-size: 14px;
  
  .breadcrumb-item {
    color: #666;
    cursor: pointer;
    transition: color 0.3s ease;
    
    &:hover {
      color: #5721E4;
    }
  }
}

.search-section {
  .search-input {
    width: 300px;
  }
}

/* 主要内容区域 */
.main-content {
  max-width: 1200px;
  margin: 0 auto;
  background: white;
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  overflow: hidden;
  margin-top: 60px;
}

/* 页面标题 */
.page-header {
  padding: 40px 40px 20px;
  border-bottom: 1px solid #f0f0f0;
  text-align: center;
}

.page-title {
  font-size: 32px;
  font-weight: 600;
  color: #2c3e50;
  margin: 0;
}

/* 最新公告标题 */
.latest-section {
  padding: 30px 40px 20px;
}

.section-title {
  font-size: 20px;
  font-weight: 600;
  color: #34495e;
  margin: 0;
  position: relative;
  
  &::before {
    content: '';
    position: absolute;
    left: -20px;
    top: 50%;
    transform: translateY(-50%);
    width: 4px;
    height: 20px;
    background: #5721E4;
    border-radius: 2px;
  }
}

/* 公告列表 */
.announcement-list {
  padding: 0 40px 20px;
}

.announcement-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 0;
  border-bottom: 1px solid #f5f5f5;
  cursor: pointer;
  transition: all 0.3s ease;
  
  &:hover {
    background-color: #f8f9ff;
    transform: translateX(5px);
  }
  
  &:last-child {
    border-bottom: none;
  }
}

.announcement-content {
  flex: 1;
}

.announcement-title {
  font-size: 16px;
  font-weight: 500;
  color: #2c3e50;
  margin: 0 0 8px;
  line-height: 1.4;
  transition: color 0.3s ease;
  
  .announcement-item:hover & {
    color: #5721E4;
  }
}

.announcement-meta {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-bottom: 8px;
}

.announcement-date {
  font-size: 12px;
  color: #999;
}

.announcement-type {
  font-size: 12px;
  color: #5721E4;
  background: #f0edff;
  padding: 2px 8px;
  border-radius: 12px;
}

.announcement-summary {
  font-size: 14px;
  color: #666;
  line-height: 1.5;
  margin: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.announcement-arrow {
  color: #ccc;
  transition: color 0.3s ease;
  
  .announcement-item:hover & {
    color: #5721E4;
  }
}

/* 空状态 */
.empty-state {
  text-align: center;
  padding: 80px 40px;
}

.empty-content {
  i {
    font-size: 64px;
    color: #ddd;
    margin-bottom: 20px;
  }
  
  p {
    font-size: 16px;
    color: #999;
    margin: 0;
  }
}

/* 分页区域 */
.pagination-section {
  padding: 30px 40px 40px;
  text-align: center;
  border-top: 1px solid #f0f0f0;
}

.view-all-button {
  margin-bottom: 20px;
}

.view-all-link {
  font-size: 14px;
  color: #5721E4;
  text-decoration: none;
  
  &:hover {
    text-decoration: underline;
  }
}

.pagination {
  justify-content: center;
  
  :deep(.el-pager li) {
    background-color: transparent;
    color: #666;
    
    &.active {
      background-color: #5721E4;
      color: white;
    }
    
    &:hover:not(.active) {
      background-color: #f0edff;
      color: #5721E4;
    }
  }
  
  :deep(.btn-prev), 
  :deep(.btn-next) {
    background-color: transparent;
    color: #666;
    
    &:hover {
      background-color: #f0edff;
      color: #5721E4;
    }
  }
}

/* 移动端适配 */
@media screen and (max-width: 768px) {
  .announcements-container {
    padding: 10px;
  }
  
  .breadcrumb-section {
    flex-direction: column;
    gap: 16px;
    align-items: stretch;
    padding: 16px;
  }
  
  .search-section .search-input {
    width: 100%;
  }
  
  .main-content {
    border-radius: 8px;
  }
  
  .page-header,
  .latest-section,
  .announcement-list,
  .pagination-section {
    padding-left: 20px;
    padding-right: 20px;
  }
  
  .page-title {
    font-size: 24px;
  }
  
  .section-title {
    font-size: 18px;
    
    &::before {
      left: -16px;
    }
  }
  
  .announcement-item {
    padding: 16px 0;
  }
  
  .announcement-title {
    font-size: 15px;
  }
  
  .announcement-meta {
    flex-direction: column;
    align-items: flex-start;
    gap: 4px;
  }
}

/* 深色模式适配 */
@media (prefers-color-scheme: dark) {
  .announcements-container {
    background-color: #1a1a1a;
  }
  
  .breadcrumb-section,
  .main-content {
    background: #2d2d2d;
    color: #fff;
  }
  
  .page-title,
  .section-title,
  .announcement-title {
    color: #fff;
  }
  
  .announcement-item:hover {
    background-color: #3a3a3a;
  }
  
  .announcement-summary {
    color: #ccc;
  }
  
  .announcement-date {
    color: #999;
  }
}
</style>