<script lang="ts" setup name="Tools">
import { useFullscreen } from '@vueuse/core'
import type { Ref } from 'vue'
import ResetPwd from './resetPwd.vue'
// import alarmList from './alarmList.vue'
import eventBus from '@/utils/eventBus'
import useSettingsStore from '@/store/modules/settings'
import useUserStore from '@/store/modules/user'
import useRouteStore from '@/store/modules/route'
import useWebsocketStore from '@/store/modules/websocket'
const router = useRouter()

const settingsStore = useSettingsStore()
const userStore = useUserStore()
const routeStore = useRouteStore()
const websocket = useWebsocketStore()

const mainPage = useMainPage()
const { isFullscreen, toggle } = useFullscreen()

const resetPwd = ref()

function userCommand(command: 'dashboard' | 'resetPassword' | 'setting' | 'hotkeys' | 'logout' | 'index' | 'board') {
  switch (command) {
    case 'dashboard':
      router.push({
        name: 'dashboard',
      })
      break
    case 'resetPassword':
      resetPwd.value.initDialog()
      break
    case 'setting':
      router.push({
        name: 'personalSetting',
      })
      break
    // case 'board':
    //   router.push({
    //     name: 'FullScreen',
    //   })
    //   break
    case 'index':
      // 清空当前系统路由
      localStorage.removeItem('currentSys')
      routeStore.removeRoutes()
      router.push({
        path: '/index',
      })
      break
    case 'hotkeys':
      eventBus.emit('global-hotkeys-intro-toggle')
      break
    case 'logout':
      // localStorage.removeItem('currentSys')
      routeStore.removeRoutes()
      userStore.logout().then(() => {
        router.push({
          name: 'login',
        })
      })
      break
  }
}
// 报警组件
const alarmRef = ref()
// 跳转 报警或综合大屏
const board = () => {
  router.push({
    name: 'FullScreenControl',
  })
}
const popoverRef = ref()
// 关闭报警列表
const closePopover = () => {
  popoverRef.value.hide()
}
const alarmCount = computed(() => alarmRef.value?.messageList || [])

const alarmFlicker = ref(0)
const showAlarmFlicker = ref(false)
watch(() => alarmRef.value?.messageList, (newVal) => {
  if (newVal) {
    if (newVal.length) {
      // console.log(newVal, 'newVal')
      if (newVal.some((item: any) => item.processStatus === '1' && (item.alarmLevelName === '预警' || item.alarmLevelName === '二级'))) {
        alarmFlicker.value = 1
      }
      else {
        alarmFlicker.value = 0
      }
    }
    else {
      alarmFlicker.value = 0
    }
  } else {
    alarmFlicker.value = 0
  }
})
const alarmTimer = ref()
watch(() => alarmFlicker.value, () => {
  if (alarmFlicker.value) {
    clearInterval(alarmTimer.value)
    alarmTimer.value = null
    showAlarmFlicker.value = false
    alarmTimer.value = setInterval(() => {
      showAlarmFlicker.value = !showAlarmFlicker.value
    }, 1000);
  }
  else {
    clearInterval(alarmTimer.value)
    alarmTimer.value = null
    showAlarmFlicker.value = false
  }
})
onMounted(() => {
  // 连接 websocket
  // websocket.initWebSocket()
})
onBeforeMount(() => {
  // 断开 websocket
  // websocket.destroyWebSocket()
  if (alarmTimer.value) {
    clearInterval(alarmTimer.value)
    alarmTimer.value = null
  }
})
const { proxy } = getCurrentInstance() as any
</script>

