<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="filteredMessages.length === 0">
        <EmptyState message="暂无消息" />
      </view>

      <view v-else class="glass-card">
        <uni-swipe-action
          v-for="msg in filteredMessages"
          :key="msg.id"
          :actions="[{text: '删除', color: '#ff3b30', key: 'delete'}]"
          @click="onSwipeAction(msg.id, $event)"
        >
          <view
            class="list-row"
            hover-class="row-hover"
            :hover-start-time="0"
            :hover-stay-time="120"
            @click="goToDetail(msg.id)"
          >
            <view class="icon-chip mr-3" :class="{ 'icon-chip-red': msg.type === 'urgent' }">
              <text class="uni-icons chip-glyph" :class="getTypeIcon(msg.type)"></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">{{ msg.title }}</text>
              <text class="row-sub truncate">{{ msg.content.length > 20 ? msg.content.substring(0, 20) + '...' : msg.content }}</text>
            </view>
            <text class="row-date flex-shrink-0 mr-1">{{ formatTime(msg.createTime) }}</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 { formatDate } from '../../utils'

const loading = ref(false)
const messages = ref<any[]>([])
const currentFilter = ref('all')

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

const filteredMessages = computed(() => {
  if (currentFilter.value === 'all') return messages.value
  return messages.value.filter(m => m.type === currentFilter.value)
})

function getTypeIcon(type: string) {
  const map: Record<string, string> = {
    task: 'uniui-flag-filled',
    system: 'uniui-gear-filled',
    urgent: 'uniui-notification-filled',
  }
  return map[type] || 'uniui-chatbubble'
}

function formatTime(date: string) {
  return formatDate(date, 'MM-DD HH:mm')
}

function onSwipeAction(msgId: string, action: { key?: string }) {
  if (action.key === 'delete') {
    uni.showModal({
      title: '确认删除',
      content: '确定要删除这条消息吗？',
      success: (res) => {
        if (res.confirm) {
          messages.value = messages.value.filter(m => m.id !== msgId)
          uni.showToast({ title: '已删除', icon: 'success' })
        }
      }
    })
  }
}

function goToDetail(msgId: string) {
  uni.navigateTo({ url: `/subPackages/pages-sales/messageDetail?id=${msgId}` })
}

onMounted(() => {
  messages.value = [
    { id: '1', title: '新任务分配', content: '您有一条新的清掏任务待处理', type: 'task', read: false, createTime: '2026-06-22 14:30' },
    { id: '2', title: '任务即将到期', content: '积丹白鹭小区任务将于明日到期', type: 'urgent', read: false, createTime: '2026-06-22 10:00' },
    { id: '3', title: '系统维护通知', content: '系统将于今晚23:00-01:00进行维护', type: 'system', read: true, createTime: '2026-06-21 09:00' },
    { id: '4', title: '任务审核通过', content: '您发布的任务已通过审核', type: 'task', read: true, createTime: '2026-06-20 16:00' },
  ]
})
</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-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);
}

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

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