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

    <div class="right-menu flex align-center">
      <template v-if="appStore.device !== 'mobile'">
        <!-- <el-select
          v-if="userId === 1 && tenantEnabled"
          v-model="companyName"
          class="min-w-244px"
          clearable
          filterable
          reserve-keyword
          :placeholder="$t('navbar.selectTenant')"
          @change="dynamicTenantEvent"
          @clear="dynamicClearEvent"
        >
          <el-option v-for="item in tenantList" :key="item.tenantId" :label="item.companyName" :value="item.tenantId"> </el-option>
          <template #prefix><svg-icon icon-class="company" class="el-input__icon input-icon" /></template>
        </el-select> -->

        <!-- <header-search id="header-search" class="right-menu-item" /> -->
        <search-menu ref="searchMenuRef" />
        <el-tooltip content="搜索" effect="dark" placement="bottom">
          <div class="right-menu-item hover-effect" @click="openSearchMenu">
            <svg-icon class-name="search-icon" icon-class="search" />
          </div>
        </el-tooltip>
        <!-- 消息 -->
        <el-tooltip :content="$t('navbar.message')" effect="dark" placement="bottom">
          <div>
            <el-popover placement="bottom" trigger="click" transition="el-zoom-in-top" :width="300" :persistent="false">
              <template #reference>
                <el-badge :value="newNotice > 0 ? newNotice : ''" :max="99">
                  <svg-icon icon-class="message" />
                </el-badge>
              </template>
              <template #default>
                <notice></notice>
              </template>
            </el-popover>
          </div>
        </el-tooltip>
        <!-- <el-tooltip content="Github" effect="dark" placement="bottom">
          <ruo-yi-git id="ruoyi-git" class="right-menu-item hover-effect" />
        </el-tooltip>

        <el-tooltip :content="$t('navbar.document')" effect="dark" placement="bottom">
          <ruo-yi-doc id="ruoyi-doc" class="right-menu-item hover-effect" />
        </el-tooltip> -->

        <el-tooltip :content="$t('navbar.full')" effect="dark" placement="bottom">
          <screenfull id="screenfull" class="right-menu-item hover-effect" />
        </el-tooltip>

        <el-tooltip :content="$t('navbar.language')" effect="dark" placement="bottom">
          <lang-select id="lang-select" class="right-menu-item hover-effect" />
        </el-tooltip>

        <el-tooltip :content="$t('navbar.layoutSize')" effect="dark" placement="bottom">
          <size-select id="size-select" class="right-menu-item hover-effect" />
        </el-tooltip>
      </template>
      <div class="avatar-container">
        <el-dropdown class="right-menu-item hover-effect" trigger="click" @command="handleCommand">
          <div class="avatar-wrapper">
            <img :src="userStore.avatar" class="user-avatar" />
            <el-icon><caret-bottom /></el-icon>
          </div>
          <template #dropdown>
            <el-dropdown-menu>
              <router-link v-if="!dynamic" to="/user/profile">
                <el-dropdown-item>{{ $t('navbar.personalCenter') }}</el-dropdown-item>
              </router-link>
              <el-dropdown-item v-if="settingsStore.showSettings" command="setLayout">
                <span>{{ $t('navbar.layoutSetting') }}</span>
              </el-dropdown-item>
              <el-dropdown-item divided command="logout">
                <span>{{ $t('navbar.logout') }}</span>
              </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import SearchMenu from './TopBar/search.vue';
import useAppStore from '@/store/modules/app';
import {useUserStore} from '@/store/modules/user';
import useSettingsStore from '@/store/modules/settings';
import useNoticeStore from '@/store/modules/notice';
import { getTenantList } from '@/api/login';
import { dynamicClear, dynamicTenant } from '@/api/system/tenant';
import { TenantVO } from '@/api/types';
import notice from './notice/index.vue';

const appStore = useAppStore();
const userStore = useUserStore();
const settingsStore = useSettingsStore();
const noticeStore = storeToRefs(useNoticeStore());
const newNotice = ref(<number>0);

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const userId = ref(userStore.userId);
const companyName = ref(undefined);
const tenantList = ref<TenantVO[]>([]);
// 是否切换了租户
const dynamic = ref(false);
// 租户开关
const tenantEnabled = ref(true);
// 搜索菜单
const searchMenuRef = ref<InstanceType<typeof SearchMenu>>();

const openSearchMenu = () => {
  searchMenuRef.value?.openSearch();
};

// 动态切换
const dynamicTenantEvent = async (tenantId: string) => {
  if (companyName.value != null && companyName.value !== '') {
    await dynamicTenant(tenantId);
    dynamic.value = true;
    proxy?.$tab.closeAllPage();
    proxy?.$router.push('/');
    proxy?.$tab.refreshPage();
  }
};

