<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.readType=='yes'?'':'unread'">
						<view v-if="item.type=='urgent'" class="item-icon bg-error">
							<u-icon name="bell" color="#fff" size="54"></u-icon>
						</view>
						<view v-else-if="item.type=='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.title}}</text>
							<text class="item-time u-skeleton-fillet">{{item.createdAt}}</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>
	let list = [
		{
			id:0,
			type:'warning',
			title:'机柜告警机柜告警机柜告警机柜告警机柜告警机柜告警机柜告警机柜告警机柜告警机柜告警机柜告警机柜告警机柜告警',
			message:'电池长时间没充电',
			params:'id=xxxx',
			createdAt:'2020-10-26 10:50:49',
			status:false
		},
		{
			id:1,
			type:'warning',
			title:'电池告警',
			message:'短路保护',
			params:'id=xxxx',
			createdAt:'2020-10-26 10:40:49',
			status:false
		},
		{
			id:2,
			type:'approval',
			title:'租金审批',
			message:'XXX有一笔租金需要审批',
			params:'id=xxxx',
			createdAt:'2020-10-26 10:30:49',
			status:false
		},
		{
			id:3,
			type:'approval',
			title:'改装维修审批',
			message:'XXX有一笔改装维修需要审批',
			params:'id=xxxx',
			createdAt:'2020-10-26 10:20:49',
			status:true
		},
		{
			id:4,
			type:'new',
			title:'通知消息',
			message:'XXX下午开会，XXX下午开会，XXX下午开会，XXX下午开会，XXX下午开会',
			params:'id=xxxx',
			createdAt:'2020-10-26 10:20:49',
			status:true
		}
	]
	import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins.js";
	export default {
		data() {
			return {
				newsList:list,
				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() {
			console.log("消息页show")
			this.isToken = uni.getStorageSync('token');
			this.mescroll.resetUpScroll(false);
			if(this.isToken){
				this.getUnreadNewNum();
			}
			// this.getUnreadNewNum();
		},
		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 = {
					page:pageNum,
					limit:pageSize,
					msgType:this.tabIndex==0?'private':'public'
				}
				if (this.isToken) {
					this.$api.getNewList(obj).then((res)=>{
						console.log("当前获取到消息",res)
						if (res.data.rows && res.data.rows.length>0) {
							// 接口返回的当前页数据列表 (数组)
							let curPageData = res.data.rows;
							// 接口返回的当前页数据长度 (如列表有26个数据,当前页返回8个,则curPageLen=8)
							let curPageLen = curPageData && curPageData.length;
							//设置列表数据
							console.log(curPageLen,res.data.total);
							if(pageNum == 1){
								this.newsList = []; //如果是第一页需手动置空列表
								// this.getUnreadNewIdList(); //
								this.getUnreadNewNum();
							}
							this.newsList = this.newsList.concat(curPageData); //追加新数据
							this.mescroll.endBySize(curPageLen, res.data.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(){
				this.$api.getUnreadNewNum({}).then((res)=>{
					console.log("当前获取未读消息数量",res)
					if(res.code == 1){
						if(res.data.count){
							uni.showTabBarRedDot({
							        index: 2
							})
						}else{
							uni.hideTabBarRedDot({
							        index: 2
							})
						}
						this.tabList[0].count = res.data.privateCount;
						this.tabList[1].count = res.data.publicCount;
					}else{
						uni.showToast({
						    title: res.msg,
								icon:"none",
						    duration: 2000
						});
					}
				}).catch(()=>{
					this.$u.toast('服务器开小差了呢，请您稍后再试')
				})
			},
			// getUnreadNewIdList(){
			// 	this.$api.getUnreadNewIdList({}).then((res)=>{
			// 		console.log("当前获取未读消息id列表",res)
			// 		if (res.data.ids && res.data.ids.length>0) {
			// 			this.unreadNewIdList = res.data.ids;
			// 		} else{
			// 			this.unreadNewIdList = [];
			// 		}
			// 	}).catch(()=>{
			// 	})
			// }
		}
	}
</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>
