<template>
  <div class="navbar" :style="{ backgroundColor: theme }">
    <!-- <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" /> -->
    <div style="width: 50%;padding: 0 30px;display: inline-block;">
      <logo />
    </div>
    <!-- <top-nav id="topmenu-container" class="topmenu-container" v-if="settingsStore.topNav" /> -->

    <div class="right-menu">
      <!-- <template> -->
        <div class="display-container">
          <el-dropdown @command="handleRouterCommand" class="display-menu hover-effect" trigger="click" @visible-change="displayDropdownVisible">
            <div class="display-wrapper" :style="{ color: theme ? '#fff' : '#000' }">
              <svg-icon icon-class="display-menu" class="display-icon" color="#39BA15" />
              <span class="display-btn">总揽监控大屏</span>
              <el-icon :class="{'is_expend_icon': displayVisible}" class="display-el-icon"><CaretBottom /></el-icon>
            </div>
            <template #dropdown>
              <el-dropdown-menu>
                <template v-for="item in displayRouter" :key="item.name">
                  <template v-if="!item.hidden">
                    <el-dropdown-item :command="item">
                      <span>{{ item.meta.title }}</span>
                    </el-dropdown-item>
                  </template>
                </template>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
        <screenfull id="screenfull" class="right-menu-item hover-effect" :color="theme ? '#fff' : '#000'" />
        <el-tooltip content="布局大小" effect="dark" placement="bottom">
          <size-select id="size-select" class="right-menu-item hover-effect" :color="theme ? '#fff' : '#000'" />
        </el-tooltip>
      <!-- </template> -->
      <div class="avatar-container">
        <el-dropdown @command="handleCommand" class="right-menu-item hover-effect" trigger="click" @visible-change="avatarDropdownVisible">
          <div class="avatar-wrapper" :style="{ color: theme ? '#fff' : '#000' }">
            <img :src="userStore.avatar" onerror="this.src='static/img/profile.png';this.onerror=null;" class="user-avatar" />
            <span class="user-name">{{ userStore.name }}</span>
            <el-icon :class="{'is_expend_icon': avatarVisible}"><CaretBottom /></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">
                <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 useAppStore from '@/store/modules/app'
import useUserStore from '@/store/modules/user'
import useSettingsStore from '@/store/modules/settings'
import Logo from '../components/Sidebar/Logo'
import usePermissionStore from '@/store/modules/permission'

// const appStore = useAppStore()
const userStore = useUserStore()
const settingsStore = useSettingsStore()
const permissionStore = usePermissionStore()
const theme = computed(() => useSettingsStore().theme);
const router = useRouter()

const displayVisible = ref(false)
const avatarVisible = ref(false)
// 所有的路由信息
const routers = computed(() => permissionStore.topbarRouters);
const displayRouter = computed(() => {
  let list = []
  routers.value.forEach(item => {
    if (item.path === '/dataDisplay') {
      list = item.children
    }
  })
  return list
})

// function toggleSideBar() {
//   appStore.toggleSideBar()
// }
const handleRouterCommand = (command) => {
  let url = router.resolve({path: command.path})
  window.open(url.href, '_blank')
}

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(() => {
      location.href = import.meta.env.VITE_APP_CONTEXT_PATH + 'index';
    })
  }).catch(() => { });
}

const emits = defineEmits(['setLayout'])
function setLayout() {
  emits('setLayout');
}
// 点击修改三角方向
const displayDropdownVisible = (val) => {
  displayVisible.value = val
}
const avatarDropdownVisible = (val) => {
  avatarVisible.value = val
}
</script>

<style lang='scss' scoped>
.navbar {
  height: 56px;
  overflow: hidden;
  position: relative;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  z-index: 1001;
  .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: 56px;
    display: flex;

    &:focus {
      outline: none;
    }
    .display-container{
      margin-right: 10px;
      .display-menu{
        height: 56px;
        .display-wrapper{
          margin: 17px 0;
          height:22px;
          line-height: 22px;
          padding-right: 12px;
          border-right: 1px solid rgba(255,255,255,0.4);
          .display-icon{
            vertical-align: middle;
            font-size: 18px;
          }
          .display-btn{
            display: inline-block;
            margin: 0 4px 0 8px;
          }
          .display-el-icon{
            font-size: 12px;
            vertical-align: middle;
            transition: all 0.25s;
          }
        }
      }
    }
    .right-menu-item {
      display: inline-block;
      padding: 0 8px;
      height: 100%;
      font-size: 14px;
      color: #fff;
      vertical-align: text-bottom;

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

        &:hover {
          background: rgba(0, 0, 0, 0.025);
        }
      }
    }

    .avatar-container {
      margin-right: 40px;

      .avatar-wrapper {
        // margin-top: 5px;
        position: relative;
        line-height: 56px;

        .user-avatar {
          cursor: pointer;
          width: 32px;
          height: 32px;
          display: inline-block;
          vertical-align: middle;
          border-radius: 20px;
        }
        i {
          cursor: pointer;
          position: absolute;
          right: -16px;
          top: 22px;
          font-size: 12px;
          transition: all 0.25s;
        }
        .user-name{
          display: inline-block;
          height: 56px;
          font-size: 14px;
          line-height: 56px;
          vertical-align: top;
          margin: 0 6px;
        }
      }
    }
  }
}
.is_expend_icon{
  transform: rotate(180deg);
}
</style>
