<template>
  <div class="blue-header-bar">
    <div class="blue-header-bar__left">
      <img src="@/assets/images/detail/logo.png" class="blue-header-bar__logo" alt="logo" />
      <span class="blue-header-bar__title">智惠映AI自动拍照机 <span class="blue-header-bar__version"  @click="openOTA">{{ currentVersion }}</span></span>
    </div>
    <div class="blue-header-bar__right">
      <div class="blue-header-bar__user">
        <span class="blue-header-bar__user-label">昵称：</span>
        <span class="blue-header-bar__user-name">
          {{  useUserInfoStore.userInfo.account_name
            || useUserInfoStore.userInfo.real_name
            || useUserInfoStore.userInfo.login_name
            || '未登录' }}
        </span>
      </div>
    </div>
  </div>
  <div class="blue-header-bar_blank"></div>
</template>

<script setup lang="ts">
import {defineProps, reactive, onMounted, onUnmounted, ref} from 'vue'
import useUserInfo from '@/stores/modules/user'
import tokenInfo from '@/stores/modules/token';
import packageJson from '@/../../package.json'
import client from '@/stores/modules/client'
import {useRouter} from "vue-router";
import icpList from '@/utils/ipc'
import { getRouterUrl } from '@/utils/appfun'

const useUserInfoStore = useUserInfo()
const tokenInfoStore = tokenInfo();
const currentVersion = ref(packageJson.version)
const clientStore = client()

const Router = useRouter()
onMounted(async ()=>{

  if (tokenInfoStore.getToken /* 已登录 */) {
    if(!useUserInfoStore.userInfo.id){
      await useUserInfoStore.getInfo()
    }
  }
})

function openOTA() {
  const { href } = Router.resolve({
    name: 'ota'
  })

  clientStore.ipc.removeAllListeners(icpList.utils.openMain)
  let params = {
    title: '版本更新',
    width: 900,
    height: 700,
    frame: true,
    id: 'ota',
    url: getRouterUrl(href)
  }
  clientStore.ipc.send(icpList.utils.openMain, params)
}
</script>

<style lang="scss" scoped>
.blue-header-bar_blank {
  width: 100%;
  height: 50px;
}

.blue-header-bar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 50px;
  background: linear-gradient(135deg, #2957FF 0%, #1E3A8A 100%);
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  z-index: 100;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  app-drag: drag;
  -webkit-app-region: drag;

  &__left {
    display: flex;
    align-items: center;
    gap: 12px;
  }

  &__logo {
    width: 36px;
    height: 36px;
  }

  &__title {
    color: #fff;
    font-size: 16px;
    font-weight: 600;
  }
  &__version {
    background: rgba(255,255,255,0.2);
    border-radius: 12px;
    font-size: 12px;
    padding: 3px 10px;
    font-weight: normal;
    margin-left: 5px;
  }

  &__right {
    display: flex;
    align-items: center;
    gap: 20px;
    app-drag: no-drag;
    -webkit-app-region: no-drag;
  }

  &__user {
    display: flex;
    align-items: center;
    color: #fff;
    font-size: 14px;

    &-label {
      opacity: 0.8;
      margin-right: 4px;
    }

    &-name {
      font-weight: 500;
    }
  }

  &__controls {
    display: flex;
    gap: 2px;
  }

  .control-btn {
    width: 30px;
    height: 30px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    color: #fff;
    font-size: 18px;
    transition: background-color 0.2s;

    &:hover {
      background-color: rgba(255, 255, 255, 0.2);
    }

    &--close:hover {
      background-color: rgba(255, 0, 0, 0.3);
    }

    span {
      line-height: 1;
    }
  }
}
</style>

