<template>
	<view class="container">
		<view class="top">
			<view class="iconfont icon-youjiantou zuojiantou" @click="goBack()">
			</view>
			<view style="width: 60%;text-align: right;">
				消息列表
			</view>
			<view class="clear" @click="oneKeyRead()">
				<text class="iconfont icon-weibiaoti544"></text>
				<text>清除未读</text>	
			</view>
		</view>
		<view class="content" v-if="system_msgs_list && system_msgs_list.length > 0">
			<view class="announcement_wrapper" v-for="(item,index) in system_msgs_list" :key="index"
			@click="goToDetail(item.id)">
				<text class="iconfont icon-zizhutuiguang"></text>
				<view class="announcement_content">
					<view class="announcement_title">
						<text class="title" v-if="item.title">{{(item.title).substring(0,8)}}...</text> 
						<text class="title" v-if="!item.title">暂无标题</text>
						<text class="date">{{item.createdAt}}</text>
						<text class="readed" :class="{dot:item.isRead == 0}"></text>
					</view>
					<view class="announcement_des">
						{{matchReg(item.context).substring(0,15)}}...
					</view>
				</view>
			</view>
		</view>
		<view class="content" v-else>
			<view style="text-align: center;">暂无消息</view>
		</view>
	</view>
</template>

<script>
	import api from '../../api/api';
	export default{
		data(){
			return{
				system_msgs_list:null,
			}
		},
		onLoad(){
			this.querySystemMessage();
		},
		methods:{
			querySystemMessage() {
			   api.querySystemMessage({systemType:1,userId:uni.getStorageSync('cffp_userId')}).then(res => {
			     if (res['success']) {
			       this.system_msgs_list = res['data']['dataList'];
			     }else{
					 this.system_msgs_list = null;
				 }
			   });
			},
			matchReg(str){
			    let reg=/<\/?.+?\/?>/g;
			    return str.replace(reg,'');
			},
			goToDetail(id){
				uni.navigateTo({
					url:`../systemMsg/system_msg_detail?id=${id}`
				})
			},
			goBack(){
				uni.switchTab({
					url:'../index/index'
				})
			},
			oneKeyRead(){
				if( this.system_msgs_list.length == 0){
					uni.showToast({
						title: '暂无可清除未读消息',
						duration: 2000,
						icon: 'none'
					})
					return false
				}
				api.oneKeyRead({systemType:1,userId:uni.getStorageSync('cffp_userId')}).then((res)=>{
					if(res['success']){
						uni.showToast({
							title: '已清除所有未读信息',
							duration: 2000,
							icon: 'none'
						})
						this.querySystemMessage();
					}else{
						uni.showToast({
							title: res['message'],
							duration: 2000,
							icon: 'none'
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.top{
		display: flex;
		height: 100rpx;
		justify-content: space-between;
		align-items: center;
		position: relative;
		background: #fff;
		padding-right: 20rpx;
		
		.clear{
			display: flex;
			align-items: center;
			.icon-weibiaoti544{
				color: #5359cd;
				font-size: 50rpx;
			}
			text:nth-child(2){
				color: #999999;
				font-size: 20rpx;
			}
		}
		
	}
	.content{
		padding: 20rpx;
		.announcement_wrapper{
			display: flex;
			margin-bottom: 20rpx;
			background: #fff;
			align-items: center;
			flex-wrap: wrap;
			padding: 20rpx 10rpx;
			.icon-zizhutuiguang{
				color: #5359cd;
				background: #eff3ff;
				font-size: 40rpx;
				width: 70rpx;
				height:70rpx ;
				line-height: 70rpx;
				text-align: center;
				border-radius: 50%;
				margin-right: 30rpx;
			}
			.announcement_content{
				width:85%;
				.announcement_title{
					display: flex;
					justify-content: space-between;
					margin-bottom: 20rpx;
					padding-top: 20rpx;
					.title{
						font-size: 40rpx;
					}
					.date{
						color: #999999;
						font-size: 28rpx;
					}
					.readed{
						width: 20rpx;
						height: 20rpx;
						border-radius: 50%;
					}
					.dot{
						background: #EF6C40;
					}
				}
				.announcement_des{
					margin: 20rpx 0;
					color: #666666;
					font-size: 32rpx;
				}
			}
			.to_detail{
				display: flex;
				align-items: center;
				justify-content: space-between;
				color: #666666;
				font-size: 20rpx;
				margin-left: 100rpx;
				width: 100%;
				padding: 20rpx 0;
				border-top: 2rpx #F2F2F2 solid;
			}
		}
		
	}
</style>