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

    <view class="px-4 pt-3">
      <!-- 筛选 -->
      <view class="filter-bar mb-3">
        <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 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">
        <uni-swipe-action
          v-for="notice in filteredNotices"
          :key="notice.noticeId"
          :actions="[{text: '标记已读', color: '#368f6f', key: 'read'}]"
          @click="onSwipeAction(notice.noticeId, $event)"
        >
          <view
            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" :class="{ 'icon-chip-red': notice.type === 'emergency' }">
              <text class="uni-icons chip-glyph" :class="getTypeIcon(notice.type)"></text>
            </view>
            <view class="flex-1 min-w-0 mr-2">
              <text class="row-title truncate">{{ notice.title }}</text>
              <text class="row-sub truncate">{{ notice.publisherName }} · {{ formatDate(notice.publishTime) }}</text>
            </view>
            <text class="type-tag flex-shrink-0 mr-1" :class="getTypeClass(notice.type)">
              {{ getTypeText(notice.type) }}
            </text>
            <text class="uni-icons uniui-arrowright row-arrow"></text>
          </view>
        </uni-swipe-action>
      </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 UniSwipeAction from '../../components/uni-swipe-action/uni-swipe-action.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 getTypeIcon(type: string) {
  const map: Record<string, string> = {
    system: 'uniui-sound',
    emergency: 'uniui-notification-filled',
    task: 'uniui-flag',
  }
  return map[type] || 'uniui-sound'
}

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 onSwipeAction(noticeId: number, action: { key?: string; text?: string }) {
  if (action.key === 'read') {
    uni.showToast({ title: '已标记已读', icon: 'success' })
  }
}

function goToDetail(noticeId: number) {
  uni.setStorageSync('currentNoticeId', noticeId)
  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;
}

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

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

.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-arrow {
  font-size: 16px;
  color: #c4d0cb;
  flex-shrink: 0;
}

.icon-chip {
  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);
}

.icon-chip-red {
  background-color: rgba(239, 68, 68, 0.1);
}

.icon-chip-red .chip-glyph {
  color: #ef4444;
}

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

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