<template>
  <div>
    <v-toolbar
      class="banner"
      density="compact"
      style="padding-left: 0px;height: 50px;font-size: 14px;"
    >
      <div class="innerBox d-flex justify-space-between">
        <div>
          <div class="nav-logo" style="cursor: pointer;" @click="handleLogoClick">
            <v-img src="../../assets/logo.png"  aspect-ratio="16/9" cover :width="90" style="height: 40px"></v-img>
          </div>
          <div class="nav-city">
            <p class="nav-city-box">
              <v-icon color="primary">mdi-map-marker</v-icon>
              <span class="nav-city-selected">广州</span>
              <span class="switchover-city nav-city-selected">[{{ $t('sys.switchCities') }}]</span>
            </p>
          </div>
          <div class="nav">
            <ul>
              <li v-for="k in list" :key="k.text">
                <a :href="k.path" style="font-size: 14px;">{{ k.text }}</a>
              </li>
            </ul>
          </div>
        </div>
        
        <div class="d-flex user-nav">
          <div class="btns d-flex align-center" v-if="!getToken()">
            <span class="nav-resume-tools">
              <a href="">{{ $t('sys.lookingJob') }}</a>
              <a href="">{{ $t('sys.recruit') }}</a>
            </span>
            <v-btn class="half-button" border color="primary" size="small" @click="handleLogin">{{ $t('login.register') }}</v-btn>
          </div>
          
          <!-- 头像用户名 -->
          <div class="d-flex align-center" v-if="getToken()">
            <span class="cursor-pointer">{{ $t('sys.news') }}</span>
            <v-menu open-on-hover>
              <template v-slot:activator="{ props }">
                <div class="d-flex ml-5 pl-2 align-center cursor-pointer" v-bind="props" @click="handleToPersonalCenter">
                  <v-avatar>
                    <v-img alt="John" :src="baseInfo?.avatar ?? 'https://minio.citupro.com/dev/menduner/7.png'"></v-img>
                  </v-avatar>
                  <div class="ml-2">{{ baseInfo?.name ?? $t('sys.tourist') }}</div>
                </div>
              </template>

              <v-list>
                <v-list-item v-for="(item, index) in items" :key="index" @click="item.change">
                  <template v-slot:prepend>
                    <v-icon :icon="item.icon"></v-icon>
                  </template>
                  <v-list-item-title>{{ item.title }}</v-list-item-title>
                </v-list-item>
              </v-list>
            </v-menu>
          </div>

          <!-- 语言切换 -->
          <v-menu>
            <template v-slot:activator="{ props }">
              <v-btn
                class="ml-3"
                color="primary"
                icon="mdi-translate"
                size="small"
                v-bind="props"
              >
              </v-btn>
            </template>
            <v-list density="compact" color="primary">
              <v-list-item
                v-for="item in localeStore.localeMap"
                :key="item.name"
                :value="item.lang"
                :active="localeStore.currentLocale.lang === item.lang"
                @click="handleChangeLocale(item)"
              >
                <v-list-item-title>{{ item.name }}</v-list-item-title>
              </v-list-item>
            </v-list>
          </v-menu>
        </div>
        
      </div>
    </v-toolbar>
    <!-- <Dialog :visible="show" title="提示" :footer="false" widthType="2" @close="show = false">
      <div class="mb-3 text-center" style="color: #999;">
        <div class="mb-5">您还未加入或注册企业, 请选择您要操作的类型！</div>
        <v-btn class="half-button" size="small" color="primary" variant="tonal" @click="handleChange(0)">加入企业</v-btn>
        <v-btn class="half-button ml-5" size="small" color="primary" variant="tonal" @click="handleChange(1)">{{ $t('enterprise.registeringNewEnterprise') }}</v-btn>
      </div>
    </Dialog> -->
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { getToken } from '@/utils/auth'
import { useUserStore } from '@/store/user'
import { useLocaleStore } from '@/store/locale'
import { useI18n } from '@/hooks/web/useI18n'
import { getUserBindEnterpriseList, getUserRegisterEnterpriseApply } from '@/api/personal/user'
defineOptions({ name: 'personal-navbar' })

defineProps({
  sticky: {
    type: Boolean,
    default: true
  }
})

const { t } = useI18n()
const localeStore = useLocaleStore()
const userStore = useUserStore()

const list = ref([
  { text: t('common.home'), path: '/home' },
  { text: t('common.position'), path: '/recruit/position' },
  { text: t('common.company'), path: '/recruit/company' }
])

import { useRouter } from 'vue-router'
const router = useRouter()
const handleLogoClick = () => { router.push({ path: '/home'}) }

// 查看用户是否有企业
const changeLoginType = async () => {
  // router.push({ path: '/login' })
  // router.push({ name: 'login', query: { loginType: 330 } })

  const data = await getUserBindEnterpriseList() // 申请通过才会数据，否则空数组
  if (data?.length) {
    localStorage.setItem('companyInfo', JSON.stringify(data))
    changeRole()
  }
  else getApplyInfo()
}

// 查看用户是否有在申请中的数据
const getApplyInfo = async () => {
  const data = await getUserRegisterEnterpriseApply()
  const bool = data && Object.keys(data).length // 已经有数据说明已经申请过了
  localStorage.setItem('userApplyInfo', JSON.stringify(data))
  const path = bool ? '/enterprise/inReview' : '/enterprise/register'
  router.push({ path })
}

const handleToPersonalCenter = () => {
  router.push({ path: '/personalCenter' })
}

// 退出登录
const handleLogout = async () => {
  // try {
    await userStore.userLogout()
    router.push({ path: '/login' })
  // } catch (error) {
  //   console.log(error, 'error')
  // }
}

// 切换为招聘者
const changeRole = async () => {
  await userStore.userLogout()
  router.push({ name: 'login', query: { loginType: 330 } })
}

const items = ref([
  { title: t('resume.onlineResume'), icon: 'mdi-list-box-outline', change: () => router.push({ path: '/resume' }) },
  { title: t('setting.accountSettings'), icon: 'mdi-cog-outline', change: () => router.push({ path: '/personalAccount/accountBinding' }) },
  { title: t('setting.switchToRecruit'), icon: 'mdi-swap-horizontal', change: changeLoginType },
  { title: t('setting.logOut'), icon: 'mdi-logout', change: handleLogout }
])

let baseInfo = ref(JSON.parse(localStorage.getItem('baseInfo')) || {}) // 人才信息
userStore.$subscribe((mutation, state) => {
  baseInfo.value = state.baseInfo
})

const handleLogin = () => {
  router.push({ path: '/login' })
}

// 语言切换
const handleChangeLocale = (item) => {
  localeStore.setCurrentLocale(item)
  location.reload()
}
</script>

<style lang="scss" scoped>
@import '@/styles/personal/navBar.scss'
</style>
