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

    <breadcrumb v-if="!topNav" id="breadcrumb-container" class="breadcrumb-container" />
    <top-nav v-if="topNav" id="topmenu-container" class="topmenu-container" />

    <div class="right-menu">
      <template v-if="device!=='mobile'">
        <search id="header-search" class="right-menu-item" />

        <screenfull id="screenfull" class="right-menu-item hover-effect" />

        <el-tooltip content="布局设置" effect="dark" placement="bottom">
          <div class="right-menu-item hover-effect" @click="setLayout" v-if="setting">
            <i class="el-icon-setting"></i>
          </div>
        </el-tooltip>

      </template>

      <el-dropdown class="avatar-container right-menu-item hover-effect" trigger="hover">
        <div class="avatar-wrapper">
          <img :src="avatar" class="user-avatar">
          <span class="user-nickname"> {{ nickName }} </span>
        </div>
        <el-dropdown-menu slot="dropdown">
          <router-link to="/user/profile">
            <el-dropdown-item>
              <i class="el-icon-user"></i>
              <span>个人中心</span>
            </el-dropdown-item>
          </router-link>
          <el-dropdown-item @click.native="setLayout" v-if="setting">
            <i class="el-icon-setting"></i>
            <span>布局设置</span>
          </el-dropdown-item>
          <el-dropdown-item divided @click.native="logout">
            <i class="el-icon-switch-button"></i>
            <span>退出登录</span>
          </el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import Breadcrumb from '@/components/Breadcrumb'
import TopNav from '@/components/TopNav'
import Hamburger from '@/components/Hamburger'
import Screenfull from '@/components/Screenfull'
import Search from '@/components/HeaderSearch'

export default {
  emits: ['setLayout'],
  components: {
    Breadcrumb,
    TopNav,
    Hamburger,
    Screenfull,
    Search
  },
  computed: {
    ...mapGetters([
      'sidebar',
      'avatar',
      'device',
      'nickName'
    ]),
    setting: {
      get() {
        return this.$store.state.settings.showSettings
      }
    },
    topNav: {
      get() {
        return this.$store.state.settings.topNav
      }
    }
  },
  methods: {
    toggleSideBar() {
      this.$store.dispatch('app/toggleSideBar')
    },
    setLayout(event) {
      this.$emit('setLayout')
    },
    logout() {
      this.$confirm('确定注销并退出系统吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$store.dispatch('LogOut').then(() => {
          location.href = '/index'
        })
      }).catch(() => {})
    }
  }
}
</script>

<style lang="scss" scoped>
.navbar {
  height: 60px;
  overflow: hidden;
  position: relative;
  background: #ffffff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  z-index: 9;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0;

  .hamburger-container {
    line-height: 60px;
    height: 100%;
    cursor: pointer;
    transition: all .3s;
    -webkit-tap-highlight-color: transparent;
    padding: 0 15px;
    flex-shrink: 0;

    &:hover {
      background: linear-gradient(135deg, rgba(102, 126, 234, 0.08) 0%, rgba(118, 75, 162, 0.08) 100%);
    }
  }

  .breadcrumb-container {
    flex: 1;
    line-height: 60px;
    padding-left: 10px;
    overflow: hidden;
  }

  .topmenu-container {
    flex: 1;
    padding: 0 20px;
  }

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

  .right-menu {
    height: 100%;
    line-height: 60px;
    display: flex;
    align-items: center;
    flex-shrink: 0;

    &:focus {
      outline: none;
    }

    .right-menu-item {
      display: inline-flex;
      align-items: center;
      justify-content: center;
      padding: 0 12px;
      height: 40px;
      font-size: 18px;
      color: #606266;
      vertical-align: middle;
      border-radius: 8px;
      margin: 0 4px;

      &.hover-effect {
        cursor: pointer;
        transition: all .3s;

        &:hover {
          background: linear-gradient(135deg, rgba(102, 126, 234, 0.1) 0%, rgba(118, 75, 162, 0.1) 100%);
          color: #667eea;
          transform: translateY(-2px);
        }
      }
    }

    .avatar-container {
      margin-right: 15px;
      padding: 0 12px;
      height: 40px;
      border-radius: 20px;
      transition: all 0.3s;

      &:hover {
        background: linear-gradient(135deg, rgba(102, 126, 234, 0.1) 0%, rgba(118, 75, 162, 0.1) 100%);
      }

      .avatar-wrapper {
        display: flex;
        align-items: center;
        height: 40px;
        position: relative;

        .user-avatar {
          cursor: pointer;
          width: 36px;
          height: 36px;
          border-radius: 50%;
          border: 2px solid #e8e8e8;
          transition: all 0.3s;

          &:hover {
            border-color: #667eea;
            box-shadow: 0 0 8px rgba(102, 126, 234, 0.4);
          }
        }

        .user-nickname {
          margin-left: 10px;
          font-size: 14px;
          font-weight: 600;
          color: #333;
          max-width: 120px;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }

        .el-icon-caret-bottom {
          cursor: pointer;
          margin-left: 8px;
          font-size: 14px;
          color: #909399;
          transition: all 0.3s;
        }
      }

      &:hover .el-icon-caret-bottom {
        color: #667eea;
      }
    }
  }
}

// 优化下拉菜单样式
::v-deep .el-dropdown-menu {
  padding: 8px 0;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  border: 1px solid #e4e7ed;
  margin-top: 8px;
  
  .el-dropdown-menu__item {
    padding: 10px 20px;
    font-size: 14px;
    line-height: 1.5;
    
    i {
      margin-right: 8px;
      font-size: 16px;
      color: #909399;
    }
    
    &:hover {
      background: linear-gradient(135deg, rgba(102, 126, 234, 0.1) 0%, rgba(118, 75, 162, 0.1) 100%);
      color: #667eea;
      
      i {
        color: #667eea;
      }
    }
  }
  
  a {
    text-decoration: none;
    
    .el-dropdown-menu__item {
      color: inherit;
    }
  }
}
</style>
