<template>
	<div class="base-notification">
		<Poptip :title="$t('utils.newNotice')" placement="bottom">
			<Badge :count="msgArr.length">
				<Icon type="md-notifications" />
			</Badge>
			<div slot="content" class="notice-wrap">
				<div v-if="!msgArr.length" class="notice-empty">
					<Icon type="md-text" />
					{{ $t('notice.noData') }}
				</div>
				<div class="notice-item" v-for="(item,index) in msgArr" :key="index" @click="doClickMsg(item,index)">
					<p class="item-name">{{ getMsgType(item) }}</p>
					<p class="item-content">{{ getMsgContent(item) }}</p>
					<!-- <span class="item-time">{{ item.expires }}</span> -->
				</div>
			</div>
		</Poptip>
	</div>
</template>

<script>
	export default {
		props: {
			msgs: {
				type: Array,
				default: () => []
			}
		},
		data(vm) {
			return {
				msgArr: [],
				msgTypes: {
					'request_school': vm.$t('notice.type1'),
					'invite_school': vm.$t('notice.type2'),
					'remove_school':vm.$t('notice.type3'),
					'request-join_school':vm.$t('notice.type1'),
					'invite-join_school':vm.$t('notice.type2'),
					'coedit_syllabus':vm.$t('notice.type4'),
					'share_syllabus':vm.$t('notice.type4')
				}
			}
		},
		created() {

		},
		methods: {
			getBodyJson(str) {
				let reg = /\\/g;
				//使用replace方法将全部匹配正则表达式的转义符替换为空
				let replaceAfter = str.replace(reg, '').replace(/=/g, ':');
				return replaceAfter
			},
			getMsgType(msg) {
				return this.msgTypes[msg.label]
			},
			getMsgContent(msg) {
				let body = JSON.parse(this.getBodyJson(msg.body))
				console.log(body)
				switch (msg.label) {
					case 'request_school':
						return `${body.tmdname}(${body.tmdid}) ${this.$t('notice.tip1')} ${body.schoolname}`
						break;
					case 'invite_school':
						return `${body.tmdname}(${body.tmdid}) ${this.$t('notice.tip2')} ${body.schoolname}`
						break;
					case 'request-join_school':
						return `${body.schoolname} ${this.$t('notice.tip3')}`
						break;
					case 'invite-join_school':
						return `${body.tmdname}(${body.tmdid}) ${this.$t('notice.tip4')} ${body.schoolname} ${this.$t('notice.tip5')}`
						break;
					case 'remove_school':
						return `${body.schoolname} ${this.$t('notice.tip6')}`
						break;
					case 'coedit_syllabus':
						return `${body.tmdname} ${this.$t('notice.tip7')}`
						break;
					case 'share_syllabus':
						return `${body.tmdname} ${this.$t('notice.tip8')}`
						break;		
					default:
						break;
				}
			},
			doClickMsg(msg,index) {
				console.log(msg)
				switch (msg.label) {
					case 'request_school':
						this.$router.push({
							name: "teachermgmt",
						})
						break;
					case 'invite-join_school':
						this.$router.push({
							name: "teachermgmt",
						})
						break;	
					case 'invite_school':
						this.$router.push({
							name: "settings",
						})
						break;
					case 'coedit_syllabus':
						this.$router.push({
							name: "syllabus",
						})
						break;
					case 'share_syllabus':
						this.$router.push({
							name: "personalSyllabus",
							tabName:'fromShare'
						})
						break;		
					default:
						break;
				}
				this.msgArr.splice(index,1)
			}
		},
		watch: {
			msgs: {
				handler(n, o) {
					if(!n.length){
						this.msgArr = []
					}else{
						// 只查询当前站点的消息通知
						let curLocation = localStorage.getItem('location')
						this.msgArr = n.filter(i => {
							return JSON.parse(this.getBodyJson(i.body)).location === curLocation
						})
						console.log(curLocation)
						console.log(this.msgArr)
					}
					
				}
			}
		}
	}
</script>

<style lang="less">
	.base-notification {
		// font-family: 'NotoSerif', '微软正黑体', 'Microsoft JhengHei UI', 'Microsoft JhengHei', Sans-serif;

		.ivu-icon {
			font-size: 22px;
			color: #d0d0d0;
			cursor: pointer;
		}

		.ivu-poptip-popper {
			padding: 0;
			top: 65px !important;
			z-index: 9999;
			width: 450px !important;
			
			.ivu-poptip-body{
				max-height: 320px;
				overflow: auto;
			}
		}

		.ivu-badge-count {
			width: 16px;
			height: 16px;
			display: inline-block;
			border-radius: 50%;
			color: white;
			border: none;
			font-size: 12px;
			line-height: 16px;
			text-align: center;
			margin-left: 10px;
			min-width: 16px;
			box-shadow: none;
			padding: 0;
		}
		
		.notice-empty{
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			margin: 20px 0;
			color: #b3b3b3;
			
			.ivu-icon{
				font-size: 44px;
				margin-bottom: 20px;
				color: #6c6c6c;
			}
		}

		.notice-wrap {
			display: flex;
			flex-direction: column;

			.notice-item {
				position: relative;
				padding: 5px;
				color: #d0d0d0;
				border-bottom: 1px solid #424242;
				cursor: pointer;

				&:last-child {
					border: none;
				}

				.item-name {
					font-size: 16px;
					font-weight: bold;
					color: #fff;
					margin: 10px 0;
				}
				
				.item-content{
					word-break: break-all;
					white-space: break-spaces;
				}

				.item-time {
					margin-top: 5px;
					font-size: 12px;
					color: #777777;
					float: right;
				}
			}
		}
	}
</style>
