<template>
	<nav class="site-navbar" :class="'site-navbar--' + navbarLayoutType">
		<div class="site-navbar__header">
			<h1 class="site-navbar__brand">
				<a class="site-navbar__brand-lg" href="javascript:;">{{entryName}}</a>
				<a class="site-navbar__brand-mini" href="javascript:;">
					<img src="~@/assets/img/avatar.png" style="width: 30px;height: 30px; border-radius: 50%;">
				</a>
			</h1>
		</div>
		<div class="site-navbar__body clearfix">
			<el-menu class="site-navbar__menu" mode="horizontal">
				<el-menu-item class="site-navbar__switch" index="0" @click="sidebarFold = !sidebarFold">
					<icon-svg name="zhedie"></icon-svg>
				</el-menu-item>
			</el-menu>
			<el-menu class="site-navbar__menu site-navbar__menu--right" mode="horizontal">
				<el-menu-item index="1" style="position: relative;" @click="$router.push({ name: 'vueMchat' })">
					<template slot="title">
						<!-- <span class="left-number" v-if="chatNum">{{chatNum}}</span> -->
						<!-- <icon-svg name="tixing" class="el-icon-setting" style="font-size: 28px;"></icon-svg> -->
						<!-- <audio autoplay="autoplay" src="" ref='audio' controls hidden></audio> -->
						<audio id="audio" autoplay="autoplay"
							src="https://pw.xianmxkj.com/file/uploadPath/2022/01/19/0753211f78d718d44ee6372e33eae9ee.mp3"
							ref='audio' controls hidden></audio>
					</template>
				</el-menu-item>
				<el-menu-item class="site-navbar__avatar" index="3">
					<el-dropdown :show-timeout="0" placement="bottom">
						<span class="el-dropdown-link">
							<span style="margin-right: 10px;">{{ username ? username : '' }} <span
									v-if="username">,欢迎您登录</span></span>
							<img src="~@/assets/img/avatar.png" :alt="userName">
						</span>
						<el-dropdown-menu slot="dropdown">
							<!-- <el-dropdown-item @click.native="updatePasswordHandle()">修改密码</el-dropdown-item> -->
							<el-dropdown-item @click.native="logoutHandle()">退出</el-dropdown-item>
						</el-dropdown-menu>
					</el-dropdown>
				</el-menu-item>
			</el-menu>
		</div>

		<!-- 弹窗, 修改密码 -->
		<update-password v-if="updatePassowrdVisible" ref="updatePassowrd"></update-password>
	</nav>
</template>

