<template>
	<header class="main-header" v-scroll-progress>
		<a href="/" class="logo"><img src="../assets/logo-full.png" alt="code-create" /></a>
		<div class="center-nav" v-show="windowWidth > 1000">
			<div class="nav-item" v-for="item in navList" :key="item.name">
				<down-menu :title="$t(item.name)" v-if="item.children">
					<router-link :to="child.path" v-for="child in item.children" :key="child.name">
						{{ $t(child.name) }}
					</router-link>
				</down-menu>
				<router-link class="link hover-bottom-animation" :to="item.path" v-else>{{
					$t(item.name)
				}}</router-link>
			</div>
		</div>
		<div class="right" v-show="windowWidth > 1000">
			<down-menu
				:title="langList.find((v) => v.value === locale)?.label || 'English'"
				position="right"
			>
				<router-link
					class="link"
					:to="`/${item.value}${path}`"
					v-for="item in langList"
					:key="item.value"
					v-show="item.value !== locale"
				>
					{{ item.label }}
				</router-link>
			</down-menu>
			<!-- <router-link class="link" to="/my-account">
				<span class="iconfont icon-tubiao-"></span>
				<span v-if="token">{{ $t('MainHeader.MyAccount') }}</span>
				<span v-else>{{ $t('MainHeader.LoginOrSignin') }}</span>
			</router-link> -->
		</div>
		<div class="right" v-show="windowWidth <= 1000">
			<span class="iconfont icon-caidan" @click="openMainMenu"></span>
		</div>
	</header>
	<main-menu-dialog
		ref="mainMenuDialogRef"
		:menu-list="navList"
		:lang-list="langList"
		:path="path"
	/>
</template>
<script setup lang="ts">
	import { ref, watch, computed, onMounted, onBeforeUnmount } from 'vue'
	import MainMenuDialog from './main-menu-dialog.vue'
	import DownMenu from './down-menu.vue'
	import { setLang, LangType } from '../lang'
	import { useI18n } from 'vue-i18n'
	const { locale } = useI18n()
	import { useRoute } from 'vue-router'
	import { useGlobalStore } from '@/stores/global'
	import { useUserInfoStore } from '@/stores/userInfo'
	const userInfoStore = useUserInfoStore()
	const globalStore = useGlobalStore()
	const windowWidth = computed(() => globalStore.state.windowWidth)
	const token = computed(() => userInfoStore.state.token)
	const route = useRoute()
	const lang = computed(() => route.params.lang)
	const path = computed(() => route.path.replace(new RegExp(`^/${lang.value}/`), '/'))
	if (lang.value) setLang(lang.value)
	watch(lang, (newVal) => {
		if (lang.value) setLang(newVal)
	})
	const langList = ref([
		{
			label: 'English',
			value: LangType.en
		},
		{
			label: '简体中文',
			value: LangType.zhCn
		},
		{
			label: '繁體中文',
			value: LangType.zhTw
		}
	])

	const navList: any = ref([
		{
			name: 'MainHeader.Home',
			path: '/'
		},
		{
			name: 'MainHeader.AboutUs',
			path: '/about-us'
		},
		{
			name: 'MainHeader.OurSolutions',
			path: '',
			children: [
				{
					name: 'MainHeader.AiDA',
					path: '/aida'
				},
				{
					name: 'MainHeader.Mixi',
					path: '/mixi'
				}
			]
		},
		{
			name: 'MainHeader.Communities',
			path: '',
			children: [
				{
					name: 'MainHeader.Events',
					path: '/events'
				},
				{
					name: 'MainHeader.UserStories',
					path: '/user-stories'
				},
				{
					name: 'MainHeader.HelpCentre',
					path: '/help-centre'
				}
			]
		},
		{
			name: 'MainHeader.Media',
			path: '/media'
		},
		{
			name: 'MainHeader.ContactUs',
			path: '/contact-us'
		}
	])

	const mainMenuDialogRef = ref()
	const openMainMenu = () => {
		mainMenuDialogRef.value.open()
	}
</script>
<style lang="less" scoped>
	.main-header {
		position: fixed;
		width: 100%;
		height: var(--main-header-height, 85px);
		padding: 15px 30px;
		box-sizing: border-box;
		top: 0;
		left: 0;
		z-index: 10000;
		transition: background-color 0.2s linear;
		&.active {
			background-color: #0a0a0a;
		}
		display: flex;
		justify-content: space-between;
		align-items: center;
		> .logo {
			height: 100%;
			width: auto;
			img {
				width: auto;
				height: 100%;
			}
		}
		> .center-nav {
			display: flex;
			align-items: center;
			justify-content: center;
			> .nav-item {
				position: relative;
				&:deep(> .down-menu > .title),
				> .link {
					margin: 0 14px;
					color: #fff;
					font-size: 14px;
					text-decoration: none;
					line-height: 37px;
					display: inline-block;
				}
				> .child {
					position: absolute;
					bottom: 0;
					visibility: hidden;
					width: 250px;
					height: auto;
					padding: 10px 0;
					box-sizing: border-box;
					border: 1px solid #e1e1e1;
					background-color: #fff;
					display: flex;
					flex-direction: column;
					transform: translateY(calc(100% + 5px));
					> .child-link {
						display: inline-block;
						padding: 10px 15px;
						color: #000;
						font-size: 15px;
						text-decoration: none;
						text-align: left;
						&:hover {
							opacity: 0.5;
						}
					}
				}
				&:hover > .child {
					animation: child-show 0.2s linear both;
				}
				@keyframes child-show {
					0% {
						visibility: hidden;
						// opacity: 0;
					}
					100% {
						// opacity: 1;
						transform: translateY(100%);
						visibility: visible;
					}
				}
			}
		}
		> .right {
			display: flex;
			align-items: center;
			justify-content: center;
			> .link {
				margin: 0 14px;
				color: #fff;
				font-size: 14px;
				text-decoration: none;
				line-height: 37px;
				display: inline-block;
				display: flex;
				align-items: center;
				justify-content: center;
				&:hover {
					opacity: 0.8;
				}
				> .iconfont {
					margin-right: 5px;
					font-size: 22px;
				}
			}
			> .icon-caidan {
				color: #fff;
				font-size: 24px;
				cursor: pointer;
			}
		}
		@media (max-width: 1000px) {
			padding: 7px 15px;
		}
	}
</style>
