<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" />

        <!-- <el-tooltip content="源码地址" effect="dark" placement="bottom">
          <ruo-yi-git id="ruoyi-git" class="right-menu-item hover-effect" />
        </el-tooltip> -->

        <!-- <el-tooltip content="文档地址" effect="dark" placement="bottom">
          <ruo-yi-doc id="ruoyi-doc" class="right-menu-item hover-effect" />
        </el-tooltip> -->

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

        <el-tooltip content="GitCC 帮助与社区" effect="dark" placement="bottom">
          <span
            class="right-menu-item hover-effect brand-link"
            @click="openGitccCommunity"
            role="link"
            :aria-label="'安澜应急指挥产品帮助与 GitCC 社区，将打开 ' + communityUrl"
            tabindex="0"
            @keydown.enter="openGitccCommunity"
          >
            <i class="el-icon-share" aria-hidden="true" />
            <span class="brand-link-text">社区/帮助</span>
          </span>
        </el-tooltip>
        <el-tooltip content="切换明/暗外观（仅本机）" effect="dark" placement="bottom">
          <span
            class="right-menu-item hover-effect brand-link"
            role="button"
            :aria-pressed="themeDark"
            :aria-label="themeDark ? '切换为浅色' : '切换为深色'"
            tabindex="0"
            @click="toggleColorMode"
            @keydown.enter="toggleColorMode"
          >
            <i :class="themeDark ? 'el-icon-sunny' : 'el-icon-moon'" />
            <span class="brand-link-text">{{ themeDark ? '浅色' : '深色' }}</span>
          </span>
        </el-tooltip>
        <el-tooltip content="智能助手" effect="dark" placement="bottom">
          <span class="right-menu-item hover-effect brand-link" @click="openAssistant" role="button" tabindex="0" @keydown.enter="openAssistant">
            <i class="el-icon-chat-dot-round"></i>
            <span class="brand-link-text">智能助手</span>
          </span>
        </el-tooltip>

        <!-- <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="hover">
        <div class="avatar-wrapper">
          <img :src="avatar" class="user-avatar">
          <span class="user-nickname"> {{ nickName }} </span>
        </div>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item v-if="device==='mobile'" icon="el-icon-share" @click.native="openGitccCommunity">社区/帮助</el-dropdown-item>
          <el-dropdown-item
            v-if="device==='mobile'"
            :icon="themeDark ? 'el-icon-sunny' : 'el-icon-moon'"
            divided
            @click.native="toggleColorMode"
          >
            {{ themeDark ? '切换浅色' : '切换深色' }}
          </el-dropdown-item>
          <router-link to="/user/profile">
            <el-dropdown-item :divided="device==='mobile'">个人中心</el-dropdown-item>
          </router-link>
          <el-dropdown-item @click.native="setLayout" v-if="setting">
            <span>布局设置</span>
          </el-dropdown-item>
          <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'
// import RuoYiGit from '@/components/RuoYi/Git'
// import RuoYiDoc from '@/components/RuoYi/Doc'

const THEME_KEY = 'ailan-color-mode'
const communityUrl = 'https://www.gitcc.com/'

export default {
  emits: ['setLayout', 'openAssistant'],
  components: {
    Breadcrumb,
    TopNav,
    Hamburger,
    Screenfull,
    // SizeSelect,
    Search,
    // RuoYiGit,
    // RuoYiDoc
  },
  data() {
    return {
      communityUrl,
      themeDark: false
    }
  },
  computed: {
    ...mapGetters([
      'sidebar',
      'avatar',
      'device',
      'nickName'
    ]),
    setting: {
      get() {
        return this.$store.state.settings.showSettings
      }
    },
    topNav: {
      get() {
        return this.$store.state.settings.topNav
      }
    }
  },
  mounted() {
    this.applyStoredTheme()
  },
  methods: {
    applyStoredTheme() {
      let v = localStorage.getItem(THEME_KEY)
      if (v == null) {
        const legacy = localStorage.getItem('shuyu-color-mode')
        if (legacy) {
          v = legacy
          localStorage.setItem(THEME_KEY, legacy)
        }
      }
      this.themeDark = v === 'dark'
      if (this.themeDark) {
        document.documentElement.classList.add('theme-dark')
      } else {
        document.documentElement.classList.remove('theme-dark')
      }
    },
    toggleColorMode() {
      this.themeDark = !this.themeDark
      if (this.themeDark) {
        document.documentElement.classList.add('theme-dark')
        localStorage.setItem(THEME_KEY, 'dark')
      } else {
        document.documentElement.classList.remove('theme-dark')
        localStorage.setItem(THEME_KEY, 'light')
      }
    },
    openGitccCommunity() {
      window.open(communityUrl, '_blank', 'noopener,noreferrer')
    },
    openAssistant() {
      this.$emit('openAssistant')
    },
    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 {
  min-height: var(--app-navbar-h, 64px);
  height: var(--app-navbar-h, 64px);
  overflow: hidden;
  position: relative;
  z-index: 10;
  background: rgba(248, 250, 252, 0.82);
  -webkit-backdrop-filter: blur(12px);
  backdrop-filter: blur(12px);
  box-shadow: 0 1px 0 rgba(15, 23, 42, 0.06);
  border-bottom: 1px solid #e2e8f0;

  .hamburger-container {
    line-height: var(--app-navbar-h, 64px);
    height: 100%;
    float: left;
    cursor: pointer;
    transition: background .3s;
    -webkit-tap-highlight-color:transparent;

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

  .breadcrumb-container {
    float: left;
  }

  .topmenu-container {
    position: absolute;
    left: 50px;
  }

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

  .right-menu {
    float: right;
    height: 100%;
    line-height: var(--app-navbar-h, 64px);

    &:focus {
      outline: none;
    }

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

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

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

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

      .avatar-wrapper {
        margin-top: 10px;
        right: 8px;
        position: relative;

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

        .user-nickname{
          position: relative;
          bottom: 10px;
          left: 2px;
          font-size: 14px;
          font-weight: bold;
        }

        .el-icon-caret-bottom {
          cursor: pointer;
          position: absolute;
          right: -20px;
          top: 25px;
          font-size: 12px;
        }
      }
    }
  }

  .brand-link {
    display: inline-flex;
    align-items: center;
    padding: 0 10px !important;
  }
  .brand-link .brand-link-text {
    font-size: 14px;
    font-weight: 500;
    margin-left: 4px;
  }
}
</style>