const dynamicClearEvent = async () => {
  await dynamicClear();
  dynamic.value = false;
  proxy?.$tab.closeAllPage();
  proxy?.$router.push('/');
  proxy?.$tab.refreshPage();
};

/** 租户列表 */
const initTenantList = async () => {
  const { data } = await getTenantList();
  tenantEnabled.value = data.tenantEnabled === undefined ? true : data.tenantEnabled;
  if (tenantEnabled.value) {
    tenantList.value = data.voList;
  }
};

defineExpose({
  initTenantList
});

const toggleSideBar = () => {
  appStore.toggleSideBar(false);
};


const logout = async () => {
  await ElMessageBox.confirm('确定注销并退出系统吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  });
  await userStore.logout();
  location.href = import.meta.env.VITE_APP_CONTEXT_PATH + 'index';
};

const emits = defineEmits(['setLayout']);
const setLayout = () => {
  emits('setLayout');
};
// 定义Command方法对象 通过key直接调用方法
const commandMap: { [key: string]: any } = {
  setLayout,
  logout
};
const handleCommand = (command: string) => {
  // 判断是否存在该方法
  if (commandMap[command]) {
    commandMap[command]();
  }
};
//用深度监听 消息
watch(
  () => noticeStore.state.value.notices,
  (newVal) => {
    newNotice.value = newVal.filter((item: any) => !item.read).length;
  },
  { deep: true }
);
</script>

<style lang="scss" scoped>
:deep(.el-select .el-input__wrapper) {
  height: 30px;
  border-radius: 6px;
}

:deep(.el-badge__content.is-fixed) {
  top: 12px;
  right: 4px;
  height: 18px;
  line-height: 18px;
  padding: 0 6px;
  border-radius: 9px;
  font-size: 12px;
  font-weight: bold;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.flex {
  display: flex;
}

.align-center {
  align-items: center;
}

.navbar {
  height: 54px;
  overflow: hidden;
  position: relative;
  /* 统一使用现代化蓝色配色方案 */
  background: var(--navbarBg, #1E40AF);
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.15);
  border-bottom: none;
  transition: all 0.3s ease;

  .hamburger-container {
    display: flex; /* 添加这行 */
    align-items: center; /* 添加这行 */
    justify-content: center; /* 添加这行 */
    line-height: 1; /* 修改这行 */
    height: 100%;
    float: left;
    cursor: pointer;
    transition: background 0.3s;
    padding: 0 16px;
    -webkit-tap-highlight-color: transparent;
    color: var(--navbarColor, #1E3A5F); /* 使用柔和的颜色 */

    &:hover {
      background: rgba(30, 64, 175, 0.1);
    }
  }

  .breadcrumb-container {
    float: left;
    margin-left: 10px;
    display: flex; /* 添加这行 */
    align-items: center; /* 添加这行 */
    height: 100%; /* 添加这行 */
    line-height: 1; /* 修改这行 */

    /* 柔和配色下增强面包屑导航可见性 */
    :deep(.el-breadcrumb__inner) {
      color: var(--navbarColor, #1E3A5F) !important;
      font-weight: 500 !important;
    }

    /* 分隔符样式 */
    :deep(.el-breadcrumb__separator) {
      color: var(--navbarColor, #1E3A5F) !important;
      opacity: 0.7;
    }

    /* 最后一项特殊样式 */
    :deep(.el-breadcrumb__item:last-child .el-breadcrumb__inner) {
      font-weight: 600 !important;
    }
  }

  .topmenu-container {
    position: absolute;
    left: 50px;
    display: flex; /* 添加这行 */
    align-items: center; /* 添加这行 */
    height: 100%; /* 添加这行 */
    line-height: 1; /* 修改这行 */

    /* 顶部菜单导航样式调整 */
    :deep(.el-menu--horizontal) {
      background-color: transparent;
      border-bottom: none;
      height: 100%; /* 添加这行 */
      display: flex; /* 添加这行 */
      align-items: center; /* 添加这行 */

      .el-menu-item {
        color: var(--navbarColor, #1E3A5F);
        height: 100%; /* 添加这行 */
        display: flex; /* 添加这行 */
        align-items: center; /* 添加这行 */

        &:hover,
        &.is-active {
          color: var(--menuActiveText, #1E40AF);
          background-color: rgba(30, 64, 175, 0.1);
        }
      }
    }
  }

  .errLog-container {
    display: flex; /* 修改为flex */
    align-items: center; /* 添加这行 */
    height: 100%; /* 添加这行 */
  }

  .tenant-select {
    margin-right: 8px;
    display: flex; /* 添加这行 */
    align-items: center; /* 添加这行 */
    height: 100%; /* 添加这行 */

    :deep(.el-input__wrapper) {
      box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
    }
  }

  .right-menu {
    float: right;
    height: 100%;
    line-height: 1; /* 改为1，让内部元素控制自己的行高 */
    display: flex;
    align-items: center; /* 添加这行，确保子元素垂直居中 */
    margin-right: 8px;

    &:focus {
      outline: none;
    }

    .right-menu-item {
      display: flex; /* 改为flex布局 */
      align-items: center; /* 确保内容垂直居中 */
      justify-content: center; /* 水平居中 */
      padding: 0 10px;
      height: 100%;
      font-size: 18px;
      color: var(--navbarColor, #1E3A5F); /* 使用柔和的颜色 */

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

        &:hover {
          background: rgba(30, 64, 175, 0.1);
          color: var(--menuActiveText, #1E40AF);
        }
      }
    }

    .icon-container {
      display: flex;
      align-items: center;
      justify-content: center;
      padding: 0 12px;
      height: 100%; /* 添加这行 */

      .menu-icon {
        font-size: 20px;
        transition: all 0.3s;
        color: #e6e6e6; /* 为深色背景调整颜色 */

        &:hover {
          transform: scale(1.1);
          color: #ffffff; /* 悬停时使用白色 */
        }
      }
    }

    .badge-item {
      cursor: pointer;
      display: flex; /* 添加这行 */
      align-items: center; /* 添加这行 */
      height: 100%; /* 添加这行 */
    }

    .avatar-container {
      margin-right: 30px;
      display: flex; /* 添加这行 */
      align-items: center; /* 添加这行 */
      height: 100%; /* 添加这行 */

      .avatar-wrapper {
        margin-top: 0; /* 修改这行，移除上边距 */
        position: relative;
        display: flex;
        align-items: center;
        padding: 0 10px;
        border-radius: 8px;
        transition: all 0.3s;
        height: 36px; /* 确保有明确的高度 */

        &:hover {
          background: rgba(255, 255, 255, 0.1);
        }

        .user-avatar {
          cursor: pointer;
          width: 36px;
          height: 36px;
          border-radius: 8px;
          box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
          transition: all 0.3s;

          &:hover {
            transform: scale(1.05);
          }
        }

        .dropdown-icon {
          cursor: pointer;
          position: absolute;
          right: -10px;
          margin-left: 4px;
          font-size: 12px;
          color: #e6e6e6; /* 为深色背景调整颜色 */
        }
      }
    }
  }

  /* SVG图标颜色调整 - 使用柔和的颜色 */
  :deep(.svg-icon) {
    color: var(--navbarColor, #1E3A5F);
    display: flex; /* 添加这行 */
    align-items: center; /* 添加这行 */
    justify-content: center; /* 添加这行 */
    height: 100%; /* 添加这行 */
  }

  /* El-icon颜色调整 */
  :deep(.el-icon) {
    color: var(--navbarColor, #1E3A5F);
    display: flex; /* 添加这行 */
    align-items: center; /* 添加这行 */
    justify-content: center; /* 添加这行 */
    height: 100%; /* 添加这行 */
  }
}

/* 深色模式使用柔和的统一样式 */
.dark-mode {
  .navbar {
    background: var(--navbarBg, #1E3A8A);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);

    .right-menu-item {
      color: var(--navbarColor, #CBD5E1);
    }

    .avatar-wrapper {
      &:hover {
        background: rgba(255, 255, 255, 0.05);
      }
    }
  }
}

/* 响应式调整 */
@media screen and (max-width: 768px) {
  .navbar {
    .right-menu {
      .right-menu-item {
        padding: 0 6px;
      }

      .icon-container {
        padding: 0 6px;
      }

      .avatar-container {
        margin-right: 15px;
      }
    }
  }
}

/* 工具提示相关样式调整 - 使用柔和的配色 */
:deep(.el-tooltip__popper) {
  background-color: #64748B !important;
  border-color: #64748B !important;

  .popper__arrow::after {
    border-top-color: #64748B !important;
  }
}

/* 下拉菜单调整，使用柔和的配色 */
:deep(.el-dropdown-menu) {
  &.el-popper[role='tooltip'] {
    .el-dropdown-menu__item:hover {
      background-color: rgba(30, 64, 175, 0.05);
    }

    .el-dropdown-menu__item.is-disabled {
      &:hover {
        background-color: transparent;
      }
    }
  }
}
</style>
