<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">
            <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">[切换城市]</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="">我要找工作</a>
              <a href="">我要招聘</a>
            </span>
            <v-btn class="half-button" border color="primary" size="small" @click="handleLogin">登录/注册</v-btn>
          </div>
          <div class="d-flex align-center" v-if="getToken()">
            <span style="cursor: pointer;">消息</span>
            <v-menu open-on-hover>
              <template v-slot:activator="{ props }">
                <div class="d-flex ml-5 align-center" v-bind="props">
                  <v-avatar>
                    <v-img alt="John" src="https://cdn.vuetifyjs.com/images/john.jpg"></v-img>
                  </v-avatar>
                  <div class="ml-2">游客</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">
              <v-list-item
                v-for="item in localeStore.localeMap"
                :key="item.name"
                :value="item.lang"
                :active="localeStore.currentLocale.lang === item.lang"
                @click="localeStore.setCurrentLocale(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" :footer="true" title="企业注册" widthType="1" @close="show = false">
      <enterpriseRegister ref="enterpriseRegisterRef"></enterpriseRegister>
    </Dialog>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { getToken } from '@/utils/auth'
import { useUserStore } from '@/store/user'
import { useLocaleStore } from '@/store/locale'
defineOptions({ name: 'personal-navbar' })
import Dialog from '@/components/CtDialog'
import enterpriseRegister from '@/views/enterprise/components/register.vue'

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

const localeStore = useLocaleStore()

const list = ref([
  { text: '首页', path: '/home' },
  { text: '职位', path: '/recruit/position' },
  { text: '公司', path: '' }
])

import { useRouter } from 'vue-router'
const router = useRouter()

const userType = ref(1) // 0企业用户、1个人用户
const show = ref(false)
const changeRole = () => {
  if (userType.value) {
    show.value = true
  } else {
    router.push({ path: '/' })
  }
}

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

const items = ref([
  { title: '在线简历', icon: 'mdi-list-box-outline', change: () => router.push({ path: '/resume' }) },
  { title: '账号设置', icon: 'mdi-cog-outline', change: () => router.push({ path: '/personalAccount/accountBinding' }) },
  { title: '切换为招聘者', icon: 'mdi-swap-horizontal', change: changeRole },
  { title: '退出登录', icon: 'mdi-logout', change: handleLogout }
])

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

</script>

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