<template>
  <div class="navbar">
    <!-- <hamburger id="hamburger-container" :is-active="appStore.sidebar.opened" class="hamburger-container"
      @toggleClick="toggleSideBar" /> -->
    <breadcrumb v-if="!settingsStore.topNav" id="breadcrumb-container" class="breadcrumb-container" />
    <div class="navContent">
      <div class="systemLogoContent">
        <img v-if="homeLogo" :src="homeLogo" class="sidebar-logo" />
      </div>
      <div class="topmenu-container">
        <top-nav v-if="settingsStore.topNav" id="topmenu-container" />
      </div>
      <div class="right-menu">
        <div class="el-dropdown avatar-container right-menu-item hover-effect">
          <div class="avatar-wrapper">
            <img :src="userStore.avatar ? userStore.avatar : avatar_icon" class="user-avatar" />
            <span class="user-nickname"> {{ userStore.nickName }} admin </span>
            <div class="logout_icon">
              <img :src="logout_icon" class="custom-icon" @click="logout" />
            </div>
          </div>
        </div> 
        <div class="right-menu-item hover-effect setting" @click="setLayout" v-if="settingsStore.showSettings">
          <svg-icon icon-class="more-up" />
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ElMessageBox } from 'element-plus'
import Breadcrumb from '@/components/Breadcrumb'
import homeLogo from '@/assets/logo/homeLogo.png'
import avatar_icon from '@/assets/images/avatar_icon.png'
import logout_icon from '@/assets/images/logout_icon.png'
import TopNav from '@/components/TopNav'
import useAppStore from '@/store/modules/app'
import useUserStore from '@/store/modules/user'
import useSettingsStore from '@/store/modules/settings'

const appStore = useAppStore()
const userStore = useUserStore()
const settingsStore = useSettingsStore()

console.log('settingsStore.topNav', settingsStore.topNav)

function toggleSideBar() {
  appStore.toggleSideBar()
}

// 退出系统
const logout = () => {
  ElMessageBox.confirm('确定注销并退出系统吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    closeOnClickModal: false, // 禁止点击遮罩层关闭
    type: 'warning'
  }).then(() => {
    userStore.logOut().then(() => {
      location.href = '/index'
    })
  }).catch(() => { })
}

const emits = defineEmits(['setLayout'])
function setLayout() {
  emits('setLayout')
}

function toggleTheme() {
  settingsStore.toggleTheme()
}
</script>

<style lang='scss' scoped>
.myRow {
  margin-left: 0 !important;
  margin-right: 0 !important;
}

.navbar {
  height: 80px;
  overflow: hidden;
  position: relative;
  background: transparent;
  box-shadow: none;

  .navContent {
    height: 43px;
    width: 100%;
    margin: 19px 40px 18px 40px;
    display: flex; // 添加flex布局
    justify-content: space-between; // 使子元素平均分布
    align-items: center; // 垂直居中
  }

  .systemLogoContent {
    width: 220px; 
  }

  .topmenu-container { 
    flex: 1; // 占据剩余空间
    padding-left: 284px; // 替代原来的padding-left 
  }

  .right-menu {
    height: 100%;
    width: 220px; 
    text-align: right;
    line-height: normal; // 覆盖原来的line-height
    display: flex; // 内部也使用flex
    align-items: center; // 垂直居中
    justify-content: flex-end; // 内容靠右

    &:focus {
      outline: none;
    }

    .right-menu-item { 
      height: 100%;
      font-size: 18px;
      color: #5a5e66;
      vertical-align: text-bottom;

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

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

      &.theme-switch-wrapper {
        display: flex;
        align-items: center;

        svg {
          transition: transform 0.3s;

          &:hover {
            transform: scale(1.15);
          }
        }
      }
    }

    .avatar-container {
      margin-right: 0px;
      padding-right: 0px;

      .avatar-wrapper { 
        display: flex;
        align-items: center;

        .user-avatar {
          width: 30px;
          height: 30px;
          border-radius: 50%;
        }

        .user-nickname { 
          bottom: 0 !important; // 移除原来的bottom定位
          margin-left: 8px;
          font-family: Microsoft YaHei;
          font-weight: 400;
          font-size: 18px;
          color: #FFFFFF;
        }

        i {
          cursor: pointer;
          position: absolute;
          right: -20px;
          top: 25px;
          font-size: 12px;
        }
      }
    }
  }

  .logout_icon {  
    margin-left: 30px;
    cursor: pointer;
  }
}
  
.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;
}

.errLog-container {
  display: inline-block;
  vertical-align: top;
}
</style>
