<template>
	<view class="page">
		<view v-if="list.length" class="msg-list">
			<view class="msg-item" v-for="item in list" :key="item.id" @tap="goDetail(item.id)">
				<view class="msg-icon" :class="item.type === 1 ? 'success' : 'fail'">
					<u-icon :name="item.type === 1 ? 'checkmark-circle-fill' : 'close-circle-fill'" size="22"
						:color="item.type === 1 ? '#52c41a' : '#f5222d'" />
				</view>
				<view class="msg-body">
					<view class="msg-title-row">
						<text class="msg-title">{{ item.title }}</text>
						<view v-if="!item.is_read" class="unread-dot"></view>
					</view>
					<text class="msg-desc">{{ item.content }}</text>
					<text class="msg-time">{{ item.create_time }}</text>
				</view>
				<text class="arrow">›</text>
			</view>
		</view>
		<view v-if="!loading && !list.length" class="empty">
			<u-icon name="bell" size="60" color="#ccc" />
			<text class="empty-text">暂无消息</text>
		</view>
		<view v-if="loading" class="loading-tip">
			<u-loading-icon size="24" />
		</view>
		<view v-if="!loading && finished && list.length" class="no-more">没有更多了</view>
	</view>
</template>

<script setup>
import { ref } from 'vue'
import { onLoad, onPullDownRefresh, onReachBottom } from '@dcloudio/uni-app'
import { get } from '@/utils/request.js'

const list = ref([])
const page = ref(1)
const loading = ref(false)
const finished = ref(false)

onLoad(() => {
	loadMessages()
})

onPullDownRefresh(() => {
	page.value = 1
	finished.value = false
	list.value = []
	loadMessages().then(() => uni.stopPullDownRefresh())
})

onReachBottom(() => {
	if (!finished.value && !loading.value) loadMessages()
})

const loadMessages = async () => {
	if (loading.value) return
	loading.value = true
	try {
		const res = await get('/api/mp/messages', { page: page.value, pageSize: 20 })
		const data = res.data || {}
		const items = data.data || []
		if (page.value === 1) {
			list.value = items
		} else {
			list.value.push(...items)
		}
		if (page.value >= (data.totalPages || 1)) {
			finished.value = true
		} else {
			page.value++
		}
	} catch (e) {}
	loading.value = false
}

const goDetail = (id) => {
	const item = list.value.find(m => m.id === id)
	if (item) item.is_read = 1
	uni.navigateTo({ url: `/pages/message/detail?id=${id}` })
}
</script>

<style lang="scss" scoped>
.page {
	min-height: 100vh;
	background: #f4f4f5;
	padding: 24rpx;
}

.msg-item {
	display: flex;
	align-items: flex-start;
	background: #fff;
	border-radius: 10rpx;
	padding: 28rpx 24rpx;
	margin-bottom: 20rpx;
	border: 1rpx solid #ebeef5;
}

.msg-icon {
	width: 56rpx;
	height: 56rpx;
	border-radius: 50%;
	display: flex;
	align-items: center;
	justify-content: center;
	flex-shrink: 0;
	margin-right: 20rpx;
	margin-top: 4rpx;

	&.success {
		background: #f6ffed;
	}

	&.fail {
		background: #fff2f0;
	}
}

.msg-body {
	flex: 1;
	min-width: 0;
}

.msg-title-row {
	display: flex;
	align-items: center;
	gap: 12rpx;
}

.msg-title {
	font-size: 30rpx;
	font-weight: 600;
	color: #333;
}

.unread-dot {
	width: 14rpx;
	height: 14rpx;
	border-radius: 50%;
	background: #f5222d;
	flex-shrink: 0;
}

.msg-desc {
	font-size: 26rpx;
	color: #888;
	margin-top: 8rpx;
	display: -webkit-box;
	-webkit-line-clamp: 1;
	-webkit-box-orient: vertical;
	overflow: hidden;
}

.msg-time {
	font-size: 24rpx;
	color: #bbb;
	margin-top: 8rpx;
}

.arrow {
	color: #c0c4cc;
	font-size: 28rpx;
	flex-shrink: 0;
	margin-left: 12rpx;
	margin-top: 8rpx;
}

.empty {
	display: flex;
	flex-direction: column;
	align-items: center;
	padding-top: 200rpx;
}

.empty-text {
	font-size: 28rpx;
	color: #ccc;
	margin-top: 20rpx;
}

.loading-tip {
	display: flex;
	justify-content: center;
	padding: 40rpx 0;
}

.no-more {
	text-align: center;
	font-size: 24rpx;
	color: #ccc;
	padding: 30rpx 0;
}
</style>
