<template>
  <div class="header grid min-h-[160px] w-full bg-cyan-800 flex flex-col">
    <div class="w-full h-[100px] flex max-w-screen-xl my-0 mx-auto justify-between items-center">
      <div class="logo">
        <a href="/">
          <img v-if="locale === 'zh'" src="@/assets/img/logo_4dge_cn.png" alt="logo" />
          <img v-if="locale === 'en'" src="@/assets/img/logo_4dge_en.png" alt="logo" />
        </a>
      </div>
      <div class="menu">
        <n-dropdown :options="options" @select="handleSelect">
          <n-button v-if="!browser.isMobile" type="primary" ghost>{{
            locale === 'zh' ? $t('zh') : $t('en')
          }}</n-button>
          <n-text v-else type="primary" ghost>{{ locale === 'zh' ? $t('zh') : $t('en') }}</n-text>
        </n-dropdown>
      </div>
    </div>
  </div>
</template>
<script setup lang="ts">
import { NDropdown, NButton } from 'naive-ui'
import browser from '@/utils/browser'
// import { SearchOutline } from '@vicons/ionicons5'

const { t, locale } = useI18n()
const router = useRouter()
const route = useRoute()

const options = ref([
  {
    label: t('zh'),
    key: 'zh',
  },
  {
    label: t('en'),
    key: 'en',
  },
])

const handleSelect = (key: string) => {
  locale.value = key
  localStorage.setItem('locale', key)

  router.replace({
    name: route.name,
    query: {
      lang: key,
    },
  })
  setTimeout(() => {
    location.reload()
  }, 50)
}
onMounted(() => {
  let localeValue = localStorage.getItem('locale') || 'zh'
  if (route.query.lang) {
    localeValue = String(route.query.lang)
    locale.value = localeValue
    localStorage.setItem('locale', localeValue)
  }

  locale.value = String(localeValue)
  document.title = t('web_title')
  console.log('route', route)
  // route.query.lang = localeValue
  router.replace({
    name: route.name,
    query: {
      lang: localeValue,
    },
  })
})
</script>
<style lang="scss" scoped>
.header {
  background-image: url('@/assets/img/banner_bg2.png');
  background-repeat: no-repeat;
  background-size: cover;
  background-position: top center;
}

[is-mobile] {
  .header {
    height: 2.1333rem;
    min-height: auto;
    padding: 0 0.5333rem;
    box-sizing: border-box;
  }
  .logo {
    img {
      width: 2.2rem;
      height: 0.5867rem;
    }
  }
  .menu {
    n-text {
      font-size: 0.3733rem;
    }
  }
}
</style>
