<template>
  <div>
    <!-- 状态栏占位区域（仅在APP中显示） -->
    <div v-if="isInAppEnvironment" style="height: 30px"></div>

    <!-- header内容 -->
    <div
      style="
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 12px 16px;
        height: 60px;
      "
    >
      <!-- 返回键 -->
      <div style="width: 6%; display: flex; align-items: center">
        <button v-if="showBack" class="back-btn" @click="handleBack">
          <img
            class="flipImg"
            :src="backImg"
            alt=""
            style="width: 100%; aspect-ratio: 1/1; display: block"
          />
        </button>
      </div>

      <!-- 标题 -->
      <h1
        style="
          font-size: 1.2em;
          font-weight: 600;
          margin: 0;
          text-align: center;
          flex: 1;
          letter-spacing: 0.3px;
        "
        :style="{ color }"
      >
        {{ title }}
      </h1>
      <button class="help-btn" style="width: 10%" @click="$emit('help')">
        <img
          v-if="showHelp"
          src="../assets/icon/helpWhite.png"
          alt=""
          style="width: 100%; display: block"
        />
      </button>

      <!-- 语言切换 -->
      <div
        style="
          display: flex;
          align-items: center;
          position: relative;
          color: rgba(187, 146, 255, 1) !important;
        "
        @click="showLang"
        v-if="showLanguageList"
      >
        <div style="font-weight: bold">{{ currentLangType }}</div>
        <!-- <div
              style="margin-left: 5px; transition: transform 0.5s"
              :class="{ rotated: visibleList }"
            >
              >
            </div> -->
        <transition name="slide-fade">
          <div v-show="visibleList" class="extraList">
            <div
              style="padding: 8px; color: black !important; font-weight: 590"
              v-for="lang in langStore.langList"
              @click.stop="handleLanguageChange(lang)"
            >
              {{ lang.value }}
            </div>
          </div>
        </transition>
      </div>

      <!-- 插槽 -->
      <slot name="extraFunction"></slot>
    </div>
  </div>
</template>

<script setup>
import { isInApp, closePage } from '@/utils/appBridge.js'
import { useRouter, useRoute } from 'vue-router'
import { computed, ref, onMounted, watch } from 'vue'
import { useLangStore } from '@/stores/lang'
import { setLocale } from '@/locales/i18n'

// 定义props
const props = defineProps({
  title: {
    type: String,
    required: true,
  },
  showBack: {
    type: Boolean,
    default: true,
  },
  showHelp: {
    type: Boolean,
    default: false,
  },
  isHomePage: {
    type: Boolean,
    default: false,
  },
  showLanguageList: {
    type: Boolean,
    default: false,
  },
  color: {
    type: String,
    default: '#fff',
  },
  backImg: {
    type: String,
    default: () => {
      return new URL('../assets/icon/arrowBack.png', import.meta.url).href
    },
  },
})

// 定义emits
defineEmits(['help'])

const router = useRouter()
const route = useRoute()
const langStore = useLangStore()

const visibleList = ref(false) //语言列表显示

// 当前语言类型
const currentLangType = computed(() => {
  return langStore.selectedLang?.value || 'EN'
})

// 监听语言变化，确保UI更新
watch(
  () => langStore.selectedLang,
  (newLang) => {
    console.log('Language changed in header:', newLang)
  },
  { deep: true }
)

const handleLanguageChange = (item) => {
  visibleList.value = false
  console.log('visibleList.value', visibleList.value)

  console.log('Changing language to:', item)
  setLocale(langStore, item.type)
  // 立即更新visibleList状态
}

// 检测是否在APP环境中
const isInAppEnvironment = ref(false)

// 定义首页路由列表
const homeRoutes = ['/host-center', '/coin-seller', '/invitation-to-register', '/lottery', '/']

// 计算是否为首页
const isCurrentlyHomePage = computed(() => {
  // 1. 首先检查 props
  const propsHomePage = props.isHomePage === true || props.isHomePage === 'true'

  // 2. 然后检查路由
  const routeHomePage = homeRoutes.includes(route.path)

  // 3. 两者任一为true即为首页
  return propsHomePage || routeHomePage
})

const handleBack = () => {
  if (isCurrentlyHomePage.value && isInApp()) {
    // 首页且在APP中：关闭页面
    console.log('home back')
    closePage() // 这个函数似乎未定义，需要确认
  } else {
    // 非首页或浏览器环境：正常返回
    router.go(-1)
  }
}

//语言列表显示
const showLang = () => {
  visibleList.value = !visibleList.value

  console.log('visibleList.value', visibleList.value)
}

// 组件挂载时检测环境
onMounted(() => {
  isInAppEnvironment.value = isInApp()
})
</script>

<style scoped>
* {
  font-family: 'SF Pro Text';
}

.back-btn,
.help-btn {
  border: none;
  background: transparent;
  transition: all 0.2s ease;
}

.back-btn:active,
.help-btn:active {
  transform: scale(0.9);
}

.extraList {
  position: absolute;
  z-index: 9;
  right: 0;
  top: 100%;
  padding: 10px;
  display: flex;
  flex-direction: column;
  width: max-content;
  background-color: rgba(255, 255, 255);
  border-radius: 10px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  margin-top: 5px;
  backdrop-filter: blur(8px);
}

.extraList > div:not(:last-child) {
  border-bottom: 0.1px solid black;
}

[dir='rtl'] .flipImg {
  transform: scaleX(-1);
}

[dir='rtl'] .extraList {
  right: auto;
  left: 0;
}

@media screen and (max-width: 360px) {
  * {
    font-size: 10px;
  }
}

@media screen and (min-width: 360px) {
  * {
    font-size: 16px;
  }
}

@media screen and (min-width: 768px) {
  * {
    font-size: 24px;
  }
}

@media screen and (min-width: 1024px) {
  * {
    font-size: 32px;
  }
}
</style>
