<template>
  <view class="sub-page">
    <TopBar title="通知消息" show-back />

    <view class="px-4 pt-3">
      <view v-if="loading" class="py-10 text-center">
        <text class="empty-text">加载中...</text>
      </view>

      <view v-else-if="noticeList.length === 0">
        <EmptyState message="暂无通知" />
      </view>

      <view v-else class="glass-card">
        <view
          v-for="notice in noticeList"
          :key="notice.noticeId"
          class="list-row"
          hover-class="row-hover"
          :hover-start-time="0"
          :hover-stay-time="120"
          @click="goToDetail(notice.noticeId)"
        >
          <view class="icon-chip mr-3">
            <text class="uni-icons uniui-notification-filled chip-glyph"></text>
            <view v-if="!notice.read" class="unread-dot"></view>
          </view>
          <view class="flex-1 min-w-0 mr-2">
            <text class="row-title truncate">{{ notice.title }}</text>
            <text class="row-sub truncate">{{ notice.content }}</text>
          </view>
          <text class="row-date flex-shrink-0 mr-1">{{ formatDate(notice.publishTime) }}</text>
          <text class="uni-icons uniui-arrowright row-arrow"></text>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import TopBar from '../../components/common/TopBar.vue'
import EmptyState from '../../components/common/EmptyState.vue'
import { getNoticeList } from '../../api/notice'
import { formatDate } from '../../utils'

const loading = ref(false)
const noticeList = ref<any[]>([])

function goToDetail(noticeId: number) {
  uni.navigateTo({ url: `/subPackages/pages-common/noticeDetail?id=${noticeId}` })
}

onMounted(async () => {
  loading.value = true
  try {
    const res = await getNoticeList()
    noticeList.value = res.rows || []
  } catch (error) {
    console.error('获取通知列表失败:', error)
  } finally {
    loading.value = false
  }
})
</script>

<style scoped>
.sub-page {
  max-width: 430px;
  margin: 0 auto;
  min-height: 100vh;
  background-color: #f6fbf9;
  padding-bottom: 24px;
}

.glass-card {
  background-color: rgba(255, 255, 255, 0.85);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border: 1px solid rgba(164, 216, 152, 0.35);
  border-radius: 24px;
  box-shadow: 0 18px 50px -12px rgba(54, 143, 111, 0.28);
  overflow: hidden;
  margin-bottom: 12px;
}

.list-row {
  display: flex;
  align-items: center;
  padding: 13px 16px;
  border-top: 1px solid rgba(164, 216, 152, 0.18);
}

.list-row:first-child {
  border-top: none;
}

.row-title {
  display: block;
  font-size: 14px;
  font-weight: 500;
  color: #1f2937;
}

.row-sub {
  display: block;
  margin-top: 3px;
  font-size: 12px;
  color: #9ca3af;
}

.row-date {
  font-size: 12px;
  color: #9ca3af;
}

.row-hover {
  background-color: rgba(164, 216, 152, 0.1);
}

.row-arrow {
  font-size: 16px;
  color: #c4d0cb;
  flex-shrink: 0;
}

.icon-chip {
  position: relative;
  width: 42px;
  height: 42px;
  border-radius: 12px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: rgba(164, 216, 152, 0.2);
}

.chip-glyph {
  font-size: 20px;
  color: #368f6f;
}

.unread-dot {
  position: absolute;
  top: -3px;
  right: -3px;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background-color: #ef4444;
  border: 2px solid #ffffff;
}

.empty-text {
  font-size: 13px;
  color: #9ca3af;
}
</style>
