<script lang="ts">
export default { name: 'AppHeader' }
</script>

<script setup lang="ts">
import type { IMenuItem } from '@/types'
import { CaretBottom } from '@element-plus/icons-vue'
import { useUserStore } from '@/stores/user'
import { useMenuStore } from '@/stores/menu'
import { useAppConfig } from '@/composables/useAppConfig'
const appConfig = useAppConfig()
const route = useRoute()
const router = useRouter()

const userStore = useUserStore()
const userInfo = userStore.user

const menuStore = useMenuStore()
const menus = $computed(() => {
  // 大赛系统隐藏学生端、教师端的菜单
  if (appConfig.system === 'game' && userStore.role?.id !== 6) {
    return []
  } else {
    return menuStore.menus
  }
})

const defaultActive = computed(() => {
  // 扁平菜单
  const flatMenuList: IMenuItem[] = menus.reduce((result: IMenuItem[], item) => {
    result.push(item)
    if (item.children) {
      result = result.concat(item.children)
    }
    return result
  }, [])
  const found = flatMenuList.reverse().find(item => {
    return route.path.includes(item.path)
  })
  return found ? found.path : '/'
})
const logout = async () => {
  await userStore.logout()
  location.href = `${appConfig.loginURL || import.meta.env.VITE_LOGIN_URL}?rd=${encodeURIComponent(location.origin)}`
}

function isUrl(path: string) {
  return /^https?:\/\//.test(path)
}

function handleClick(path: string) {
  if (isUrl(path)) {
    window.open(path)
  } else {
    router.push(path)
  }
}
</script>

<template>
  <header class="app-header">
    <div class="app-header-left">
      <template v-if="appConfig.logo">
        <div class="logo">
          <router-link to="/"><img :src="appConfig.logo" /></router-link>
        </div>
        <div class="line"></div>
      </template>
      <h1 class="app-name">
        <router-link to="/">{{ appConfig.title }}</router-link>
      </h1>
    </div>
    <div class="app-header-nav">
      <el-menu mode="horizontal" router :default-active="defaultActive">
        <template v-for="item in menus" :key="item.path">
          <el-sub-menu :index="item.path" :popper-offset="0" popper-class="sub-menu-popper" v-permission="item.tag" v-if="item.children">
            <template #title>
              {{ item.name }}
            </template>
            <el-menu-item
              :index="subitem.path"
              v-for="subitem in item.children"
              :key="subitem.path"
              v-permission="subitem.tag"
              @click="handleClick(subitem.path)">
              {{ subitem.name }}
            </el-menu-item>
          </el-sub-menu>
          <el-menu-item :index="item.path" v-permission="item.tag" @click="handleClick(item.path)" v-else>
            {{ item.name }}
          </el-menu-item>
        </template>
      </el-menu>
    </div>
    <div class="app-header-right">
      <el-dropdown v-if="userInfo">
        <div class="user">
          <div class="avatar">
            <img :src="userInfo.avatar || 'https://webapp-pub.ezijing.com/website/base/images/avatar.svg'" />
          </div>
          <p>{{ userInfo.name }}</p>
          <el-icon><CaretBottom /></el-icon>
        </div>
        <template #dropdown>
          <el-dropdown-menu style="width: 280px">
            <div class="app-header-user">
              <div class="app-header-user-avatar">
                <img :src="userInfo.avatar || 'https://webapp-pub.ezijing.com/website/base/images/avatar.svg'" />
              </div>
              <div class="app-header-user-main">
                <h3>{{ userInfo.name }}</h3>
                <p>{{ userInfo.email || userInfo.mobile }}</p>
              </div>
              <div class="app-header-user-buttons">
                <el-button round @click="logout">退出登录</el-button>
              </div>
            </div>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
  </header>
</template>

<style lang="scss">
.app-header {
  position: sticky;
  top: 0;
  z-index: 2001;
  padding: 0 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 66px;
  background-color: #fff;
  .logo {
    width: 145px;
  }
}
.app-header-left {
  display: flex;
  align-items: center;
  .line {
    margin: 0 20px;
    height: 30px;
    border-left: 2px solid var(--main-color);
  }
  .app-name {
    font-size: 28px;
    font-family: Source Han Sans CN;
    color: #333;
    font-weight: 500;
    line-height: 30px;
    white-space: nowrap;
  }
}
.app-header-nav {
  margin: 0 40px;
  flex: 1;
  height: 100%;
  --el-menu-item-height: 63px;
  --el-menu-item-font-size: 16px;
  .el-menu--horizontal {
    font-weight: 500;
    border-bottom: 0;
    > .el-menu-item {
      margin-left: 40px;
      padding: 0;
      background-color: unset !important;
      border-bottom: 0 !important;
    }
  }
  .el-sub-menu__title {
    margin-left: 40px;
    padding: 0;
    border-bottom: 0 !important;
  }
  .el-sub-menu__icon-arrow {
    display: none;
  }
}
.sub-menu-popper {
  border: 0 !important;
  border-radius: 0;
  border-top: 3px solid var(--main-color) !important;
  .sub-menu-popper {
    border-top: 0 !important;
  }
  .el-menu-item {
    padding: 0 25px !important;
  }
  .el-menu--popup {
    min-width: 160px;
    padding: 18px 0;
  }
}

.app-header-right {
  display: flex;
  .user {
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    color: #666;
    border-radius: 21px;
    padding: 4px;
    p {
      margin: 0 10px;
    }
    &:hover {
      background-color: rgba(219, 219, 219, 0.23);
      box-shadow: 2px 1px 8px 1px #e4e8eb;
    }
  }
  .avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    overflow: hidden;
    img {
      width: 100%;
      height: 100%;
      object-fit: cover;
      border-radius: 50%;
      overflow: hidden;
    }
    &:hover {
      background-color: rgba(60, 64, 67, 0.08);
    }
  }
}
.app-header-user {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  padding: 16px;
}
.app-header-user-avatar {
  margin-bottom: 6px;
  width: 80px;
  height: 80px;
  border-radius: 50%;
  overflow: hidden;
  img {
    width: 100%;
    height: 100%;
    object-fit: cover;
  }
}
.app-header-user-main {
  h3 {
    color: #202124;
    font: 500 16px/22px Helvetica, Arial, sans-serif;
    letter-spacing: 0.29px;
    margin: 0;
    text-align: center;
    text-overflow: ellipsis;
    overflow: hidden;
  }
  p {
    color: #5f6368;
    font: 400 14px/19px Helvetica, Arial, sans-serif;
    letter-spacing: normal;
    text-align: center;
    text-overflow: ellipsis;
    overflow: hidden;
  }
}
.app-header-user-buttons {
  padding-top: 16px;
}
</style>
