<template>
  <view class="message-page" :class="{ 'wechat-browser': isWeixinBrowser }">
    <view class="header-title" :class="{ 'hide-in-wechat': isWeixinBrowser }">消息</view>
    <view class="message-list">
      <view
        v-for="(item, idx) in noticeList"
        :key="item.id"
        class="message-card"
      >
        <view class="message-card-header">
          <view class="tag" :style="{ background: tagColors[idx % tagColors.length] }">
            <text class="tag-text">{{ item.type_name }}</text>
          </view>
          <view class="date">{{ formatChinaDate(item.created_at) }}</view>
        </view>
        <view class="message-title">{{ item.title }}</view>
        <view class="message-content">{{ item.content }}</view>
      </view>
    </view>
  </view>
</template>

<script>
import { API } from '@/config/index.js';
export default {
  name: 'MessagePage',
  data() {
    return {
      noticeList: [],
      isWeixinBrowser: false,
      tagColors: [
        'linear-gradient(90deg, #ffb980 0%, #ffc99a 100%)',
        'linear-gradient(90deg, #217aff 0%, #3b7cff 100%)',
        'linear-gradient(90deg, #ff5c5c 0%, #ff7a7a 100%)',
        'linear-gradient(90deg, #22c58b 0%, #2ed9a3 100%)',
        '#f39c12', '#8e44ad', '#16a085'
      ]
    }
  },
  onLoad() {
    // #ifdef H5
    this.isWeixinBrowser = /MicroMessenger/i.test(navigator.userAgent)
    // #endif
  },
  onShow() {
    this.fetchNotifications();
  },
  methods: {
    async fetchNotifications() {
      const token = uni.getStorageSync('token');
      if (!token) return;
      try {
        const res = await new Promise((resolve, reject) => {
          uni.request({
            url: `${API.NOTIFICATION_LIST}?token=${token}`,
            method: 'GET',
            success: resolve,
            fail: reject
          });
        });
        if (res.data && res.data.errcode === 0) {
          this.noticeList = Array.isArray(res.data.data.data) ? res.data.data.data : [];
        }
      } catch (e) {
        console.error('Failed to fetch notifications:', e);
      }
    },
    formatChinaDate(dateStr) {
      if (!dateStr) return '';
      const date = new Date(dateStr);
      const y = date.getFullYear();
      const m = String(date.getMonth() + 1).padStart(2, '0');
      const d = String(date.getDate()).padStart(2, '0');
      return `${y}-${m}-${d}`;
    }
  }
}
</script>

<style scoped>
.message-page {
	background: linear-gradient(180deg, #cbe6ff 0%, #f6faff 100%);
  min-height: 100vh;
  padding-bottom: 80px;
  font-family: 'SourceHanSansCN', 'PingFang SC', 'Microsoft YaHei', sans-serif;
}

.wechat-browser {
  margin-top: -44rpx;
}

.header-title {
  text-align: center;
  font-size: 36rpx;
  font-weight: bold;
  padding-top: 7vh;
  letter-spacing: 2rpx;
}
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 16px 10px 16px;
  background: linear-gradient(180deg, #cbe6ff 0%, #f6faff 100%);
}
.back-btn, .more-btn {
  font-size: 24px;
  color: #333;
}
.title {
  font-size: 22px;
  font-weight: bold;
  color: #222;
}
.message-list {
  padding: 10px 0 0 0;
  margin-top: 44rpx;
}
.message-card {
  background: #fff;
  border-radius: 10px;
  margin: 0 16px 16px 16px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.04);
  padding: 18px 18px 12px 18px;
  height: 272rpx;
}
.message-card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}
.tag {
  font-size: 22rpx;
  padding: 4rpx 18rpx;
  border-radius: 8rpx;
  margin-right: 24rpx;
  white-space: nowrap;
  color: #fff;
  display: inline-block;
  font-weight: 500;
  border: none;
  transform: skewX(-20deg);
}
.tag-text {
  display: inline-block;
  transform: skewX(20deg);
}
.date {
  color: #173766;
  font-size: 15px;
}
.message-title {
  font-size: 16px;
  font-weight: 550;
  margin-bottom: 12px;
  color: #222;
  margin-top: 12px;
}
.message-content {
  color: #355;
  font-size: 14px;
  color: #173766;
}
.tabbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  height: 60px;
  background: #fff;
  display: flex;
  border-top: 1px solid #eaeaea;
  z-index: 10;
}
.tab-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: #888;
  font-size: 14px;
}
.tab-item.active {
  color: #217aff;
}
.icon {
  font-size: 22px;
  margin-bottom: 2px;
}
.hide-in-wechat {
  display: none !important;
}
</style> 