<template>
	<view class="container">
		<view class="search-box">
			<u-search
				v-model="searchForm.keyword"
				:clearabled="true"
				bg-color="#E5E5E5"
				:input-style="searchInputStyle"
				placeholder="请输入搜索内容"
				@search="onSearchConfirm"
				@custom="onSearchConfirm"
				@clear="onSearchClear"
			></u-search>
		</view>
		<u-empty
			mode="data"
			v-if="list.length === 0"
			iconSize="120"
			textSize="58"
			text="暂无数据"
		>
		</u-empty>
		<view class="list-box">
			<view class="list-item-box" v-for="(item, index) in list" :key="item.id" @click="onChatClick(item, index)">
				<view class="main-box">
					<view class="content-box">
						<view class="title">
							{{item.title}}
						</view>
						<view class="text">
							{{item.content}}
						</view>
						<view class="tag" v-show="!item.read"></view>
					</view>
					<view class="other-box">
						<view class="date">
							{{getDate(item.createTime)}}
						</view>
					</view>
				</view>
				<view class="line-box"></view>
			</view>
		</view>
		<uni-load-more v-show="visualLoadMore" :status="loadMoreStatus"></uni-load-more>
		<!-- 与包裹页面所有内容的元素u-page同级，且在它的下方 -->
		<u-tabbar v-model="tabbarCurrentIndex" :list="tabbarList" icon-size="50" :active-color="tabbarActiveColor" :inactive-color="tabbarInactiveColor"></u-tabbar>
	</view>
</template>

<script setup>
	import { ref, computed } from 'vue'
	import { onLoad, onShow, onReachBottom, onPullDownRefresh } from '@dcloudio/uni-app'
	import { query, count } from '@/api/chat.js'
	import { useTabbarStore } from '@/store/tabbarStore.js'

	const tabbarStore = useTabbarStore()
	// 底部导航栏数据
	const tabbarList = computed(() => {
		return tabbarStore.list
	})
	// 底部导航栏选中颜色
	const tabbarActiveColor = computed(() => {
		return tabbarStore.activeColor
	})
	// 底部导航栏未选中颜色
	const tabbarInactiveColor = computed(() => {
		return tabbarStore.inactiveColor
	})
	const tabbarCurrentIndex = 1
	
	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([])
	function getDate(date) {
		return date.split(' ')[0]
	}
	
	function onSearchConfirm() {
		searchForm.value.pageNumber = 1
		pageNum.value = 1
		onSearch()
	}
	
	function onSearchClear() {
		searchForm.value.keyword = null
		searchForm.value.pageNumber = 1
		pageNum.value = 1
		onSearch()
	}
	
	function onSearch() {
		loadMoreStatus.value = 'more'
		query(searchForm.value).then(res => {
			if (res && res.code === 0) {
				list.value = res.data
				if (res.count === 0) {
					visualLoadMore.value = false
				}
			}
		})
	}
	
	function onChatClick(chat, index) {
		list.value[index].read = true
		tabbarStore.setMessageCountRead()
		uni.navigateTo({
			url: `/pages/chatDetail/chatDetail?id=${chat.id}&title=${chat.title}`
		})
	}
	onPullDownRefresh((e) => {
		searchForm.value.pageNumber = 1
		loadMoreStatus.value = 'more'
		query(searchForm.value).then(res => {
			if (res && res.code === 0) {
				list.value = res.data
				if (res.count === 0) {
					visualLoadMore.value = false
				}
				uni.showToast({
					title: '刷新成功',
					icon: 'success',
					duration: 2000
				})
			}
			uni.stopPullDownRefresh()
		})
	})
	
	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.code === 0) {
				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
				}
			}
		})
	})
	onShow(() => {
		onSearch()
		count().then(res => {
			if (res && res.code === 0) {
				tabbarStore.setMessageCount(res.data.amount)
				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%;
						// width: 100%;
						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;
							padding-left: 10rpx;
						}
						.text {
							height: calc(30% - 5rpx);
							font-size: $uni-title-font-size-2;
							color: $uni-text-color-grey;
							@include text-overflow();
							padding-left: 10rpx;
						}
						.tag {
							position: absolute;
							left: -14rpx;
							top: 0rpx;
							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>
