<template>
  <div class="navbar">
    <logo v-if="showLogo" :collapse="isCollapse" />
    <hamburger id="hamburger-container" :is-active="appStore.sidebar.opened" class="hamburger-container" @toggle-click="toggleSideBar" />
    <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'">
        <search-menu ref="searchMenuRef" />
        <el-tooltip content="搜索" effect="dark" placement="bottom">
          <div class="right-menu-item hover-effect" @click="openSearchMenu">
            <div class="search-icon" />
          </div>
        </el-tooltip>
        <!-- 消息 -->
<!--        <el-tooltip content="消息" effect="dark" placement="bottom">-->
<!--          <div style="margin: 12px">-->
<!--            <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">-->
<!--                  <div class="message" />-->
<!--                </el-badge>-->
<!--              </template>-->
<!--              <template #default>-->
<!--                <notice></notice>-->
<!--              </template>-->
<!--            </el-popover>-->
<!--          </div>-->
<!--        </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" />
            <span>{{ nickName }}</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 divided command="logout">
                <span>退出登录</span>
              </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </div>
  </div>
</template>

<script setup>
import Logo from './Sidebar/Logo.vue';
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 notice from './notice/index.vue';

const appStore = useAppStore();
const userStore = useUserStore();
const settingsStore = useSettingsStore();
const noticeStore = storeToRefs(useNoticeStore());
const newNotice = ref(2);
const showLogo = computed(() => settingsStore.sidebarLogo);
const isCollapse = computed(() => !appStore.sidebar.opened);
const nickName = computed(() => userStore.nickname);

// 搜索菜单
const searchMenuRef = ref();

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

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 = {
  setLayout,
  logout
};
const handleCommand = (command) => {
  // 判断是否存在该方法
  if (commandMap[command]) {
    commandMap[command]();
  }
};
//用深度监听 消息
watch(
  () => noticeStore.state.value.notices,
  (newVal) => {
    newNotice.value = newVal.filter((item) => !item.read).length;
  },
  { deep: true }
);
</script>

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

:deep(.el-badge__content.is-fixed) {
  top: 12px;
}

.flex {
  display: flex;
}

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

.navbar {
  height: 50px;
  overflow: hidden;
  position: relative;
  //background: url('@/assets/images/header.png') no-repeat;
  //background-size: 100% 100%;
  box-shadow: 0 1px 6px rgba(0, 21, 41, 0.12);
  position: relative;
  &::before {
    content: '';
    width: 163px;
    height: 34px;
    background: url('@/assets/images/city.png') no-repeat;
    background-size: 100% 100%;
    position: absolute;
    bottom: 0;
    left: 0;
    pointer-events: none;
  }
  &::after {
    content: '';
    width: 163px;
    height: 34px;
    background: url('@/assets/images/city.png') no-repeat;
    background-size: 100% 100%;
    position: absolute;
    bottom: 0;
    right: 0;
    pointer-events: none;
  }
  .hamburger-container {
    line-height: 50px;
    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%;
    display: flex;

    &:focus {
      outline: none;
    }

    .right-menu-item {
      display: inline-block;
      padding: 0 12px;
      //height: 100%;
      font-size: 18px;
      color: #5a5e66;
      vertical-align: text-bottom;

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

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

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

      .avatar-wrapper {
        margin-top: 5px;
        position: relative;
        display: flex;
        align-items: center;
        font-size: 14px;
        height: 100%;
        .user-avatar {
          cursor: pointer;
          width: 24px;
          height: 24px;
          border-radius: 50%;
          margin: 12px;
        }

        i {
          cursor: pointer;
          //position: absolute;
          //right: -20px;
          //top: 25px;
          font-size: 12px;
        }
      }
    }
  }
}
.search-icon {
  width: 17px;
  height: 16px;
  background: url('@/assets/images/search.png') no-repeat;
  background-size: 100% 100%;
}
.message {
  width: 17px;
  height: 16px;
  background: url('@/assets/images/message.png') no-repeat;
  background-size: 100% 100%;
}
</style>
