<template>
  <div class="navbar">
    <!-- 面包屑 -->
    <div class="left-menu">
      <div class="hamburger-container" @click="toggleSideBar">
        <i
          :class="[sidebar.opened ? 'el-icon-s-fold' : 'el-icon-s-unfold']"
        ></i>
      </div>
      <breadcrumb
        id="breadcrumb-container"
        class="breadcrumb-container"
        v-show="!isMobile || !sidebar.opened"
      />
    </div>

    <div class="right-menu">
      <screenfull />
      <el-dropdown
        class="avatar-container right-menu-item hover-effect"
        trigger="click"
      >
        <div class="avatar-wrapper">
          <img class="user-avatar" :src="avatar" alt="avatar" />
          <span style="font-size: medium">{{ name }}</span>
          <i class="el-icon-caret-bottom" />
        </div>
        <el-dropdown-menu slot="dropdown">
          <router-link to="/">
            <el-dropdown-item>首页</el-dropdown-item>
          </router-link>
          <!-- <router-link to="/auth/changePassword">
            <el-dropdown-item>{{ $t("changePass") }}</el-dropdown-item>
          </router-link> -->
          <el-dropdown-item divided>
            <span style="display: block" @click="logout">退出登录</span>
          </el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import Breadcrumb from "@/components/Breadcrumb";
import Screenfull from "@/components/Screenfull";

export default {
  props: {
    nameshow: {
      type: Boolean,
      default: false
    }
  },
  components: {
    Breadcrumb,
    Screenfull
  },
  computed: {
    ...mapGetters(["sidebar", "avatar", "name", "device"]),
    isMobile() {
      return this.device === "mobile";
    }
  },
  data() {
    return {
      helpswitch: true,
    };
  },
  methods: {
    clickopen() {
      this.$emit("helpcenterEvent", this.helpswitch);
    },
    toggleSideBar() {
      this.$store.dispatch("app/toggleSideBar");
    },
    async logout() {
      await this.$store.dispatch("user/logout");
      this.$router.push(`/login`);
    }
  }
};
</script>
<style type="text/css">
.el-breadcrumb__inner,
.el-breadcrumb__inner a {
  /*color: #fff;*/
  color: rgb(217, 142, 106);
}

.el-breadcrumb__inner,
.el-breadcrumb__inner a:hover {
  color: #f0f3f7;
}

.el-breadcrumb__separator {
  color: #000;
  color: rgb(217, 142, 106);
}

.app-breadcrumb.el-breadcrumb .no-redirect {
  /*color: #f0f3f7!important;*/
  color: #000;
  cursor: pointer !important;
}
</style>
<style lang="scss" scoped>
.navbar {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
  height: 54px;
  overflow: hidden;
  position: relative;
  background: #fff;
  -webkit-box-shadow: 0 2px 20px 0 rgba(15, 12, 70, 0.1);
  box-shadow: 0 2px 20px 0 rgba(15, 12, 70, 0.1);
  color: #000;

  .left-menu {
    display: flex;
    flex-direction: row;
    align-items: center;
    margin-left: 10px;
    flex: 1;
    overflow: hidden;

    @media screen and (max-width: 768px) {
      .breadcrumb-container {
        transition: opacity 0.3s;
      }
    }
  }

  .breadcrumb-container {
    float: left;
    margin-left: 10px;
  }

  .errLog-container {
    display: inline-block;
    vertical-align: top;
  }

  .right-menu {
    background: #fff;
    position: absolute; // 改为 absolute 定位，避免在小屏幕上的定位问题
    right: 20px;
    line-height: 54px;
    display: flex;
    flex-direction: row;
    align-items: center; // 添加垂直居中对齐

    @media screen and (max-width: 768px) {
      // 添加移动端适配
      right: 0;
      padding-right: 10px;

      .right-menu-item {
        padding: 0 5px; // 减小内边距
      }
      .screenfull-container {
        display: none !important; // 在移动端隐藏用户名
      }
      .avatar-container {
        margin-left: 0px !important;
        margin-right: 0px !important;

        .avatar-wrapper {
          .user-avatar {
            width: 30px; // 减小头像尺寸
            height: 30px;
          }

          .el-icon-caret-bottom {
            right: -10px !important;
            top: 14px !important;
          }

          span {
            display: none; // 在移动端隐藏用户名
          }
        }
      }
    }

    &:focus {
      outline: none;
    }

    .right-menu-item {
      display: inline-block;
      padding: 0 8px;
      height: 100%;
      font-size: 18px;
      color: #000;
      vertical-align: text-bottom;

      span {
        color: #000 !important;
      }

      &.hover-effect {
        cursor: pointer;
        transition: background 0.3s;

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

    .avatar-container {
      margin-right: 35px;
      margin-left: 10px;

      .avatar-wrapper {
        position: relative;
        height: 100%;
        display: flex;
        align-items: center;

        .user-avatar {
          cursor: pointer;
          width: 36px;
          height: 36px;
          border-radius: 50%;
          margin-right: 10px;
          border: 1px solid #ddd;
          object-fit: cover;
        }

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