<script>
	import UpdatePassword from './main-navbar-update-password'
	import {
		clearLoginInfo
	} from '@/utils'
	import {
		constants
	} from 'fs'
	import { entryName } from '@/utils/httpRequest'
	export default {
		data() {
			return {
				updatePassowrdVisible: false,
				username: '',
				intervalId: 0,
				storeId: 0,
				isshow: false,
				chatNum: '',
				chatNum1: '',
				chatNum2: '',
				errCount: 0,
				errCount1: 0,
				errCount2: 0,
				chatNum3: 0,
				chatNum4: 0,
				chatNum5: 0,
				chatNumJl: 0,
				chatNumQy: 0,
				chatNumYh: 0,
				chatNumJzts: 0,
				chatNumDzh: 0,
				yhQuanxian: false,
				jlQuanxian: false,
				qyQuanxian: false,
				ppQuanxian: false,
				jzxmQuanxian: false,
				entryName:'',
			}
		},
		components: {
			UpdatePassword
		},
		computed: {
			navbarLayoutType: {
				get() {
					return this.$store.state.common.navbarLayoutType
				}
			},
			sidebarFold: {
				get() {
					return this.$store.state.common.sidebarFold
				},
				set(val) {
					this.$store.commit('common/updateSidebarFold', val)
				}
			},
			mainTabs: {
				get() {
					return this.$store.state.common.mainTabs
				},
				set(val) {
					this.$store.commit('common/updateMainTabs', val)
				}
			},
			userName: {
				get() {
					return this.$store.state.user.name
				}
			},
		},
		methods: {
			// 修改密码
			updatePasswordHandle() {
				this.updatePassowrdVisible = true
				this.$nextTick(() => {
					this.$refs.updatePassowrd.init()
				})
			},
			// 退出
			logoutHandle() {
				this.$confirm(`确定进行[退出]操作?`, '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					this.$cookie.set('token', '')
					this.$cookie.set('userId', '')
					sessionStorage.clear()
					//router.options.isAddDynamicMenuRoutes = false;
					this.$router.push({
						name: 'login'
					})

				}).catch(() => {})
			},

			// 获取用户信息
			userSelect() {
				let userId = this.$cookie.get('userId')
				this.$http({
					url: this.$http.adornUrl(`sys/user/info`),
					method: 'get',
					params: this.$http.adornParams({})
				}).then(({
					data
				}) => {
					let returnData = data.user;
					// if (returnData == null) {
					//   this.$cookie.set('token', '')
					//   this.$cookie.set('userId', '')
					//   sessionStorage.clear()
					//   this.$notify({
					//     title: '提示',
					//     duration: 1800,
					//     message: '用户信息失效，退出登录',
					//     type: 'warning'
					//   });
					//   this.$router.push({
					//     path: '/login'
					//   });
					// } else {
					this.username = returnData.username
					// }
				})
			},
			// 获取聊天消息
			chatSelect() {
				this.$http({
					url: this.$http.adornUrl('message/selectMessageCount'),
					method: 'get',
					params: this.$http.adornParams({
						'state': 10
					})
				}).then(({
					data
				}) => {
					let returnData = data.data;
					this.chatNum = returnData
					if (this.chatNum == 0) {
						this.isshow = false
						this.errCount = 0
					} else {
						if (this.errCount != data.data) {
							this.errCount = data.data
							this.aplayAudio()
							// this.chatAudio();
							// let audio = new Audio()
							// audio.src = require('../assets/img/notify.mp3')
							// audio.play();
							//如果不是当前页面，标题栏闪动+消息提示
							if (document.hidden) {
								var options = {
									body: '您有新的未读消息，请及时处理',
									silent: true
								}
								var notification = new Notification('消息通知', options);
								notification.onclick = function() {
									window.open(window.location.href, '_blank');
								}
								//标题栏闪动
								var defaultTitle = document.title;
								if (that.isReceive) {
									return;
								} else {
									that.isReceive = true;
								}
								that.timer = setInterval(function() {
									var title = document.title;
									if (document.hidden && that.isReceive) {
										if (/你有新消息/.test(title) == false) {
											document.title = '【你有新消息】';
										} else {
											document.title = defaultTitle;
										}
									} else {
										that.isReceive = false;
										document.title = defaultTitle;
									}
								}, 500);
							}
							// this.isshow = true

						} else {
							this.errCount = data.data
						}
					}
				})
			},
			//申请浏览器通知权限，具体参见上面文档链接
			notification() {
				let that = this;
				if (!("Notification" in window)) {
					console.log("浏览器不支持消息通知");
					return;
				}
				Notification.requestPermission(function(permission) {});
			},
			// 获取待审核入职
			chatSelect2() {
				var _this = this
				_this.$http({
					url: _this.$http.adornUrl('message/selectMessageCount'),
					method: 'get',
					params: _this.$http.adornParams({
						'state': 10
					})
				}).then(({
					data
				}) => {
					let returnData = data.data;
					if (data.data > 0) {
						_this.aplayAudio()
						if (_this.yhQuanxian == true) {
							_this.$notify.info({
								title: '消息',
								dangerouslyUseHTMLString: true,
								message: '您有' + data.data + '条待审核入职。',
								offset: 80,
								duration: 0,
								type: "warning",
								onClick() {
									_this.defineCallBack2()
								},
								onClose() {
									// _this.defineCallBack2()
								}
							});
						}

					}


				})
			},
			//  待审核简历
			messageSelect() {
				var _this = this
				_this.$http({
					url: _this.$http.adornUrl('message/selectMessageCount'),
					method: 'get',
					params: _this.$http.adornParams({
						'state': 30
					})
				}).then(({
					data
				}) => {
					let returnData = data.data;
					if (data.data > 0) {
						_this.chatNum1 = data.data
						// if(_this.chatNum1==_this.errCount1){

						// }else{
						_this.aplayAudio()
						_this.errCount1 = data.data
						if (_this.jlQuanxian == true) {
							_this.$notify.info({
								title: '消息',
								dangerouslyUseHTMLString: true,
								message: '您有' + data.data + '条待审核简历。',
								offset: 80,
								duration: 0,
								type: "warning",
								onClick() {
									_this.defineCallBack1()
								},
								onClose() {
									// _this.defineCallBack1()
								}
							});
						}

						// }



					}
				})
			},
			//  待审核企业
			messageSelect2() {
				var _this = this
				_this.$http({
					url: _this.$http.adornUrl('message/selectMessageCount'),
					method: 'get',
					params: _this.$http.adornParams({
						'state': 20
					})
				}).then(({
					data
				}) => {
					let returnData = data.data;
					if (data.data > 0) {
						_this.chatNum2 = data.data
						// if(_this.chatNum2==_this.errCount2){

						// }else{
						_this.aplayAudio()
						_this.errCount2 = data.data
						if (_this.qyQuanxian == true) {
							_this.$notify.info({
								title: '消息',
								dangerouslyUseHTMLString: true,
								message: '您有' + data.data + '条待审核企业。',
								offset: 80,
								duration: 0,
								type: "warning",
								onClick() {
									_this.defineCallBack()
								},
								onClose() {
									// _this.defineCallBack()
								}
							});
						}

						// }
					}
				})
			},
			
			defineCallBack() {
				this.$router.replace({
					name: 'autonym',
					query: {
						status: 1
					}
				})
			},
			defineCallBack1() {
				this.$router.replace({
					name: 'carBrandList',
					query: {
						status: 1
					}
				})
			},
			defineCallBack4() {
				this.$router.replace({
					name: 'universalMission',
					query: {
						status: 1
					}
				})
			},
			defineCallBack3() {
				this.$router.replace({
					name: 'locality',
					query: {
						status: 1
					}
				})
			},
			defineCallBack2() {
				this.$router.replace({
					name: 'userList',
					query: {
						userResumesStatus: 1
					}
				})
			},
			defineCallBack5() {
				console.log('跳转人才库··')
				this.$router.replace({
					name: 'carBrandList',
					query: {
						isRecommend: 1
					}
				})
			},
			aplayAudio() {
				const audio = document.getElementById('audio')
				audio.play()
				console.log('语音提示')
			},
			// 获取简历未读数据
			dataSelect() {
				this.$http({
					url: this.$http.adornUrl('resumes/selectResumesList'),
					method: 'get',
					params: this.$http.adornParams({
						'page': 1,
						'limit': 10,
						'status': 1,
						'resumesName': '',
						'resumesPhone': '',
						'rulePostName': '',
						'resumesPost': '',
						'resumesWorkExperience': '',
						'resumesCompensation': '',
						'resumesMarriage': '',
						'school': '',
						'resumesAge': '',
						'isRecommend': ''
					})
				}).then(({
					data
				}) => {
					console.log('获取简历未读数据')
					if (data.data) {
						this.$cookie.set('numberJl', data.data.totalCount)
					}

				})
			},
			// 获取招聘待审核数据
			dataSelectPP() {
				this.$http({
					url: this.$http.adornUrl('postPush/getAdminPostPushList'),
					method: 'get',
					params: this.$http.adornParams({
						'page': 1,
						'limit': 10,
						'status': 1
					})
				}).then(({
					data
				}) => {
					if (data.data) {
						this.$cookie.set('numberPP', data.data.total)
					}
				})
			},
			// 获取企业待审核数据
			dataSelectPy() {
				this.$http({
					url: this.$http.adornUrl('company/selectCompanyList'),
					method: 'get',
					params: this.$http.adornParams({
						'page': 1,
						'limit': 10,
						'status': 1,
						'userName': '',
						'companyPhone': '',
						'companyName': ''
					})
				}).then(({
					data
				}) => {
					if (data.data) {
						this.$cookie.set('numberQy', data.data.total)
					}
				})
			},
			// 获取用户待审核数据
			dataSelectYh() {
				this.$http({
					url: this.$http.adornUrl('user/selectUserList'),
					method: 'get',
					params: this.$http.adornParams({
						'page': 1,
						'limit': 10,
						'member': '',
						'phone': '',
						'userName': '',
						'isAuthentication': 0,
						'userResumesStatus': 3,
						'weekMember': '',
						'resumes': ''
					})
				}).then(({
					data
				}) => {
					if (data.data) {
						this.$cookie.set('numberYh', data.data.totalCount)
					}
				})
			},
			// 获取精准推送待审核数据
			dataSelectJzts() {
				this.$http({
					url: this.$http.adornUrl('postPush/selectPostPushList'),
					method: 'get',
					params: this.$http.adornParams({
						'page': 1,
						'limit': 10,
						'userName': '',
						'classify': 2,
						'status': 1,
						'companyName': '',
						'phone': '',
						'projectName': '',
						'projectAward': '',
						'address': '',
					})
				}).then(({
					data
				}) => {
					if (data.data) {
						this.$cookie.set('numberJzts', data.data.total)
					}
				})
			},
			// 获取精准推送待审核数据
			dataSelectDzh() {
				this.$http({
					url: this.$http.adornUrl('postPush/selectPostPushList'),
					method: 'get',
					params: this.$http.adornParams({
						'page': 1,
						'limit': 10,
						'userName': '',
						'classify': 1,
						'status': 1,
						'companyName': '',
						'phone': '',
					})
				}).then(({
					data
				}) => {
					if (data.data) {
						this.$cookie.set('numberDzh', data.data.total)
					}
				})
			},
		},
		mounted() {
			this.entryName = entryName
			this.userSelect()
			this.chatSelect()
			this.chatSelect2()
			this.notification();
			// this.messageSelect()
			this.messageSelect2()
			this.dataSelect()
			this.dataSelectPy()
			this.dataSelectPP()
			this.dataSelectYh()
			// this.dataSelectJzts()
			// this.dataSelectDzh()
			var permissions = sessionStorage.getItem('permissions')
			console.log('permissions', )
			var arrPermissions = JSON.parse(permissions)
			for (var i in arrPermissions) {
				if (arrPermissions[i] == 'userList:pass') {
					this.yhQuanxian = true
				}
				if (arrPermissions[i] == 'carBrandList:tongguo') {
					this.jlQuanxian = true
					console.log('this.jlQuanxian', this.jlQuanxian)
				}
				if (arrPermissions[i] == 'autonym:tongguo') {
					this.qyQuanxian = true
				}
				if (arrPermissions[i] == 'locality:shenhe') {
					this.jzxmQuanxian = true
				}
				if (arrPermissions[i] == 'recruitListS:update') {
					this.ppQuanxian = true
				}

			}

		},
		created() {

			this.chatNum = setInterval(() => {
				// this.chatSelect()
				if (this.yhQuanxian == true) {
					this.chatSelect2()
				}
			}, 6000)


			// this.chatNum1 = setInterval(() => {
			// 	if (this.jlQuanxian == true) {
			// 		this.messageSelect()
			// 	}
			// }, 6000)


			this.chatNum2 = setInterval(() => {
				if (this.qyQuanxian == true) {
					this.messageSelect2()
				}
			}, 6000)


			this.chatNumJl = setInterval(() => {
				this.dataSelect()
			}, 6000)
			this.chatNumQy = setInterval(() => {
				this.dataSelectPy()
			}, 6000)
			this.chatNumYh = setInterval(() => {
				this.dataSelectYh()
			}, 6000)
			this.chatNumPP = setInterval(() => {
				this.dataSelectPP()
			}, 6000)
			// this.chatNumJzts = setInterval(() => {
			// 	this.dataSelectJzts()
			// }, 6000)
			// this.chatNumDzh = setInterval(() => {
			// 	this.dataSelectDzh()
			// }, 6000)

		},
		beforeDestroy() {
			clearInterval(this.chatNum)
		}
	}
</script>
<style scoped="scoped">
	.site-navbar__avatar .el-dropdown-link>img {
		height: 36px;
	}

	.left-number {
		position: absolute;
		top: 0;
		right: 20px;
		width: 18px;
		height: 18px;
		line-height: 18px;
		border-radius: 50%;
		background: #ff4949;
		color: #ffffff;
		text-align: center;
		font-size: 10px;
		font-family: PingFang SC, PingFang SC-Regular;
		font-weight: 400;
		margin-top: 5px;
	}
</style>
