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

    <!-- 筛选 -->
    <view class="px-4 pt-3">
      <view class="filter-bar">
        <view
          v-for="type in filterTypes"
          :key="type.value"
          class="filter-chip"
          :class="{ 'filter-chip-active': currentFilter === type.value }"
          @click="currentFilter = type.value"
        >
          {{ type.label }}
        </view>
      </view>
    </view>

    <!-- 通知列表 -->
    <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="filteredNotices.length === 0">
        <EmptyState message="暂无通知" />
      </view>

      <view v-else class="glass-card">
        <view
          v-for="notice in filteredNotices"
          :key="notice.noticeId"
          class="notice-row"
          hover-class="row-hover"
          :hover-start-time="0"
          :hover-stay-time="120"
          @click="goToDetail(notice.noticeId)"
        >
          <view class="flex items-center justify-between mb-2">
            <view class="flex items-center flex-1 min-w-0 mr-2">
              <view v-if="notice.priority === 3" class="priority-dot mr-2"></view>
              <text class="row-title truncate">{{ notice.title }}</text>
            </view>
            <text class="type-tag flex-shrink-0" :class="getTypeClass(notice.type)">
              {{ getTypeText(notice.type) }}
            </text>
          </view>
          <text class="row-sub notice-content">{{ notice.content }}</text>
          <view class="flex items-center justify-between mt-2">
            <text class="row-date">{{ notice.publisherName }} · {{ formatDate(notice.publishTime) }}</text>
            <text class="row-date">阅读 {{ notice.readCount }}</text>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref, computed, 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 notices = ref<any[]>([])
const currentFilter = ref('all')

const filterTypes = [
  { value: 'all', label: '全部' },
  { value: 'system', label: '系统' },
  { value: 'emergency', label: '应急' },
  { value: 'task', label: '任务' },
]

const filteredNotices = computed(() => {
  if (currentFilter.value === 'all') return notices.value
  return notices.value.filter(n => n.type === currentFilter.value)
})

function getTypeText(type: string) {
  const map: Record<string, string> = {
    system: '系统',
    emergency: '应急',
    task: '任务',
  }
  return map[type] || type
}

function getTypeClass(type: string) {
  const map: Record<string, string> = {
    system: 'type-tag-green',
    emergency: 'type-tag-red',
    task: 'type-tag-gold',
  }
  return map[type] || 'type-tag-gray'
}

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

onMounted(async () => {
  loading.value = true
  try {
    const res = await getNoticeList()
    notices.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;
}

/* 筛选条：默认白底灰边，激活态绿色 */
.filter-bar {
  display: flex;
  gap: 8px;
  overflow-x: auto;
}

.filter-chip {
  padding: 6px 16px;
  border-radius: 999px;
  font-size: 13px;
  color: #6b7280;
  background-color: #ffffff;
  border: 1px solid #e5e7eb;
  flex-shrink: 0;
}

.filter-chip-active {
  color: #ffffff;
  font-weight: 600;
  background: linear-gradient(135deg, #368f6f 0%, #4ba98a 100%);
  border-color: transparent;
}

.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;
}

.notice-row {
  padding: 14px 16px;
  border-top: 1px solid rgba(164, 216, 152, 0.18);
}

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

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

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

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

.notice-content {
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

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

.priority-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background-color: #ef4444;
  flex-shrink: 0;
}

.type-tag {
  padding: 2px 10px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
}

.type-tag-green {
  color: #368f6f;
  background-color: rgba(164, 216, 152, 0.24);
}

.type-tag-red {
  color: #ef4444;
  background-color: rgba(239, 68, 68, 0.1);
}

.type-tag-gold {
  color: #b7791f;
  background-color: rgba(251, 211, 141, 0.25);
}

.type-tag-gray {
  color: #6b7280;
  background-color: #f3f4f6;
}

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