<template>
	<div class="base-notification">
		<Poptip :title="$t('utils.newNotice')" class="dark-iview-poptip">
			<Badge :count="msgArr.length">
				<Icon type="md-notifications" />
			</Badge>
			<div slot="content" class="notice-wrap">
				<div class="notice-item" v-for="(item,index) in msgArr" :key="index" @click="doClickMsg(item)">
					<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() {
			return {
				msgArr: [],
				msgTypes: {
					'request_school': '申请通知',
					'invite_school': '邀请通知',
				}
			}
		},
		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}) 申请加入 ${body.schoolname}`
						break;
					case 'invite_school':
						return `${body.tmdname}(${body.tmdid}) 邀请您加入 ${body.schoolname}`
						break;
					case 'request-join_school':
						return `${body.schoolname} 已同意您的加入请求`
						break;
					case 'invite-join_school':
						return `${body.tmdname}(${body.tmdid}) 已接受您的加入 ${body.schoolname} 的邀请`
						break;
					case 'remove_school':
						return `${body.schoolname} 已将您移除`
						break;
					default:
						break;
				}
			},
			doClickMsg(msg) {
				switch (msg.label) {
					case 'request_school':
						this.$router.push('teachermgmt')
						break;
					case 'invite_school':
						this.$router.push('settings')
						break;
					default:
						break;
				}
			}
		},
		watch: {
			msgs: {
				handler(n, o) {
					this.msgArr = n
					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;
		}

		.dark-iview-poptip .ivu-poptip-popper {
			padding: 0;
			top: 40px !important;
			z-index: 9999;
		}

		.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-wrap {
			display: flex;
			flex-direction: column;

			.notice-item {
				position: relative;
				padding: 10px;
				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-time {
					margin-top: 5px;
					font-size: 12px;
					color: #777777;
					float: right;
				}
			}
		}
	}
</style>
