<template>
<div class="horizontal-navbar">
    <logo v-if="showLogo" :collapse="isCollapse" class="navbar-logo" />
    <el-scrollbar class="scrollbar-wrapper">
      <el-menu
        :default-active="activeMenu"
        mode="horizontal"
        :background-color="getMenuBackground"
        :text-color="getMenuTextColor"
        :active-text-color="'#fff'"
        :unique-opened="false"
      >
        <sidebar-item
          v-for="(route, index) in menuShowData"
          :key="route.id || route.path + index"
          :item="route"
          :base-path="route.path"
        />
      </el-menu>
    </el-scrollbar>

    <div class="right-menu">
      <!-- 这里可以放用户信息、设置按钮等 -->
       <header-search id="header-search" class="right-menu-item" />


        <el-dropdown style="margin-left: 20px;" @command="handleCommand" class="avatar-container right-menu-item hover-effect" trigger="hover">
          <div class="avatar-wrapper">
            <div class="user-avatar"  style="background-color: #69a6cb;"></div>
            <div class="user-nickname" style="position: absolute;text-align:center;width: 30px;"> {{ user.realname || '' }} </div>
          </div>
          <template #dropdown>
            <el-dropdown-menu>
              <!-- <router-link to="/user/profile">
                <el-dropdown-item>个人中心</el-dropdown-item>
              </router-link> -->
              <el-dropdown-item command="logout">
                <span>退出登录</span>
              </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
    </div>
  </div>
</template>

<script setup>
import { ElMessageBox } from 'element-plus'
import HeaderSearch from '@/components/HeaderSearch'
import Logo from './Logo'
import SidebarItem from './SidebarItem'
import variables from '@/assets/styles/variables.module.scss'
import useAppStore from '@/store/modules/app'
import useSettingsStore from '@/store/modules/settings'
import useUserStore from '@/store/modules/user'
import usePermissionStore from '@/store/modules/permission'
import { computed } from 'vue'

const route = useRoute()
const appStore = useAppStore()
const userStore = useUserStore()
const settingsStore = useSettingsStore()
const permissionStore = usePermissionStore()
// 转换接口数据为组件需要的格式
const processedRouters = computed(() => {
  return permissionStore.sidebarRouters
})
const user = JSON.parse(sessionStorage.getItem('userInfo')).user??{}
// 菜单类型获取
const navType = computed(() => {
  const navStatus = appStore.navStatus
  const type = navStatus.type === 'project' ? '2' : '1'
  return type
})

// 菜单显示数据
const menuShowData = computed(() => {
  const list = processedRouters.value.filter(item => {
    if (navType.value === '1') {
      if (navType.value === item.type || !item.type) {
        return true
      } else {
        return false
      }
    } else {
      if (navType.value === item.type) {
        return true
      } else {
        return false
      }
    }
  })
  return list
})

const showLogo = computed(() => settingsStore.sidebarLogo)
const sideTheme = computed(() => settingsStore.sideTheme)
const theme = computed(() => settingsStore.theme)
const isCollapse = computed(() => !appStore.sidebar.opened)

// 获取菜单背景色
const getMenuBackground = computed(() => {
  return 'rgba(33, 103, 217, 1)'
  // if (settingsStore.isDark) {
  //   return 'var(--sidebar-bg)'
  // }
  // return sideTheme.value === 'theme-dark' ? variables.menuBg : variables.menuLightBg
})

// 获取菜单文字颜色
const getMenuTextColor = computed(() => {
  if (settingsStore.isDark) {
    return 'var(--sidebar-text)'
  }
  return sideTheme.value === 'theme-dark' ? variables.menuText : variables.menuLightText
})

const activeMenu = computed(() => {
  const { meta, path } = route
  if (meta.activeMenu) {
    return meta.activeMenu
  }
  return path
})

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


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

function logout() {
  ElMessageBox.confirm('确定注销并退出系统吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    userStore.logOut().then(() => {
      // location.href = '/index'
      appStore.signOut('logout')
      appStore.setNavStatus({type: 'manage'})
      appStore.setQueryData({
        projectId: ''
      })
       window.location.replace(`${import.meta.env.BASE_URL}`)

    })
  }).catch(() => { })
}

</script>

<style lang="scss" scoped>
.horizontal-navbar {
  display: flex;
  align-items: center;
  height: var(--navbar-height);

  .navbar-logo {
    flex-shrink: 0;
    // width: vars.$base-sidebar-width;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  
  .scrollbar-wrapper {
    flex: 1;
    overflow: hidden;
    
    :deep(.el-scrollbar__view) {
      height: 100%;
      
    }
    
    .el-menu {
      border-bottom: none;
      height: 100%;
      
      &--horizontal {
        white-space: nowrap;
      }
    }
  }
  
  .right-menu {
    display: flex;
    flex-shrink: 0;
    padding-right: 15px;
  }
}

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

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

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

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

        i {
          cursor: pointer;
          position: absolute;
          right: -20px;
          top: 25px;
          font-size: 12px;
        }
      }
    }
</style>