<template>
	<view class="container">
		<view class="search-box">
			<u-search
				v-model="searchForm.keyword"
				:clearabled="true"
				bg-color="#E5E5E5"
				:input-style="searchInputStyle"
				placeholder="请输入搜索内容"
			></u-search>
		</view>
		<view class="list-box">
			<view class="list-item-box" v-for="item in list" :key="item.id" @click="onChatClick(item)">
				<view class="main-box">
					<!-- <view class="icon-box">
						<cover-image class="icon" :src="`https://sylwt.top/api/visitor/resources/image?name=/ydl/menber-center/chat-icon/${item.type}.png`"></cover-image>
						<view class="tag" v-show="item.isRead"></view>
					</view> -->
					<view class="content-box">
						<view class="title">
							{{item.title}}
						</view>
						<view class="text">
							{{item.content}}
						</view>
						<view class="tag" v-show="item.isRead"></view>
					</view>
					<view class="other-box">
						<view class="date">
							{{item.time}}
						</view>
					</view>
				</view>
				<view class="line-box"></view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import { ref } from 'vue'
	import { onLoad, onReachBottom } from '@dcloudio/uni-app'
	import { query, count } from '@/api/chat.js'
	
	const uToast = ref()
	
	const searchInputStyle = {
		backgroundColor: '#E5E5E5'
	}
	const pageNum = ref(1)
	const pageSize = ref(10)
	const visualLoadMore = ref(false)
	const loadMoreStatus = ref('more')
	const searchForm = ref({
		keyword: '',
		pageNumber: 1,
		pageSize: 10
	})
	const list = ref([
		{
			id: '1',
			title: '会费缴交成功通知',
			content: '尊敬的用户，您在2023年8月16日15:00成交缴交会费……',
			type: 'zhifu',
			time: '8/21',
			isRead: true
		},
		{
			id: '2',
			title: '继续教育通知',
			content: '尊敬的用户，您预约2023年8月16日《广州市存量房网……',
			type: 'jiaoyu',
			time: '8/20',
			isRead: true
		},
		{
			id: '3',
			title: '课程购买成功通知',
			content: '尊敬的用户，您在成功购买2023年8月16日《广州市存……',
			type: 'goumai',
			time: '8/19',
			isRead: true
		},
		{
			id: '4',
			title: '个人会员生日祝福',
			content: '尊敬的用户，今日是您的生日，协会祝你在……',
			type: 'shengri',
			time: '8/16',
			isRead: false
		},
	])
	
	function onSearch() {
		loadMoreStatus.value = 'more'
		query(searchForm.value).then(res => {
			if (res && res.message === 'success') {
				list.value = res.data
				if (res.count === 0) {
					visualLoadMore.value = false
				}
			}
		})
	}
	
	function onChatClick(chat) {
		uni.navigateTo({
			url: `/pages/chatDetail/chatDetail?id=${chat.id}&title=${chat.title}`
		})
	}
	
	
	onReachBottom(async () => {
		if (loadMoreStatus.value === 'noMore') {
			uni.showToast({
				title: '没有更多啦＞﹏＜',
				icon: 'none'
			})
			return
		}
		visualLoadMore.value = true
		loadMoreStatus.value = 'loading'
		searchForm.value.pageNum++
		query(searchForm.value).then(res => {
			if (res && res.message === 'success') {
				if (res.data) {
					if (res.count >= list.value.length) {
						if (list.value.length === res.count) {
							loadMoreStatus.value = 'noMore'
							visualLoadMore.value = true
						} else {
							list.value.push(...res.data)
							loadMoreStatus.value = 'more'
							visualLoadMore.value = false
						}							
					} else {
						loadMoreStatus.value = 'noMore'
						visualLoadMore.value = true
					}
				} else {
					loadMoreStatus.value = 'noMore'
					visualLoadMore.value = true
				}
			}
		})
	})
	onLoad(() => {
		onSearch()
		count().then(res => {
			if (res && res.message === 'success') {
				uni.setTabBarBadge({ //显示数字
					index: 1, //tabbar下标
					text: `${res.data.amount}` ?? '0' //数字
				})
			}
		})
	})
</script>

<style lang="scss" scoped>
	.container {
		height: 100vh;
		width: 100vw;
		background-color: $uni-bg-color;
		padding: 0 20rpx;
		
		.search-box {
			margin-bottom: 20rpx;
			@include topMagnet();
			::v-deep(.u-search) {
				background-color: #e5e5e5;
				border-radius: 50rpx;
				
				.u-action {
					width: 18%;
					background-color: $uni-color-primary;
					border-radius: 50rpx;
					color: $uni-text-color-inverse;
					margin-right: 8rpx;
					font-size: 28rpx;
					line-height: 50rpx;
					letter-spacing: 3rpx;
					text-align: center;
				}
			}
		}
	
		.list-box {
			padding: 0 20rpx;
			.list-item-box {
				.main-box {
					display: flex;
					justify-content: space-between;
					align-items: center;
					padding: 30rpx 0;
					
					&:active {
						background-color: $uni-bg-color-hover;
					}
					// .icon-box {
					// 	width: 15%;
					// 	display: flex;
					// 	justify-content: center;
					// 	align-items: center;
					// 	position: relative;
					// 	.icon {
					// 		width: 70rpx;
					// 		height: 70rpx;
					// 	}
					// 	.tag {
					// 		position: absolute;
					// 		right: 5rpx;
					// 		top: -3rpx;
					// 		width: 15rpx;
					// 		height: 15rpx;
					// 		background-color: $uni-color-error;
					// 		border-radius: 50%;
					// 	}
					// }
					.content-box {
						width: 85%;
						display: flex;
						flex-direction: column;
						justify-content: space-around;
						gap: 10rpx;
						position: relative;
						.title {
							height: calc(70% - 5rpx);
							font-size: $uni-title-font-size-2;
							font-weight: bold;
						}
						.text {
							height: calc(30% - 5rpx);
							font-size: $uni-font-size-2;
							color: $uni-text-color-grey;
							@include text-overflow();
						}
						.tag {
							position: absolute;
							left: -14rpx;
							top: -3rpx;
							width: 15rpx;
							height: 15rpx;
							background-color: $uni-color-error;
							border-radius: 50%;
						}
					}
					.other-box {
						width: 15%;
						display: flex;
						flex-direction: column;
						justify-content: flex-end;
						align-items: center;
						gap: 10rpx;
						.date {
							font-size: $uni-font-size-2;
							color: $uni-text-color-grey;
						}
					}
				}
				.line-box {
					height: 1rpx;
					width: 80%;
					background-color: #E5E5E5;
					margin: 0 auto;
				}
			}
		}
	}
		
	
</style>
