<template>
	<view class="new-box">
		<mescroll-body class="new-mescroll u-skeleton" ref="mescrollRef" top="0" @init="mescrollInit" @down="downCallback" @up="upCallback" :up="upOption" >
			<view class="new-list" v-if="newsList && newsList.length > 0">
				<view class="new-list-item " v-for="(item,index) in newsList" :key="index" @tap="goDetail(index)">
					<view class="item-icon-box u-skeleton-circle" :class="item.hasRead?'':'unread'">
						<view v-if="item.noticeType=='1'" class="item-icon bg-warning">
							<u-icon name="bell" color="#fff" size="54"></u-icon>
						</view>
<!-- 						<view v-else-if="item.noticeType=='warning'" class="item-icon bg-warning">
							<u-icon name="error" color="#fff" size="54"></u-icon>
						</view> -->
						<view v-else class="item-icon bg-info">
							<u-icon name="chat" color="#fff" size="54"></u-icon>
						</view>
					</view>
					<view class="item-content">
						<view class="item-content-top">
							<text class="item-title u-line-1 u-skeleton-fillet">{{item.noticeTitle}}</text>
							<text class="item-time u-skeleton-fillet">{{item.createTime}}</text>
						</view>
						<view class="item-content-txt u-line-1 u-skeleton-fillet">{{item.message}}</view>
					</view>
				</view>
			</view>
		</mescroll-body>
		<!--引用组件-->
		<u-skeleton :loading="loading" :animation="true" bgColor="#FFF"></u-skeleton>
	</view>
</template>

<script>
	import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins.js";
	export default {
		data() {
			return {
				newsList:[],
				unreadNewIdList:[],
				loading: true, // 是否显示骨架屏组件
				upOption:{
					page:{
						num : 0 ,
						size : 12 ,
						time : null
					},
					noMoreSize: 4,
					empty:{
						icon:'https://fast.gk-hd.com/resource/app-sec/new.png',
						use : true ,
						tip: '~ 消息列表为空 ~',
					  fixed: true,
						top: "200rpx",
					},
					bgColor:"#f5f5f5"
				},
				isToken:false,
				tabList:[{
					name: '我的信息',
					count: 0
				}, {
					name: '系统公告',
					count: 0
				}],
				tabIndex: 0
			}
		},
		mixins:[MescrollMixin],
		onShow() {
			this.isToken = uni.getStorageSync('userToken');
			// if(this.isToken){
			// 	this.getUnreadNewNum();
			// }
			this.getUnreadNewNum();
			this.$nextTick(() => {
					if (this.mescroll) {
							this.mescroll.resetUpScroll();
					} else {
							console.warn('mescroll is not initialized yet');
					}
			});
		},
		methods:{
			tabChange(index) {
				this.tabIndex = index;
				this.mescroll.resetUpScroll(false);
			},
			/*上拉加载的回调*/
			upCallback(page) {
				console.log("查询数据",page)
				let pageNum = page.num; // 页码, 默认从1开始
				let pageSize = page.size; // 页长, 默认每页10条
				let obj = {
					pageNum:pageNum,
					pageSize:pageSize,
				}
				if (this.isToken) {
					this.$api.getNewList(obj).then((res)=>{
						console.log("当前获取到消息",res)
						if (res.rows && res.rows.length>0) {
							// 接口返回的当前页数据列表 (数组)
							let curPageData = res.rows;
							// 接口返回的当前页数据长度 (如列表有26个数据,当前页返回8个,则curPageLen=8)
							let curPageLen = curPageData && curPageData.length;
							//设置列表数据
							console.log(curPageLen,res.total);
							if(pageNum == 1){
								this.newsList = []; //如果是第一页需手动置空列表
								// this.getUnreadNewIdList(); //
								// this.getUnreadNewNum();
							}
							this.newsList = this.newsList.concat(curPageData); //追加新数据
							this.mescroll.endBySize(curPageLen, res.total); // 推荐
							this.loading = false;
							console.log("newsList",this.newsList)
						} else{
							this.newsList = [];
							this.mescroll.endErr();
							this.mescroll.showEmpty();
							this.loading = false;
							// this.mescroll.endUpScroll(true)
						}
					}).catch(()=>{
						this.$u.toast('服务器开小差了呢，请您稍后再试')
						this.newsList = [];
						//联网失败, 结束加载
						this.mescroll.endErr();
						this.mescroll.showEmpty();
						this.loading = false;
					})
				}else{
					this.newsList = [];
					this.mescroll.endErr();
					this.mescroll.showEmpty();
					this.loading = false;
				}
			},
			// 跳转消息详情
			goDetail(index){
				console.log("当前点击消息",this.newsList[index])
				let obj = JSON.stringify(this.newsList[index]);
				uni.navigateTo({
					url: '../new/detail?obj=' + encodeURIComponent(obj)
				})
			},
			getUnreadNewNum(){
				let obj = {
					pageNum:1,
					pageSize:10,
					hasRead: false
				}
				this.$api.getNewList(obj).then((res)=>{
					console.log("当前获取未读消息数量",res)
					if(res.code == 200){
						if(res.total){
							uni.showTabBarRedDot({
							        index: 0
							})
						}else{
							uni.hideTabBarRedDot({
							        index: 0
							})
						}
					}else{
					}
				}).catch(()=>{
					this.$u.toast('服务器开小差了呢，请您稍后再试')
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.bg-error{
		background-color: $u-type-error;
	}
	.bg-warning{
		background-color: $u-type-warning;
	}
	.bg-info{
		background-color: $u-type-info;
	}
	.tabs-box{
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		z-index: 10;
	}
	.new-box{
		height: 100%;
		.new-list{

			.new-list-item{
				display: flex;
				align-items: center;
				padding: 15rpx 30rpx;
				border-bottom: 1px solid #f2f2f2;
				.item-icon-box{
					position: relative;
					margin-right: 20rpx;
					&.unread::after{
						content: '';
						position: absolute;
						top: 0rpx;
						right: 0rpx;
						width: 20rpx;
						height: 20rpx;
						border-radius: 50%;
						border: 1px solid #fff;
						background-color: red;
					}
					.item-icon{
						width: 100rpx;
						height: 100rpx;
						border-radius: 50%;
						display: flex;
						align-items: center;
						justify-content: center;
						box-shadow: 2px 2px 2px -2px rgba(159,159,159,0.66);
						-webkit-box-shadow: 2px 2px 2px -2px rgba(159,159,159,0.66);
						-moz-box-shadow: 2px 2px 2px -2px rgba(159,159,159,0.66);
						.iconfont-gx{
							color: #fff;
							font-size: 54rpx;
						}
					}
				}

				.item-content{
					flex: 1;
					width: 0;
					height: 100rpx;
					display: flex;
					flex-direction: column;
					justify-content: space-around;
					.item-content-top{
						display: flex;
						justify-content: space-between;
						align-items: center;
						.item-title{
							font-weight: bold;
							font-size: 30rpx;
							width: 320rpx;
						}
						.item-time{
							color: $u-tips-color;
							font-size: 24rpx;
						}
					}
					.item-content-txt{
						width: 100%;
						color: $u-tips-color;
						font-size: 24rpx;
					}
				}
			}

		}
	}

</style>
