<template>
  <div class="navbar" :class="{'topnav-mode': topNav}">
    <!-- Logo 区域 -->
    <div class="navbar-left">
      <router-link to="/" class="logo-link">
        <img v-if="logImg" :src="logImg" class="logo-img" />
        <span class="logo-title">{{ title }}</span>
      </router-link>
    </div>

    <!-- 顶部一级菜单 (topNav模式) -->
    <top-nav v-if="topNav" class="topmenu-container" />

    <!-- 面包屑 (经典模式) -->
    <template v-else>
      <hamburger id="hamburger-container" :is-active="sidebar.opened" class="hamburger-container" @toggleClick="toggleSideBar" />
      <breadcrumb id="breadcrumb-container" class="breadcrumb-container" />
    </template>

    <!-- 右侧用户区域 -->
    <div class="right-menu">
      <template v-if="device!=='mobile'">
        <screenfull id="screenfull" class="right-menu-item hover-effect" />
      </template>

      <el-dropdown class="avatar-container right-menu-item hover-effect" trigger="click">
        <div class="avatar-wrapper">
          <div class="avatar-icon">
            <span>{{ userNameFirst }}</span>
          </div>
          <span class="avatar-name">{{ nickName }}</span>
          <i class="el-icon-caret-bottom" />
        </div>
        <el-dropdown-menu slot="dropdown">
          <router-link to="/user/profile">
            <el-dropdown-item>个人中心</el-dropdown-item>
          </router-link>
          <el-dropdown-item divided @click.native="logout">
            <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'

export default {
  components: {
    Breadcrumb,
    TopNav,
    Hamburger,
    Screenfull
  },
  computed: {
    ...mapGetters([
      'sidebar',
      'avatar',
      'device',
      'name'
    ]),
    topNav() {
      return this.$store.state.settings.topNav
    },
    title() {
      return process.env.VUE_APP_TITLE_INDEX || '云联融智'
    },
    nickName() {
      return this.$store.getters && this.$store.getters.nickName || '管理员'
    },
    userNameFirst() {
      const name = this.nickName
      return name ? name.charAt(0) : '管'
    },
    logImg() {
      return require(process.env.VUE_APP_LOG_URL)
    }
  },
  methods: {
    toggleSideBar() {
      this.$store.dispatch('app/toggleSideBar')
    },
    async logout() {
      this.$confirm('确定注销并退出系统吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$store.dispatch('LogOut').then(() => {
          location.href = '/index';
        })
      }).catch(() => {});
    }
  }
}
</script>

<style lang="scss" scoped>
.navbar {
  height: 56px;
  overflow: hidden;
  position: relative;
  background: #fff;
  border-bottom: 1px solid #f0f0f0;
  box-shadow: 0 1px 4px rgba(0,21,41,.04);
  display: flex;
  align-items: center;
  padding: 0 20px;

  .navbar-left {
    flex-shrink: 0;
    display: flex;
    align-items: center;

    .logo-link {
      display: flex;
      align-items: center;
      text-decoration: none;
      outline: none;

      .logo-img {
        width: 30px;
        height: 30px;
        margin-right: 10px;
      }

      .logo-title {
        font-size: 15px;
        font-weight: 600;
        color: #303133;
        white-space: nowrap;
      }
    }
  }

  .hamburger-container {
    line-height: 46px;
    height: 100%;
    float: left;
    cursor: pointer;
    transition: background .3s;

    &:hover {
      background: rgba(0, 0, 0, .025)
    }
  }

  .breadcrumb-container {
    float: left;
  }

  .topmenu-container {
    flex: 1;
    min-width: 0;
  }

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

    &:focus {
      outline: none;
    }

    .right-menu-item {
      display: inline-flex;
      align-items: center;
      padding: 0 8px;
      height: 100%;
      font-size: 18px;
      color: #5a5e66;

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

        &:hover {
          background: rgba(0, 0, 0, .025)
        }
      }
    }

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

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

        .avatar-icon {
          width: 30px;
          height: 30px;
          border-radius: 8px;
          background: linear-gradient(135deg, #4F46E5, #7C3AED);
          display: flex;
          align-items: center;
          justify-content: center;
          color: #fff;
          font-size: 13px;
          font-weight: 600;
        }

        .avatar-name {
          margin-left: 8px;
          font-size: 14px;
          color: #303133;
          max-width: 100px;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }

        .el-icon-caret-bottom {
          margin-left: 4px;
          font-size: 12px;
          color: #999;
        }
      }
    }
  }
}

/* topNav 模式下的菜单样式覆盖 */
.navbar.topnav-mode {
  ::v-deep .topmenu-container.el-menu--horizontal {
    border-bottom: none !important;
    height: 56px;
    display: flex;
    align-items: center;

    & > .el-menu-item {
      height: 56px !important;
      line-height: 56px !important;
      color: #555 !important;
      font-size: 14px;
      font-weight: 500;
      padding: 0 16px !important;
      margin: 0 4px !important;
      border-bottom: 2px solid transparent !important;

      &:hover {
        color: #303133 !important;
        background: transparent !important;
      }

      &.is-active {
        color: #4F46E5 !important;
        border-bottom: 2px solid #4F46E5 !important;
        background: transparent !important;
      }

      .svg-icon {
        margin-right: 6px;
        font-size: 16px;
      }
    }

    & > .el-submenu {
      .el-submenu__title {
        height: 56px !important;
        line-height: 56px !important;
        color: #555 !important;
        font-size: 14px;
        font-weight: 500;
        padding: 0 16px !important;
        margin: 0 4px !important;
        border-bottom: 2px solid transparent !important;

        &:hover {
          color: #303133 !important;
          background: transparent !important;
        }

        .svg-icon {
          margin-right: 6px;
          font-size: 16px;
        }
      }

      &.is-active .el-submenu__title {
        color: #4F46E5 !important;
        border-bottom: 2px solid #4F46E5 !important;
      }
    }
  }
}
</style>
