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

<script setup lang="ts">
import { menus } from '@/assets/menus'
import { useUserStore } from '@/stores/user'
import type { IMenuItem } from '@/types'
withDefaults(defineProps<{ hasTitle?: boolean }>(), {
  hasTitle: true
})

const route = useRoute()

const userStore = useUserStore()

const userInfo = userStore.user

const logout = async () => {
  await userStore.logout()
  location.href = `${import.meta.env.VITE_LOGIN_URL}?rd=${encodeURIComponent(location.href)}`
}

function genNavClassName(data: IMenuItem) {
  const reg = new RegExp(`^${data.path}`)
  return reg.test(route.fullPath) ? 'is-active' : ''
}
</script>

<template>
  <header class="app-header">
    <div class="app-header-left">
      <div class="logo">
        <router-link to="/"><img src="https://webapp-pub.ezijing.com/website/base/logo_white.svg" /></router-link>
      </div>
      <h1 class="app-name">统一资源管理平台</h1>
    </div>
    <div class="app-header-nav">
      <div
        class="app-header-nav-item"
        v-for="(item, index) in menus"
        :key="index"
        :class="genNavClassName(item)"
        v-permission="item.tag">
        <router-link :to="item.path">{{ item.name }}</router-link>
      </div>
    </div>
    <div class="app-header-right">
      <el-dropdown v-if="userInfo">
        <div class="avatar">
          <img :src="userInfo.avatar || 'https://webapp-pub.ezijing.com/website/base/avatar.png'" />
        </div>
        <template #dropdown>
          <el-dropdown-menu style="width: 280px">
            <div class="app-header-user">
              <div class="app-header-user-avatar">
                <img :src="userInfo.avatar || '/center_resource/avatar.png'" />
              </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: 70px;
  background-color: var(--main-color);
  color: #fff;
  .logo {
    width: 120px;
  }
}
.app-header-left {
  display: flex;
  align-items: center;
  .app-name {
    margin-left: 20px;
    padding: 0 15px;
    line-height: 1;
    border-left: 2px solid #fff;
    font-size: 18px;
  }
}
.app-header-nav {
  flex: 1;
  display: flex;
  align-items: center;
  // justify-content: center;
}
.app-header-nav-item {
  height: 45px;
  a {
    display: inline-block;
    font-size: 16px;
    line-height: 45px;
    padding: 0 18px;
  }
  &:hover,
  &.is-active {
    background: rgba(144, 6, 44, 0.39);
    color: #fff;
  }
}

.app-header-right {
  display: flex;

  .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>
