<template>
  <div class="navbar">
    <hamburger
      :is-active="sidebar.opened"
      class="hamburger-container"
      @toggleClick="toggleSideBar"
    />

    <breadcrumb class="breadcrumb-container" />

    <div
      class="user_info"
      style="position: absolute; height: 100%; line-height: 50px; right: 110px;display: flex;align-items: center;"
    >
      <img src="https://img.pqblog.com/i/2024/03/10/142902.jpg" class="user-avatar" style="width: 40px;height: 40px;border-radius: 50%;" />
      <div style="margin-left: 15px;line-height: 50px;">{{userName}}</div>
    </div>

    <div class="right-menu">
      <div class="logout" @click="logout">
        <i class="el-icon-switch-button"></i>
      </div>
    </div>
  </div>
</template>

<script>
import { mapGetters, mapState } from "vuex";
import Breadcrumb from "@/components/Breadcrumb";
import Hamburger from "@/components/Hamburger";

export default {
  
  components: {
    Breadcrumb,
    Hamburger,
  },
  computed: {
    userName() {
      return window.localStorage.getItem("userName");
    },
    ...mapGetters(["sidebar", "avatar", "name"]),
  },
  methods: {
    toggleSideBar() {
      this.$store.dispatch("app/toggleSideBar");
    },
    logout() {
      this.$confirm('是否退出登录？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then( async () => {
           await this.$store.dispatch("user/logout");
           this.$router.push(`/login?redirect=${this.$route.fullPath}`);
        }).catch(() => {});
        return false
      // await this.$store.dispatch("user/logout");
      // this.$router.push(`/login?redirect=${this.$route.fullPath}`);
    },
  },
};
</script>

<style lang="scss" scoped>
.navbar {
  height: 50px;
  overflow: hidden;
  position: relative;
  background: #fff;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);

  .hamburger-container {
    line-height: 46px;
    height: 100%;
    float: left;
    cursor: pointer;
    transition: background 0.3s;
    -webkit-tap-highlight-color: transparent;

    &:hover {
      background: rgba(0, 0, 0, 0.025);
    }
  }

  .breadcrumb-container {
    float: left;
  }

  .right-menu {
    float: right;
    height: 100%;
    line-height: 50px;

    .logout {
      width: 80px;
      height: 50px;
      border-left: 2px solid #eee;
      box-sizing: border-box;
      font-size: 25px;
      text-align: center;
      cursor: pointer;
      &:hover {
        color: #5cb6ff;
      }
    }
    .user_info {
      .user-avatar {
        width: 40px;
        height: 40px;
        border-radius: 50%;
      }
    }

    .avatar-container {
      margin-right: 30px;

      .avatar-wrapper {
        margin-top: 5px;
        position: relative;

        .user-avatar {
          cursor: pointer;
          width: 40px;
          height: 40px;
          border-radius: 10px;

          
        }

        .el-icon-caret-bottom {
          cursor: pointer;
          position: absolute;
          right: -20px;
          top: 25px;
          font-size: 12px;
        }
      }
    }
  }
}
</style>
