<script lang="ts">
import logo from "@/assets/images/logo.png";
import { EMitt, ESidebarLayoutEnum, EThemeSetting } from "@/constants/enum";
import emits from "@/utils/emits";
import { getThemeConfigCacheByKey } from "@/utils/theme";
import { computed, defineComponent, reactive } from "vue";
import { useAppStore } from "@/store";
import { useAlertMarquee } from "@/composables/useAlertMarquee";
import { useImportTaskStore } from "@/store/importTasks";
import {Bell, Message} from "@element-plus/icons-vue";
import BaseSidebar from "../sidebar/base-sidebar.vue";
import Breadcrumb from "./breadcrumb.vue";
import CollapseSidebarBtn from "./collapse-sidebar-btn.vue";
import Expand from "./expand.vue";
import HeaderMixNavMenus from "./header-mix-nav-menus.vue";
import Logo from "./logo.vue";
import "@/assets/css/header.less";

/**
 * 顶部主区域
 */
export default defineComponent({
  name: "Header",
  components: {Bell, Message, BaseSidebar, Breadcrumb, CollapseSidebarBtn, Expand, HeaderMixNavMenus, Logo },
  setup() {
    const store = useAppStore();
    const { messageCount: alertCount, toggleDrawer: toggleNotificationCenter } = useAlertMarquee();
    const importTaskStore = useImportTaskStore();
    const combinedCount = computed(() => alertCount.value + importTaskStore.activeTasks.length);
    const state = reactive({
      sidebarLayout: getThemeConfigCacheByKey(EThemeSetting.NavLayout)
    });
    emits.on(EMitt.OnSetNavLayout, (vl) => {
      state.sidebarLayout = vl;
    });
    const onRefresh = () => {
      emits.emit(EMitt.OnReloadTabPage);
    };
    return { store, state, onRefresh, logo, ESidebarLayoutEnum, combinedCount, toggleNotificationCenter, Bell };
  }
});
</script>
<template>
  <div class="rr-header-ctx">
    <div class="rr-header-ctx-logo hidden-xs-only">
      <logo :logoUrl="logo" logoName="气象数据平台"></logo>
    </div>
    <div class="rr-header-right">
      <div class="rr-header-right-left">
        <div class="rr-header-right-items rr-header-action" :style="`display:${state.sidebarLayout === ESidebarLayoutEnum.Top ? 'none' : ''}`">
          <collapse-sidebar-btn></collapse-sidebar-btn>
          <div role="button" tabindex="0" aria-label="刷新页面" @click="onRefresh" @keydown.enter="onRefresh" style="cursor: pointer">
            <div class="el-badge">
              <el-icon><refresh-right /></el-icon>
            </div>
          </div>
        </div>
        <div class="rr-header-right-left-br ele-scrollbar-hide hidden-xs-only">
          <base-sidebar v-if="state.sidebarLayout === ESidebarLayoutEnum.Top" mode="horizontal" :router="true"></base-sidebar>
          <header-mix-nav-menus v-else-if="state.sidebarLayout === ESidebarLayoutEnum.Mix"></header-mix-nav-menus>
          <breadcrumb v-else></breadcrumb>
        </div>
        <div class="rr-header-notify" role="button" tabindex="0" aria-label="通知中心" @click="toggleNotificationCenter" @keydown.enter="toggleNotificationCenter">
          <el-badge :value="combinedCount" :hidden="combinedCount === 0">
            <el-icon><Bell /></el-icon>
          </el-badge>
        </div>
        <expand :userName="store.state.user.username"></expand>
      </div>
    </div>
  </div>
</template>

<style scoped>
.rr-header-notify {
  height: 50px;
  line-height: 56px;
  padding: 0 12px;
  color: rgba(255, 255, 255, 0.66);
  cursor: pointer;
  transition: background 0.2s, color 0.2s;
}

.rr-header-notify:hover {
  color: #fff;
  background: rgba(0, 0, 0, 0.08);
}

.rr-header-notify :deep(.el-badge) {
  line-height: normal;
}
</style>
