<template>
  <header class="app-header">
    <div class="logo">
      <router-link to="/"><img src="https://webapp-pub.ezijing.com/highway/admin/logo.png" /></router-link>
    </div>
    <div class="app-header-right">
      <el-dropdown>
        <div class="avatar">
          <img :src="user.avatar || 'https://zws-imgs-pub.ezijing.com/pc/base/logo.png'" />
        </div>
        <el-dropdown-menu slot="dropdown">
          <div class="user">
            <div class="user-name">{{ user.realname || user.nickname }}</div>
            <el-button round size="medium" class="btn-logout" @click="logout">退出登录</el-button>
          </div>
        </el-dropdown-menu>
      </el-dropdown>
    </div>
  </header>
</template>

<script>
export default {
  name: 'AppHeader',
  computed: {
    user() {
      return this.$store.state.user
    }
  },
  methods: {
    logout() {
      this.$store.dispatch('logout').then(() => {
        window.location.href = `${import.meta.env.VITE_LOGIN_URL}?rd=${encodeURIComponent(window.location.href)}`
      })
    }
  }
}
</script>

<style lang="scss">
.app-header {
  padding: 0 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 64px;
  background-color: #fff;
  border-bottom: 4px solid var(--main-color);
  color: #5F5F5F;

  .logo {
    // width: 120px;
  }
}
.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;
    }
  }
}
.user {
  padding: 16px;
}
.user-name {
  margin-bottom: 20px;
}
.btn-logout {
  width: 200px;
}
</style>
