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

<script setup lang="ts">
import { useUserStore } from '@/stores/user'
const userStore = useUserStore()
const userInfo = userStore.user

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

<template>
  <header class="app-header">
    <div class="app-header-left">
      <div class="logo">
        <router-link to="/"><img src="https://zws-imgs-pub.ezijing.com/pc/base/ezijing-logo.svg" /></router-link>
      </div>
      <h1 class="app-name">产业学院</h1>
    </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/images/avatar.svg'" />
        </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>{{ userStore.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: 64px;
  background-color: #fff;
  border-bottom: 1px solid #e6e6e6;
  .logo {
    width: 120px;
  }
}
.app-header-left {
  display: flex;
  align-items: center;
  .app-name {
    margin-left: 20px;
    padding: 0 18px;
    font-size: 24px;
    font-weight: 400;
    line-height: 1;
    color: #333;
    border-left: 1px solid #707070;
  }
}

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