
<template>
  <view class="sub-page">
    <TopBar title="消息通知" show-back>
      <template #right>
        <text class="right-link" @click="markAllRead">全部已读</text>
      </template>
    </TopBar>

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

      <view v-if="filteredMessages.length === 0">
        <EmptyState message="暂无消息" />
      </view>

      <view v-else class="glass-card">
        <view
          v-for="msg in filteredMessages"
          :key="msg.id"
          class="list-row"
          hover-class="row-hover"
          :hover-start-time="0"
          :hover-stay-time="120"
          @click="goToDetail(msg)"
        >
          <view class="icon-chip mr-3">
            <text class="uni-icons uniui-chatbubble-filled chip-glyph"></text>
            <view v-if="!msg.read" class="unread-dot"></view>
          </view>
          <view class="flex-1 min-w-0 mr-2">
            <text class="row-title truncate" :class="{ 'row-title-read': msg.read }">{{ msg.title }}</text>
            <text class="row-sub row-clamp">{{ msg.content }}</text>
          </view>
          <text class="row-date flex-shrink-0">{{ formatRelativeTime(msg.createTime) }}</text>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import TopBar from '@/components/common/TopBar.vue'
import EmptyState from '@/components/common/EmptyState.vue'
import { useNotificationStore } from '@/stores/notification'
import { formatRelativeTime, navigateTo } from '@/utils'

const notificationStore = useNotificationStore()
const currentFilter = ref('all')

const filterTabs = [
  { label: '全部', value: 'all' },
  { label: '任务消息', value: 'task' },
  { label: '系统消息', value: 'system' },
]

const filteredMessages = computed(() => {
  let list = notificationStore.notifications
  if (currentFilter.value !== 'all') {
    list = list.filter(n => n.type === currentFilter.value)
  }
  return list.map(n => ({
    ...n,
    content: n.content.length > 60 ? n.content.slice(0, 60) + '...' : n.content
  }))
})

function markAllRead() {
  notificationStore.markAllAsRead()
}

function goToDetail(msg: typeof filteredMessages.value[0]) {
  notificationStore.markAsRead(msg.id)
  uni.setStorageSync('current_message', msg)
  navigateTo('/subPackages/pages-common/messageDetail')
}
</script>

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

.right-link {
  font-size: 13px;
  color: #368f6f;
}

/* 筛选条：默认白底灰边，激活态绿色 */
.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);
}

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

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

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

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

.row-title-read {
  font-weight: 500;
  color: #6b7280;
}

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

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

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