<template>
	<view class="container">
		<uni-nav-bar fixed :border="false" backgroundColor="#fff" title="系统通知" :statusBar="true"
			left-icon="left" @clickLeft="$navBack()">
			<!-- #ifndef MP-WEIXIN -->
			<template v-slot:right>
				<image class="righticon" src="/static/image/readall.png" mode="aspectFill" @click="handleReadAll"></image>
			</template>
			<!-- #endif -->
		</uni-nav-bar>
		
		<view class="container-body">
			<mescroll-body ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback" :down="downOption" :up="upOption">
			<view>
				<view class="list-item" v-for="item in dataList" :key="item.id" @click.stop="handleDetail(item)">
					<view class="new" v-if="item.isRead != 1 && isallRead!=1"></view>
					<view class="list-item-head border-line">
						<view>{{item.title}}</view>
						<view class="time">{{item.createTime}}</view>
					</view>
					<view class="list-item-con">
						<view class="">{{item.content}}</view>
						<image class="img" v-if="item.imgUrl" :src="item.imgUrl" mode="aspectFill"></image>
					</view>
				</view>
			</view>
			</mescroll-body>
		</view>
		
	</view>
</template>

<script>
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
	import { getPushLog } from "@/api/user.js"
	import { getPushLogRead } from "@/api/user.js"
	export default {
		mixins: [MescrollMixin], 
		data() {
			return {
				mescroll:null,
				downOption: {   //下拉刷新
				 	use:true,
					auto: true // 不自动加载 (mixin已处理第一个tab触发downCallback)
				},
				upOption: {
					onScroll:false,
					use: true, // 是否启用上拉加载; 默认true
					page: {
						pae: 0, // 当前页码,默认0,回调之前会加1,即callback(page)会从1开始
						size: 10 // 每页数据的数量,默认10
					},
					noMoreSize: 10, // 配置列表的总数量要大于等于5条才显示'-- END --'的提示
					textNoMore:"已经到底了",
					empty: {
						icon:'https://cos.his.cdwjyyh.com/fs/20240423/cf4a86b913a04341bb44e34bb4d37aa2.png',
						tip: '暂无数据'
					}
				},
				dataList: [],
				isallRead: 0
			}
		},
		methods: {
			mescrollInit(mescroll) {
				this.mescroll = mescroll;
			},
			/*下拉刷新的回调 */
			downCallback(mescroll) {
				mescroll.resetUpScroll()
			},
			upCallback(page) {
				//联网加载数据
				let that = this;
				let data = {
					pageNum: page.num,
					pageSize: page.size
				};
				getPushLog(data).then(res => {
					if(res.code==200){
						//设置列表数据
						if (page.num == 1) {
							that.dataList = res.rows; 
						} else {
							that.dataList = that.dataList.concat(res.rows);
							 
						}
						that.mescroll.endBySize(res.rows.length, res.total);
						
					}else{
						uni.showToast({
							icon:'none',
							title: "请求失败",
						});
						that.dataList = [];
						that.mescroll.endErr();
					}
				});
			},
			handleDetail(item) {
				console.log("qxj handleDetail");
				console.log(item);
				if(item.url) {
					uni.navigateTo({url: item.url});
					this.setReadAll('item',item);
				}else{
					this.setReadAll('item',item);
				}
			},
			handleReadAll() {
				uni.showModal({
					title: '',
					content: '确定标记所有未读消息为已读吗？',
					success: (res)=> {
						if (res.confirm) {
							this.setReadAll('all')
						}
					}
				});
			},
			setReadAll(type,item) {
				const param = type=="all" ? [] : [item.id]
				if(type!="all" &&item.isRead == 1) {
					return
				}
				getPushLogRead(param).then(res=>{
					if(res.code == 200) {
						setTimeout(e => {
							this.$updateMsgDot();
						}, 1000);
						if(type=="all") {
							this.isallRead = 1
							uni.showToast({
								title: res.msg,
								icon: 'none'
							});
						} else {
							item.isRead = 1
						}
					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		
	}
	.righticon {
		width: 48rpx;
		height: 48rpx;
		margin-right: 16rpx;
	}
	.container-body {
		padding: 24rpx;
		padding-bottom: calc(var(--window-bottom) + 24rpx);
		box-sizing: border-box;
	}
	.new {
		position: absolute;
		display: inline-block;
		right: 4px;
		top: 4px;
		background: red;
		width: 11px;
		height: 11px;
		border-radius: 50%;
		color: #ffffff;
	}
	.list-item {
		position: relative;
		margin-bottom: 24rpx;
		background-color: #fff;
		border-radius: 16rpx;
		overflow: hidden;
		font-family: PingFang SC, PingFang SC;
		font-weight: 400;
		font-size: 28rpx;
		color: #555;
		word-break: break-all;
		&-head {
			padding: 24rpx;
			font-size: 32rpx;
			font-weight: 500;
			color: #000;
			.time {
				font-size: 24rpx;
				color: #999999;
			}
		}
		&-con {
			padding: 24rpx;
			.img {
				width: 100%;
				height: 300rpx;
				margin-top: 16rpx;
			}
		}
	}
</style>