<template>
  <div class="tools">
    <div class="buttons">
      <span v-if="settingsStore.navSearch.enable" class="item" @click="eventBus.emit('global-search-toggle')">
        <el-icon>
          <svg-icon name="ep:search" />
        </el-icon>
      </span>
      <span v-if="proxy.hasPerm('/fullscreen')" class="item" @click="board">
        <el-icon>
          <svg-icon name="ep:data-line" />
        </el-icon>
      </span>
      <span class="item" @click="toggle">
        <el-icon>
          <svg-icon :name="isFullscreen ? 'fullscreen-exit' : 'fullscreen'" />
        </el-icon>
      </span>
      <!-- 报警信息列表 -->
      <!-- <span v-if="proxy.hasPerm('/alarmnotice')" class="item">
        <el-popover ref="popoverRef" width="390">
          <template #reference>
            <div>
              <el-badge :hidden="!alarmCount.length" :value="alarmCount.length" >
                <el-icon>
                  <svg-icon v-show="!showAlarmFlicker" name="bell" />
                  <svg-icon v-show="showAlarmFlicker" name="bell-alarm" />
                </el-icon>
              </el-badge>
            </div>
          </template>
          <template #default>
            <alarm-list @close="closePopover" ref="alarmRef" />
          </template>
        </el-popover>
      </span> -->
      <span v-if="settingsStore.toolbar.enablePageReload" class="item" @click="mainPage.reload()">
        <el-icon>
          <svg-icon name="ep:refresh-right" />
        </el-icon>
      </span>
      <span v-if="settingsStore.toolbar.enableColorScheme" class="item"
        @click="settingsStore.setColorScheme(settingsStore.app.colorScheme === 'dark' ? 'light' : 'dark')">
        <el-icon>
          <svg-icon v-show="settingsStore.app.colorScheme === 'light'" name="ep:sunny" />
          <svg-icon v-show="settingsStore.app.colorScheme === 'dark'" name="ep:moon" />
        </el-icon>
      </span>
      <span v-if="settingsStore.toolbar.enableAppSetting" class="item" @click="eventBus.emit('global-theme-toggle')">
        <el-icon>
          <svg-icon name="ep:setting" />
        </el-icon>
      </span>
    </div>
    <el-dropdown class="user-container" size="default" @command="userCommand">
      <div class="user-wrapper">
        <!-- <el-avatar size="small">
          <el-icon>
            <svg-icon name="ep:user-filled" />
          </el-icon>
        </el-avatar> -->
        {{ userStore.name }}
        <el-icon>
          <svg-icon name="ep:caret-bottom" />
        </el-icon>
      </div>
      <template #dropdown>
        <el-dropdown-menu class="user-dropdown">
          <!-- <el-dropdown-item v-if="settingsStore.dashboard.enable" command="dashboard">
            控制台
          </el-dropdown-item>
          <el-dropdown-item command="setting">
            个人设置
          </el-dropdown-item>
          <el-dropdown-item v-if="settingsStore.mode === 'pc'" divided command="hotkeys">
            快捷键介绍
          </el-dropdown-item> -->
          <!-- <el-dropdown-item divided command="index">
            回首页
          </el-dropdown-item> -->
          <el-dropdown-item divided command="resetPassword">
            修改密码
          </el-dropdown-item>
          <el-dropdown-item divided command="logout">
            退出登录
          </el-dropdown-item>
        </el-dropdown-menu>
      </template>
    </el-dropdown>
    <reset-pwd ref="resetPwd" />
  </div>
</template>

<style lang="scss" scoped>
.tools {
  display: flex;
  align-items: center;
  padding: 0 20px;
  white-space: nowrap;

  .buttons {
    margin-right: 20px;

    .item {
      display: inline-flex;
      align-items: center;
      justify-content: center;
      height: 24px;
      width: 34px;
      cursor: pointer;
      // vertical-align:baseline;
      // display: flex;
      // flex-direction: column;
      // align-items: center;
      // justify-content: center;


      .el-icon {
        // color: var(--el-text-color-primary);
        color: #fff;
        transition: var(--el-transition-color);
      }
    }
  }
}

:deep(.user-container) {
  display: inline-block;
  height: 24px;
  line-height: 24px;
  cursor: pointer;

  .user-wrapper {
    color: #fff;

    .el-avatar {
      vertical-align: middle;
      margin-top: -2px;
      margin-right: 4px;
    }
  }
}
</style>
