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

    <view class="px-4 pt-3">
      <view class="glass-card p-5">
        <text class="notice-title">{{ message?.title || '消息详情' }}</text>
        <view class="meta-row">
          <text class="meta-text">{{ message?.createTime ? formatDate(message.createTime, 'YYYY-MM-DD HH:mm') : '' }}</text>
          <text v-if="!isRead" class="unread-tag">未读</text>
        </view>

        <view class="notice-body">
          <text class="body-p">{{ message?.content || '暂无内容' }}</text>
        </view>
      </view>

      <view v-if="!isRead" class="px-4 pt-2">
        <view
          class="action-btn action-btn-primary"
          hover-class="action-btn-hover"
          :hover-start-time="0"
          :hover-stay-time="120"
          @click="handleMarkRead"
        >
          标记已读
        </view>
      </view>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import TopBar from '@/components/common/TopBar.vue'
import { formatDate } from '@/utils'
import { useNotificationStore } from '@/stores/notification'
import type { Message } from '@/api/notice'

const notificationStore = useNotificationStore()
const message = ref<Message | null>(null)

const isRead = computed(() => {
  if (!message.value) return true
  return message.value.isRead === 1 || message.value.readStatus === 1
})

async function handleMarkRead() {
  if (!message.value?.messageId) return
  await notificationStore.markAsRead(message.value.messageId)
  message.value.isRead = 1
  message.value.readStatus = 1
}

onMounted(() => {
  const data = uni.getStorageSync('current_message')
  if (data) {
    message.value = data
  }
})
</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;
}

.notice-title {
  display: block;
  font-size: 17px;
  font-weight: 700;
  color: #1f2937;
  line-height: 1.4;
}

.meta-row {
  margin-top: 8px;
  padding-bottom: 12px;
  border-bottom: 1px solid rgba(164, 216, 152, 0.18);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.meta-text {
  font-size: 12px;
  color: #9ca3af;
}

.unread-tag {
  padding: 2px 10px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
  color: #ef4444;
  background-color: rgba(239, 68, 68, 0.1);
}

.notice-body {
  padding-top: 12px;
}

.body-p {
  display: block;
  margin-bottom: 12px;
  font-size: 14px;
  line-height: 1.8;
  color: #4b5563;
}

.action-btn {
  width: 100%;
  padding: 12px 0;
  border-radius: 14px;
  text-align: center;
  font-size: 15px;
  font-weight: 600;
}

.action-btn-hover {
  opacity: 0.88;
}

.action-btn-primary {
  color: #ffffff;
  background: linear-gradient(135deg, #368f6f 0%, #4ba98a 100%);
  box-shadow: 0 10px 24px -10px rgba(54, 143, 111, 0.7);
}
</style>
