<template>
  <div
    class="navbar"
    :style="{
      display: 'flex',
      backgroundColor: settingsStore.topTheme + '!important',
    }"
  >
    <transition
      class="transition-container"
      style="float: left; display: flex; height: 100%"
      name="sidebarLogoFade"
      v-if="settingsStore.navigationType === 'top-menuMix-type' || settingsStore.navigationType === 'top-menu-type'"
    >
      <div class="sidebar-logo-link" key="expand" @click="returnMiddle">
        <img v-if="middlePage.page.homeLogo" :src="middlePage.page.homeLogo" class="sidebar-logo" style="margin: auto 15px; height: 30px" @click="home" />
        <span
          @click="home"
          class="sidebar-title"
          :style="{
            margin: 'auto',
            marginRight: '15px',
            fontSize: '22px',
            fontFamily: 'Microsoft YaHei',
            fontWeight: 400,
            'white-space': 'nowrap',
            color: settingsStore.topFontColor + '!important',
          }"
        >
          {{ middlePage.page.middleTitle }}
        </span>
      </div>
    </transition>


    <top-nav class="topmenu-container" v-if="settingsStore.navigationType === 'top-menu-type'" style="min-width: 0; flex: 1; justify-content: center" id="topmenu-container" />

    <div class="right-menu" :style="{ marginLeft: settingsStore.navigationType !== 'top-menu-type' ? 'auto' : '' }">
      <template v-if="appStore.device !== 'mobile'">
        <div
          v-if="middlePage.page.largeScreenUrl"
          class="right-menu-item"
          :style="{
            color: settingsStore.topFontColor + '!important',
          }"
          @click="dpUrl"
        >
          <svg-icon icon-class="大屏" />
        </div>

        <header-search
          class="right-menu-item"
          :style="{
            color: settingsStore.topFontColor + '!important',
          }"
        />

        <msg-view v-if="middlePage.page.messageStatus === 1" class="right-menu-item"></msg-view>
      </template>
      <div class="avatar-container">
        <el-dropdown
          @command="handleCommand"
          class="right-menu-item hover-effect"
          trigger="click"
          :style="{
            color: settingsStore.topFontColor + '!important',
          }"
        >
          <div class="avatar-wrapper">
            <span>{{ userStore.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 command="setLayout">
                <span>布局设置</span>
              </el-dropdown-item>
              <router-link to="/system/editMenu" v-hasPermi="['menu:edit']">
                <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 { ElMessage, ElMessageBox } from "element-plus";
import { getCurrentInstance, defineComponent, ref, onMounted, watch } from "vue";
/*----------------------------------接口引入-----------------------------------*/
/*----------------------------------组件引入-----------------------------------*/
import TopNav from "@/components/TopNav";
import Screenfull from "@/components/Screenfull";
import SizeSelect from "@/components/SizeSelect";
import HeaderSearch from "@/components/HeaderSearch";
/*----------------------------------store引入-----------------------------------*/
import { useUserStore, useSettingsStore, useAppStore } from "@/store/modules/index";
/*----------------------------------公共方法引入-----------------------------------*/
/*----------------------------------公共变量-----------------------------------*/
const router = useRouter();
const appStore = useAppStore();
const userStore = useUserStore();
const settingsStore = useSettingsStore();

const props = defineProps({});
const emits = defineEmits(["setLayout"]);
const { proxy } = getCurrentInstance();
/*----------------------------------变量声明-----------------------------------*/
const dropdown = ref(null);
const middlePage = computed(() => userStore.middlePageData); //全局公共变量集
const navigationType = ref(settingsStore.navigationType); //导航栏模式

function toggleSideBar() {
  appStore.toggleSideBar();
}

/**
 * @下拉菜单触发事件
 */
function handleCommand(command) {
  switch (command) {
    case "setLayout":
      emits("setLayout");
      break;
    case "logout":
      logout();
      break;
    case "emptyNotice":
      break;
    case "seeMore":
      router.push({ path: "/message/center" });
      dropdown.value.handleClose();
      break;
    default:
      break;
  }
}

/**
 * @logo点击事件
 */
function returnMiddle() {
  if (localStorage.getItem("middlePageStatus")) {
    router.push({ path: "/middle" });
  }
}

/**
 * @logo标题点击事件
 */
function home() {
  let data = middlePage.defaultHomePage;
  if (data.indexOf("http") > 0) {
    window.open(data, "_blank");
  } else {
    router.push({ path: data });
  }
}

/**
 * @大屏跳转按钮点击事件
 */
function dpUrl() {
  window.open(middlePage.page.largeScreenUrl + "?token=" + localStorage.getItem("token725"), "_blank");
}

/**
 * @退出按钮点击事件
 */
function logout() {
  ElMessageBox.confirm("确定注销并退出系统吗？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      localStorage.clear();
      userStore.logOut().then(() => {
        location.href = "/login";
      });
    })
    .catch(() => {});
}
</script>

<style lang="scss" scoped>
@import "@/assets/styles/variables.module.scss";
@import "@/assets/styles/handle.scss";

.navbar {
  height: 56px;
  overflow: hidden;
  position: relative;
  background: #fff;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);

  .hamburger-container {
    line-height: 46px;
    height: 100%;

    // position: absolute;
    // right: 0;
    cursor: pointer;
    transition: background 0.3s;
    -webkit-tap-highlight-color: transparent;

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

  .topmenu-container {
    // position: absolute;
    // left: 50px;
    // margin: auto;
    background: transparent;
  }

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

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

    &:focus {
      outline: none;
    }

    .right-menu-item {
      display: inline-block;
      padding: 0 8px;
      height: 100%;
      font-size: 16px;
      @include font_color("fontColor");
      @include fill_color("fontColor");
      vertical-align: text-bottom;
      cursor: pointer;

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

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

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

      .avatar-wrapper {
        position: relative;
        line-height: 54px;
        // margin-top:12px;
        span {
          display: inline-block;
          position: relative;
          // top:-9px;
          font-size: 14px;
        }

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

        i {
          cursor: pointer;
          position: absolute;
          right: -20px;
          top: 21px;
          font-size: 12px;
        }
      }
    }
  }
}

.sidebar-title {
  padding: 0 10px;
}
</style>
