<!-- 公告栏 -->
 <!-- Announcement.vue -->
<template>
    <div class="announcement-container">
      <div v-for="a in announcements" :key="a.announcement_id" class="announcement-item">
        <h3>{{ a.title }}</h3>
        <p class="announcement-time">{{ a.updated_at }}</p>
        <div v-html="a.content" class="announcement-content"></div>
      </div>
    </div>
  </template>
  
  <script lang="ts" setup>
  import { ref, reactive,onMounted } from 'vue';
  import axios from 'axios';

  interface Announcement {
  announcement_id: number;
  title: string;
  updated_at: string;
  content: string;
  }

  let announcements = reactive<Announcement[]>([]);

  //调用axios获取公告
// 查找全部的方法api
function getAll() {
    axios.get('http://127.0.0.1:8080/announcements')
        .then(res => {
            // 使用扩展运算符来保持响应性
            console.log("开始存放",res.data)
            announcements.splice(0, announcements.length)
            announcements.push(...res.data.data)
            console.log("获取公告数据成功", res.data.data);
        })
        .catch(error => console.error('There was an error fetching the data!', error))
}

  onMounted(() => {
    console.log("组件已挂载，开始请求公告数据");
    getAll();
  });
  </script>
  
<style scoped>
  .announcement-container {
    border-top: 2px solid #ffcc00; /* 黄色边框 */
    padding: 20px;
    background-color: #fff9c4; /* 浅黄色背景 */
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); /* 阴影效果 */
    border-radius: 8px; /* 圆角 */
    margin: 20px;
  }
  
  .announcement-item {
    margin-bottom: 20px;
    padding: 10px;
    background-color: #fff; /* 白色背景 */
    border: 1px solid #ffcc00; /* 黄色边框 */
    border-radius: 4px; /* 圆角 */
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05); /* 阴影效果 */
  }
  
  .announcement-item h3 {
    color: #e65100; /* 橙色标题 */
    font-size: 1.2em;
    margin-bottom: 5px;
  }
  
  .announcement-time {
    color: #888;
    font-size: 0.9em;
    margin-bottom: 10px;
  }
  
  .announcement-content {
    margin-top: 10px;
    color: #333; /* 深色内容 */
  }
</style>