<template>
	<view class="container">
		<view class="top">
			<!-- #ifdef APP -->
			<view class="iconfont icon-youjiantou zuojiantou" @click="goBack()"></view>
			<view class="commonTitle" style="width: 60%;text-align: right;">
				消息列表
			</view>
			<!-- #endif -->
			
			<view class="clear" @click="oneKeyRead()" >
				<text class="iconfont icon-saoba myIcon" ></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';
	import { initJssdkShare, setWechatShare } from '@/util/fiveshare';
	export default{
		data(){
			return{
				system_msgs_list:[],
			}
		},
		onLoad(){
			this.querySystemMessage();
		},
		onShow(){
			this.querySystemMessage();
			// #ifdef H5
						initJssdkShare(() => {
						  setWechatShare();
						}, window.location.href);
						// #endif
		},
		methods:{
			querySystemMessage() {
			   api.querySystemMessage({systemType:1,userId:uni.getStorageSync('cffp_userId')}).then(res => {
			     if (res['success']&&res['data']['dataList']&&res['data']['dataList'].length>0) {
			       this.system_msgs_list = res['data']['dataList'];
			     }else{
					 this.system_msgs_list = [];
				 }
			   });
			},
			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',
					url:'/pages/personalCenter/personalCenter'
				})
			},
			oneKeyRead(){
				if( this.system_msgs_list.length == 0){
					uni.showToast({
						title: '暂无未读消息',
						duration: 2000,
						icon: 'none'
					})
					return 
				}
				let that = this
				uni.showModal({
					// title: '确定将所有未读消息标为已读？',
					content:'确定将所有未读消息标为已读？',
					showCancel: true,
					success: function(res) {
						
						if (res.confirm) {
							api.oneKeyRead({systemType:1,userId:uni.getStorageSync('cffp_userId')}).then((res)=>{
								if(res['success']){
									that.querySystemMessage();
								}else{
									uni.showToast({
										title: res['message'],
										duration: 2000,
										icon: 'none'
									})
								}
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
				// 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" scoped>
	.top{
		display: flex;
		height: 100rpx;
		/* #ifdef APP */
		justify-content: space-between;
		/* #endif */
		/* #ifdef H5 */
		justify-content: flex-end;
		/* #endif */
		align-items: center;
		position: relative;
		background: #fff;
		padding-right: 20rpx;
		
		.clear{
			display: flex;
			align-items: center;
			flex-direction: column;
			.myIcon{
				color: #5359cd;
				font-size: 40rpx;
			}
			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: 30rpx;
					}
					.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: 26rpx;
				}
			}
			.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>