<template>
  <div class="header">
    <div class="logo" @click="toHome"></div>
    <div class="header-right">
      <el-button class="big_screen" plain @click="bigScreen">
        <img src="../assets/img/home_big_screen_icon.png" style="margin-right: 5px" /> 综合显示
      </el-button>
      <div class="header-user-con">
        <div class="header__user">
          <!-- 用户头像 -->
          <el-avatar class="user-avator" :size="30" :src="usrInfo.profilePicture" />
          <!-- 用户名下拉菜单 -->
          <el-dropdown class="user-name" trigger="click" @command="handleCommand">
            <div class="el-dropdown-link">
              <div style="margin-right: 10px">
                {{ usrInfo.trueName || '' }}
              </div>
              <div class="select-icon"></div>
            </div>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="user" @click="router.push('/user')">个人中心</el-dropdown-item>
                <el-dropdown-item divided command="logout" @click="onLogOut">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
        <!-- 消息中心 -->
        <div class="btn-bell" @click="toMessage">
          <el-tooltip effect="dark" :content="message ? `有${message}条未读消息` : `消息中心`" placement="bottom">
            <i class="el-icon-lx-notice"></i>
          </el-tooltip>
          <span class="btn-bell-badge" v-if="message"></span>
        </div>
      </div>
      <!-- 折叠按钮 -->
      <div class="collapse-btn" @click="collapseChange">
        <el-icon v-if="sidebar.collapse">
          <Expand />
        </el-icon>
        <el-icon v-else>
          <Fold />
        </el-icon>
      </div>
    </div>
  </div>
</template>
<script setup lang="ts">
  import { onMounted } from 'vue'
  import { useSidebarStore } from '../store/sidebar'
  import { useRouter } from 'vue-router'
  import imgurl from '../assets/img/img.jpg'
  import { ElMessage } from 'element-plus'
  import { fetchCurrentUserInfo } from '@/api/profile'
  import { signOut } from '@/api/login'
  import { getUserNotifications } from '@/api'

  const username: string | null = localStorage.getItem('ms_username')
  let message = ref(0)

  const sidebar = useSidebarStore()
  // 侧边栏折叠
  const collapseChange = () => {
    sidebar.handleCollapse()
  }

  onMounted(async () => {
    if (document.body.clientWidth < 1500) {
      collapseChange()
    }
    getUserInfo()
    await getMsg()
  })
  const getMsg = async () => {
    const data: any = await getUserNotifications({})
    message.value = data
  }
  const toMessage = () => {
    router.push('/message-center')
  }
  const toHome = () => router.push('/overview')
  const usrInfo: any = ref({})
  const getUserInfo = async () => {
    const data: any = await fetchCurrentUserInfo()
    usrInfo.value = data
  }

  // 用户名下拉菜单选择事件
  const router = useRouter()
  const handleCommand = (command: string) => {
    if (command == 'user') {
      router.push('/user')
    }
  }
  const onLogOut = async () => {
    // await signOut()
    localStorage.clear()
    window.client.end()
    window.client = null
    ElMessage.success('退出成功')
    router.push('/login')
  }
  const bigScreen = () => {
    router.push('/big-screen/synthesis')
  }
</script>
<style scoped>
  .header {
    position: relative;
    box-sizing: border-box;
    height: 50px;
    font-size: 22px;
    color: #fff;
    background-color: #2a376e;
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100vw;
  }

  .collapse-btn {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100%;
    float: right;
    padding: 0 21px;
    padding-left: 0;
    cursor: pointer;
  }

  .header .logo {
    cursor: pointer;
    float: left;
    width: 156px;
    height: 32px;
    background-image: url(../assets/img/logo.png);
    background-size: cover;
    margin-left: 19px;
  }

  .header-right {
    display: flex;
    align-items: center;
  }

  .header-user-con {
    display: flex;
    height: 50px;
    align-items: center;
  }

  .btn-fullscreen {
    transform: rotate(45deg);
    margin-right: 5px;
    font-size: 24px;
  }

  .btn-bell,
  .btn-fullscreen {
    position: relative;
    width: 30px;
    height: 30px;
    text-align: center;
    border-radius: 15px;
    cursor: pointer;
    display: flex;
    align-items: center;
    margin: 0 20px;
  }

  .btn-bell-badge {
    position: absolute;
    right: 4px;
    top: 0px;
    width: 8px;
    height: 8px;
    border-radius: 4px;
    background: #f56c6c;
    color: #fff;
  }

  .btn-bell .el-icon-lx-notice {
    color: #fff;
  }

  .user-name {
    margin-left: 10px;
  }

  .user-avator {
    width: 24px;
    height: 24px;
  }

  .el-dropdown-link {
    color: #fff;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: space-between;
  }

  .el-dropdown-menu__item {
    text-align: center;
  }

  .select-icon {
    width: 14px;
    height: 14px;
    background-image: url(../assets/icon/down-arrow.png);
    background-size: cover;
  }

  .header__user {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 10px;
    width: 120px;
    height: 32px;
    background: #3b4d8a;
    border-radius: 16px;
  }
  .big_screen {
    border: 1px solid #3A95C7;
    border-radius: 16px;
    margin-right: 20px;
    background: #305A8F;
    color: white;
  }

  .big_screen:hover{
    color: white;
    background: #466994;
  }
</style>
