<template>
  <div class="navbar">
    <hamburger id="hamburger-container"
      :is-active="appStore.sidebar.opened"
      class="hamburger-container"
      @toggleClick="toggleSideBar" />
    <breadcrumb id="breadcrumb-container"
      class="breadcrumb-container"
      v-if="!settingsStore.topNav" />
    <top-nav id="topmenu-container"
      class="topmenu-container"
      v-if="settingsStore.topNav" />

    <div class="right-menu">
      <template v-if="appStore.device !== 'mobile'">
        <!-- 新版本 -->
        <el-tooltip content="版本说明"
          effect="dark"
          placement="bottom">
          <div class="right-menu-item hover-effect new-version"
            @click="showVersionFn">
            <svg-icon icon-class="new-version" />
          </div>
        </el-tooltip>

        <version-notice v-model="showVersionNoticeVisible"
          :nowVersion="nowVersion"
          :versionList="showVersionList"
          @confirm="saveNowVersion" />

        <el-tooltip content="帮助文档"
          effect="dark"
          placement="bottom">
          <div class="right-menu-item hover-effect question"
            @click="handleHelpClick">
            <svg-icon icon-class="question" />
          </div>
        </el-tooltip>

        <el-tooltip content="搜索菜单"
          effect="dark"
          placement="bottom">
          <header-search id="header-search"
            class="right-menu-item" />
        </el-tooltip>

        <el-tooltip content="全屏模式"
          effect="dark"
          placement="bottom">
          <screenfull id="screenfull"
            class="right-menu-item hover-effect" />
        </el-tooltip>

        <el-tooltip content="主题模式"
          effect="dark"
          placement="bottom">
          <div class="right-menu-item hover-effect theme-switch-wrapper"
            @click="toggleTheme">
            <svg-icon v-if="settingsStore.isDark"
              icon-class="sunny" />
            <svg-icon v-if="!settingsStore.isDark"
              icon-class="moon" />
          </div>
        </el-tooltip>

        <el-tooltip content="布局大小"
          effect="dark"
          placement="bottom">
          <size-select id="size-select"
            class="right-menu-item hover-effect" />
        </el-tooltip>
      </template>
      <div class="avatar-container">
        <el-dropdown @command="handleCommand"
          class="right-menu-item hover-effect"
          trigger="click">
          <div class="avatar-wrapper">
            <!-- <img :src="userStore.avatar" class="user-avatar" /> -->
            <span class="user-name">{{ name }}</span>
            <el-icon><caret-bottom /></el-icon>
          </div>
          <template #dropdown>
            <el-dropdown-menu>
              <router-link to="/user/profile">
                <el-dropdown-item>个人中心</el-dropdown-item>
              </router-link>
              <el-dropdown-item command="setLayout"
                v-if="settingsStore.showSettings">
                <span>布局设置</span>
              </el-dropdown-item>
              <el-dropdown-item divided
                command="logout"
                v-if="showQuit">
                <span>退出登录</span>
              </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ElMessageBox } from 'element-plus'
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 HeaderSearch from '@/components/HeaderSearch'
import useAppStore from '@/store/modules/app'
import useUserStore from '@/store/modules/user'
import useSettingsStore from '@/store/modules/settings'
import useVersionStore from '@/store/modules/version'

const appStore = useAppStore()
const userStore = useUserStore()
const settingsStore = useSettingsStore()
const versionStore = useVersionStore()
const { proxy } = getCurrentInstance()


const name = userStore.name;
const showQuit = !window.h5sdk; // 判断飞书客户端内，则不显示退出登录
const nowVersion = computed(() => versionStore.version)
const showVersionNoticeVisible = computed({
  get() {
    return versionStore.versionChange
  },
  set(val) {
    versionStore.versionChange = val
  }
}) // 判断版本通知是否出现
const showVersionList = computed(() => versionStore.versionList)

// 展示版本通知弹框
const showVersionFn = () => {
  handleVersionList()
}

// 处理版本通知数据结构
const handleVersionList = () => {
  showVersionNoticeVisible.value = true
}

// 保存当前版本
const saveNowVersion = () => {
  // 保存给 localStorage
  proxy.$cache.local.set('version', versionStore.version)
  // 关闭版本通知弹框
  showVersionNoticeVisible.value = false
}

// 帮助文档跳转打开
const handleHelpClick = () => {
  window.open('https://wangxiaolu.feishu.cn/docx/CHsWdujGroT3U8xwKmycnSWbnxh', '_blank')
}

function toggleSideBar() {
  appStore.toggleSideBar()
}

function handleCommand(command) {
  switch (command) {
    case "setLayout":
      setLayout();
      break;
    case "logout":
      logout();
      break;
    default:
      break;
  }
}

function logout() {
  ElMessageBox.confirm('确定注销并退出系统吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    userStore.logOut().then(() => {
      // 当前域名+路径
      const currentUrl = window.location.origin + window.location.pathname;
      location.href = currentUrl;
    })
  }).catch(() => { });
}

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

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

<style lang='scss'
  scoped>
  .navbar {
    height: 50px;
    overflow: hidden;
    position: relative;
    background: var(--navbar-bg);
    box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);

    .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;
    }

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

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

    .right-menu {
      float: right;
      height: 100%;
      line-height: 50px;
      display: flex;

      .new-version {
        font-size: 30px !important;
        padding-top: 2px !important;
      }

      .question {
        padding-top: 2px !important;

        svg {
          font-size: 22px;
        }
      }

      &:focus {
        outline: none;
      }

      .right-menu-item {
        display: inline-block;
        padding: 0 8px;
        height: 100%;
        font-size: 18px;
        color: var(--navbar-text);
        vertical-align: text-bottom;



        &.hover-effect {
          cursor: pointer;
          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: 40px;

        .el-dropdown {
          display: flex;
          align-items: center;

          .user-name {
            font-size: 16px;
          }

          .avatar-wrapper {
            position: relative;

            .user-avatar {
              cursor: pointer;
              width: 40px;
              height: 40px;
              border-radius: 10px;
            }

            i {
              cursor: pointer;
              position: absolute;
              right: -20px;
              top: 50%;
              transform: translate(-50%, 0);
              font-size: 12px;
            }
          }
        }
      }
    }
  }
</style>
