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

    <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'">
        <search id="header-search" class="right-menu-item" />
        <screenfull id="screenfull" class="right-menu-item hover-effect" />
        <el-tooltip content="布局大小" effect="dark" placement="bottom">
          <size-select id="size-select" class="right-menu-item hover-effect" />
        </el-tooltip>
      </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'
import SizeSelect from '@/components/SizeSelect'
import Search from '@/components/HeaderSearch'

export default {
  components: {
    Breadcrumb,
    TopNav,
    Hamburger,
    Screenfull,
    SizeSelect,
    Search
  },
  computed: {
    ...mapGetters(['sidebar', 'avatar', 'device']),
    topNav() {
      return this.$store.state.settings.topNav
    },
    title() {
      return process.env.VUE_APP_TITLE_INDEX || '云联融智SAAS'
    },
    nickName() {
      return (this.$store.getters && this.$store.getters.nickName) || '管理员'
    },
    userNameFirst() {
      const name = this.nickName
      return name ? name.charAt(0) : '管'
    },
    logImg() {
      try {
        return require(process.env.VUE_APP_LOG_URL)
      } catch (e) {
        return ''
      }
    }
  },
  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>
@import '~@/assets/styles/admin-menu.scss';

.navbar {
  height: $admin-top-height;
  overflow: hidden;
  position: relative;
  background: #fff;
  border-bottom: 1px solid $admin-border;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.08);
  display: flex;
  align-items: center;
  padding: 0 20px 0 0;

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

    .logo-link {
      display: flex;
      align-items: center;
      gap: 10px;
      text-decoration: none;
      outline: none;
      margin-right: 8px;

      .logo-img {
        width: 32px;
        height: 32px;
        object-fit: contain;
      }

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

    .nav-divider {
      width: 1px;
      height: 24px;
      background: $admin-border;
      margin-right: 8px;
      flex-shrink: 0;
    }
  }

  .hamburger-container {
    line-height: $admin-top-height;
    height: 100%;
    cursor: pointer;
    transition: background 0.3s;

    &:hover {
      background: rgba(0, 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;
    margin-left: auto;
    padding-left: 20px;

    .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 0.3s;

        &:hover {
          background: rgba(64, 158, 255, 0.08);
        }
      }
    }

    .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, #409eff, #1890ff);
          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: #909399;
        }
      }
    }
  }
}

.navbar.topnav-mode {
  ::v-deep .topmenu-container.el-menu--horizontal {
    border-bottom: none !important;
    height: $admin-top-height;
    display: flex;
    align-items: center;
    background: transparent !important;

    & > .el-menu-item {
      height: $admin-top-height !important;
      line-height: $admin-top-height !important;
      color: $admin-top-text !important;
      font-size: 14px;
      font-weight: 500;
      padding: 0 18px !important;
      margin: 0 !important;
      border-bottom: 2px solid transparent !important;
      background: transparent !important;

      &:hover {
        color: $admin-top-active !important;
        background: transparent !important;
      }

      &.is-active {
        color: $admin-top-active !important;
        border-bottom-color: $admin-top-active !important;
        background: transparent !important;
      }

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

    & > .el-submenu {
      .el-submenu__title {
        height: $admin-top-height !important;
        line-height: $admin-top-height !important;
        color: $admin-top-text !important;
        font-size: 14px;
        font-weight: 500;
        padding: 0 18px !important;
        margin: 0 !important;
        border-bottom: 2px solid transparent !important;
        background: transparent !important;

        &:hover {
          color: $admin-top-active !important;
          background: transparent !important;
        }

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

      &.is-active .el-submenu__title {
        color: $admin-top-active !important;
        border-bottom-color: $admin-top-active !important;
      }
    }
  }
}
</style>
