<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)}`
}
const SAAS_LAB_URL = import.meta.env.VITE_SAAS_LAB_URL
const SAAS_DML_URL = import.meta.env.VITE_SAAS_DML_URL
</script>

<template>
  <header class="app-header">
    <div class="app-header-left">
      <div class="logo">
        <router-link to="/"><img src="https://zws-imgs-pub.oss-cn-beijing.aliyuncs.com/pc/base/ezijing-logo-white.svg" /></router-link>
      </div>
      <h1 class="app-name">数字技能实训中心</h1>
    </div>
    <div class="app-header-center">
      <a href="/" class="is-active">理论课程</a>
      <!-- <a :href="SAAS_LAB_URL" target="_blank">商业数据分析</a> -->
      <a :href="SAAS_DML_URL" target="_blank">数字营销</a>
    </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: #ab0a3d;
  border-bottom: 1px solid #e6e6e6;
  .logo {
    width: 1.2rem;
  }
}
.app-header-left {
  display: flex;
  align-items: center;
  margin-right: 90px;
  .app-name {
    margin-left: 0.2rem;
    padding: 0 0.18rem;
    font-size: 0.18rem;
    font-weight: 400;
    line-height: 1;
    color: #fff;
    border-left: 1px solid #fff;
  }
}
.app-header-center {
  flex: 1;
  margin: 0 10px;
  display: flex;
  a {
    line-height: 64px;
    font-size: 20px;
    color: #fff;
    // margin-left: 90px;
    margin-right: 20px;
    padding: 0 34px;
    &.is-active {
      color: #fff;
      background-color: #c30b45;
    }
  }
}
.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;
}
.is-h5 {
  .app-header-center {
    display: none;
  }
}
</style>
