<template>
	<view class="page">
		<u-empty v-if="sysMsgData.length == 0" text="暂无系统消息" mode="list"></u-empty>
		<view v-if="sysMsgData.length > 0" class="viewBox">
			<scroll-view :scroll-y="true" :enable-back-to-top="true" :enable-flex="true" @scrolltolower="lower"
				:style="{ height: '100%' }">
				<view v-for="(item,index) in sysMsgData" :key="index">
					<view class="itemBox">
						<view class="date">
							{{item.date.slice(0,10)}}
						</view>
						<view v-if="item.list" v-for="(subItem,subIndex) in item.list" :key="subIndex">
							<view class="subItemBox">
								<view class="top">
									<image class="icon" :src="icon" mode="heightFix"></image>
									<view class="type">
										系统消息
									</view>
								</view>
								<view class="tipMsg" :style="{'color':(subItem.Type==9?'#F05353':'#000000;')}">
									{{subItem.Content}}
								</view>
								<view class="conBox">
									<image class="img" :src="subItem.ReplyContent[0].CoverImg"></image>
									<view class="con">
										{{subItem.ReplyContent[0].ActivityName}}
									</view>
								</view>
							</view>
						</view>
					</view>
				</view>
				<u-loadmore :status="status" :load-text="loadText" :font-size="24" :margin-top="20" :margin-bottom="20"
					bg-color="#EAEBEC" />
			</scroll-view>
		</view>
		<!-- 加载中 -->
		<view class="loading" v-if="loading">
			<u-loading mode="flower" size="48"></u-loading>
			<Text style="color: #fff; margin-top: 10rpx;">加载中...</Text>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				pageTitle: "系统消息",
				icon: "https://viitto-1301420277.cos.ap-chengdu.myqcloud.com/Static/Blind-ppplus.png",
				msg: {
					pageIndex: 1,
					pageSize: 10,
					SelectType: 4,
					Status: -1
				},
				sysMsgData: [],
				pageCount: null,
				loading: false,
				status: "loadmore",
				loadText: {
					loadmore: "轻轻上拉，加载更多",
					loading: "努力加载中",
					nomore: "没有更多了",
				},
				showone:true
			}
		},
		methods: {
			getMiaiMessagePageList() {
				this.request2({
						url: '/api/AppletMiai/GetMiaiMessagePageList',
						data: this.msg
					},
					res => {
						if (res.resultCode == 1) {
							this.loading = false;
							let data = this.handleDate(res.data.pageData, "CreateDate")
							console.log(data)
							this.sysMsgData = this.sysMsgData.concat(data);
							this.pageCount = res.data.pageCount
							if (this.pageCount == 1) {
							   this.status = "nomore";
							}
							if( this.showone == true && this.sysMsgData.length>0){
								this.setAllMsgSee()
								this.showone = false
							}
						}
					}
				);
			},
			//合并相同日期的元素
			handleDate(arr, date) {
				let newArr = []
				arr.map((item, i) => {
					let find = newArr.filter((e) => {
						return e.date.slice(0, 10) == item[date].slice(0, 10)
					})
					if (find.length > 0) {
						find[0].list.push(item)
					} else {
						let obj = {
							date: item[date],
							list: [item]
						}
						newArr.push(obj)
					}
				}, [])
				return newArr
			},
			lower(e) {
				console.log("1")
				if (this.msg.pageIndex < this.pageCount) {
					this.msg.pageIndex++;
					this.status = "loading";
					this.getMiaiMessagePageList();
				} else {
					this.status = "nomore";
				}
			},
			//设置全部消息为已看
			setAllMsgSee() {
				this.request2({
						url: '/api/AppletMiai/DelMiaiMessageInfoBatch',
						data: {
							TypeIds: "8,9,10"
						},
					},
					res => {
						if (res.resultCode == 1) {
			
						}
					}
				);
			}
		},
		onShow() {
			this.getMiaiMessagePageList();
			
		},
		mounted() {
			uni.setNavigationBarTitle({
				title: this.pageTitle,
			});
			uni.setNavigationBarColor({
				backgroundColor: '#FFFFFF',
			})
		}
	}
</script>

<style lang="scss" scoped>
	.page {
		padding: 0 30rpx;
		min-height: 100vh;
		background-color: #EAEBEC;
	}
	.viewBox{
	height: calc(100vh);
	overflow: hidden;
	}
	.itemBox {
		.date {
			height: 84rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			font-size: 26rpx;
			font-weight: 500;
			color: #888888;
		}

		.subItemBox {
			// height: 292rpx;
			padding: 0 30rpx 30rpx;
			background-color: #FFFFFF;
			border-radius: 4rpx;
			margin-bottom: 30rpx;

			.top {
				width: 100%;
				height: 84rpx;
				display: flex;
				align-items: center;
				padding-top: 37rpx;

				.icon {
					width: 30rpx;
					height: 30rpx;
					margin-right: 10rpx;
				}

				.type {
					font-size: 30rpx;
					font-weight: 800;
					color: #000000;
				}
			}

			.tipMsg {
				font-size: 26rpx;
				font-weight: bold;
				color: #000000;
				margin-bottom: 30rpx;
				margin-top: 20rpx;
			}

			.conBox {
				height: 110rpx;
				background-color: #F5F5F5;
				border-radius: 4rpx;
				display: flex;

				.img {
					width: 110rpx;
					height: 110rpx;
				}

				.con {
					flex-grow: 5;
					padding: 0 20rpx;
					display: flex;
					align-items: center;
					font-size: 26rpx;
					font-weight: 500;
					color: #111111;
				}
			}
		}
	}
</style>
