<!-- @format -->

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

    <div class="right-menu">
      <span
        style="
          color: #fff;
          display: inline-block;
          margin-right: 15px;
          font-size: 14px;
        "
        >登录用户：<span>{{ userStore.name }}</span></span
      >
      <div
        style="
          display: flex;
          align-items: center;
          margin-right: 10px;
          margin-top: 0px;
          cursor: pointer;
        "
      >
        <el-dropdown>
          <!-- <el-badge :hidden="!isHidden" class="item" is-dot>
            <svg-icon icon-class="xld" class="idClass" />
          </el-badge> -->
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item v-for="item of logList" :key="item.id">
                <div
                  class="itemClass"
                  :style="{ color: item.readStatus == '1' ? 'gray' : '' }"
                  @click="handleJump(item)"
                >
                  <span
                    style="
                      display: inline-block;
                      margin-right: 10px;
                      width: 120px;
                    "
                    >{{ item.noticeTitle }}：</span
                  >
                  <span class="content">{{ item.noticeContent }}</span>
                </div>
              </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
      <div class="avatar-container">
        <el-dropdown
          @command="handleCommand"
          class="right-menu-item hover-effect"
          trigger="click"
        >
          <div class="avatar-wrapper">
            <img :src="userStore.avatar" class="user-avatar" />
            <el-icon><caret-bottom /></el-icon>
          </div>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item command="logout">
                <span>退出登录</span>
              </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ElMessageBox } from "element-plus";
import Breadcrumb from "@/components/Breadcrumb";
import TopNav from "@/components/TopNav";
import Hamburger from "@/components/Hamburger";

import useAppStore from "@/store/modules/app";
import useUserStore from "@/store/modules/user";
import useSettingsStore from "@/store/modules/settings";
import { getLogList, readLog } from "@/api/menu.js";
import { useRouter } from "vue-router";
import { computed } from "vue";
import { color } from "echarts";

const router = useRouter();
const appStore = useAppStore();
const userStore = useUserStore();
const settingsStore = useSettingsStore();
const logList = ref([]);

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

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

function logout() {
  ElMessageBox.confirm("确定注销并退出系统吗？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      userStore.logOut().then(() => {
        location.href = "/index";
      });
    })
    .catch(() => {});
}

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

getLogList({ userId: userStore.id }).then((res) => {
  logList.value = res.data;
});

//通知跳转
const handleJump = (row) => {
  readLog({ id: row.id, readStatus: "1" }).then((res) => {
    getLogList({ userId: userStore.id }).then((res) => {
      logList.value = res.data;
    });
  });
  if (row.noticeType == "clbz") {
    router.push({
      path: "/material",
    });
  } else if (row.noticeType == "bysl") {
    router.push({
      path: "/inadmissible",
    });
  } else if (row.noticeType == "sptg") {
    router.push({
      path: "/approved",
    });
  }
};
const isHidden = computed(() => {
  return logList.value.some((item) => item.readStatus == "0");
});
</script>

<style lang="scss" scoped>
.itemClass:hover .content {
  text-decoration-line: underline;
}
.navbar {
  background: #0e62fe;
  height: 50px;
  overflow: hidden;
  position: relative;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);

  .hamburger-container {
    line-height: 46px;
    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;
  }
  .idClass {
    color: #fff;
    font-size: 26px;
  }
  .topmenu-container {
    position: absolute;
    left: 50px;
  }

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

  .itemClass {
    padding: 10px 15px;
  }
  .right-menu {
    float: right;
    height: 100%;
    line-height: 50px;
    display: flex;

    &:focus {
      outline: none;
    }

    .right-menu-item {
      display: inline-block;
      padding: 0 8px;
      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: 40px;

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

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

        i {
          cursor: pointer;
          position: absolute;
          right: -20px;
          top: 25px;
          font-size: 12px;
        }
      }
    }
  }
}
::v-deep .el-badge__content.is-fixed {
  top: 7px;
}
</style>
