<template>
  <q-layout view="hHh lpR fFf">
    <q-header elevated>
      <q-toolbar class="sd-page-padding bg-white" style="height: 80px;">
        <img src="~assets/images/logo.png" alt="logo"
          style="position: absolute; left: 8%; top: 50%; transform: translateY(-50%); width: 83px; height: 62px;" />
        <div class="row items-center no-wrap">
          <q-btn flat no-caps @click="$router.push('/test')">
            <div style="position: relative;">
              <span class="text-subtitle1 text-color-linear">主站</span>
              <q-icon name="sports_esports" color="text-subtitle1 text-color-linear"
                style="position: absolute; bottom: -18px; left: 2px; z-index: -1; transform: rotate(-15deg);" />
            </div>
          </q-btn>
          <div @mouseenter="showGamesDropdown" @mouseleave="hideGamesDropdown">
            <q-btn-dropdown :content-class="['no-shadow-menu']" :menu-offset="[50, 22]" flat no-caps class="text-black"
              ref="gamesDropdown">
              <template v-slot:label>
                <span class="text-subtitle1">游戏</span>
              </template>
              <q-list @mouseenter="showGamesDropdown" @mouseleave="hideGamesDropdown">
                <q-item clickable v-close-popup to="/games" class="flex items-center">
                  <q-item-section side>
                    <q-icon class="text-grey-7" right size="16px" style="margin-top: 4px;" name="computer" />
                  </q-item-section>
                  <q-item-section>PC</q-item-section>
                </q-item>
                <q-item clickable v-close-popup to="/games" class="flex items-center">
                  <q-item-section side>
                    <q-icon class="text-grey-7" right size="16px" style="margin-top: 4px;" name="phone_android" />
                  </q-item-section>
                  <q-item-section>Mobile</q-item-section>
                </q-item>
                <q-item clickable v-close-popup to="/games" class="flex items-center">
                  <q-item-section side>
                    <q-icon class="text-grey-7" right size="16px" style="margin-top: 4px;" name="sports_esports" />
                  </q-item-section>
                  <q-item-section>Playstation</q-item-section>
                </q-item>
                <q-item clickable v-close-popup to="/games" class="flex items-center">
                  <q-item-section side>
                    <q-icon class="text-grey-7" right size="16px" style="margin-top: 4px;" name="gamepad" />
                  </q-item-section>
                  <q-item-section>Xbox</q-item-section>
                </q-item>
                <q-item clickable v-close-popup to="/games" class="flex items-center">
                  <q-item-section side>
                    <q-icon class="text-grey-7" right size="16px" style="margin-top: 4px;" name="videogame_asset" />
                  </q-item-section>
                  <q-item-section>Switch</q-item-section>
                </q-item>
              </q-list>
            </q-btn-dropdown>
          </div>
          <div @mouseenter="showHelpDropdown" @mouseleave="hideHelpDropdown">
            <q-btn-dropdown :content-class="['no-shadow-menu']" :menu-offset="[20, 22]" flat no-caps class="text-black"
              ref="helpDropdown">
              <template v-slot:label>
                <span class="text-subtitle1">帮助中心</span>
              </template>
              <q-list @mouseenter="showHelpDropdown" @mouseleave="hideHelpDropdown">
                <q-item clickable v-close-popup to="/blog" class="flex items-center">
                  <q-item-section>FAQ</q-item-section>
                </q-item>
                <q-item clickable v-close-popup to="/help" class="flex items-center">
                  <q-item-section>反馈记录</q-item-section>
                </q-item>
              </q-list>
            </q-btn-dropdown>
          </div>
        </div>
        <q-space />
        <div class="row items-center no-wrap q-gutter-md text-black">
          <!-- Search -->
          <q-input 
            borderless
            v-model="searchText" 
            clearable 
            placeholder="搜索游戏..." 
            dense 
            outlined 
            rounded
            style="width: 300px;"
            class="rounded-borders">
            <template v-slot:prepend>
              <q-icon name="search" />
            </template>
          </q-input>

          <!-- Cart -->
          <q-btn flat round class="bg-grey-3 rounded-borders">
            <q-icon name="shopping_cart" />
            <q-badge color="red" floating>1</q-badge>
          </q-btn>

          <!-- Language/Currency -->
          <q-btn flat @click="showLanguageDialog = true" class="bg-grey-3 rounded-borders text-black q-px-md q-py-sm">
            $ {{ selectedCurrency }} I {{ selectedLanguage }}
          </q-btn>

          <!-- Language Selection Dialog -->
          <q-dialog v-model="showLanguageDialog">
            <q-card style="min-width: 300px;">
              <q-card-section>
                <div class="text-h6">选择语言和货币</div>
              </q-card-section>

              <q-card-section>
                <div class="q-mb-md">
                  <div class="text-subtitle2 q-mb-sm">语言</div>
                  <q-select v-model="selectedLanguage" :options="languageOptions" emit-value map-options outlined
                    dense />
                </div>

                <div>
                  <div class="text-subtitle2 q-mb-sm">货币</div>
                  <q-select v-model="selectedCurrency" :options="currencyOptions" emit-value map-options outlined
                    dense />
                </div>
              </q-card-section>

              <q-card-actions align="right">
                <q-btn flat label="取消" color="grey" @click="showLanguageDialog = false" />
                <q-btn flat label="确定" color="primary" @click="confirmSelection" />
              </q-card-actions>
            </q-card>
          </q-dialog>

          <!-- User Profile -->
          <q-avatar class="bg-grey-3 rounded-borders cursor-pointer" size="40px">
            <q-icon name="account_circle" size="32px" />
          </q-avatar>

          

        </div>
      </q-toolbar>
    </q-header>

    <q-page-container>
      <template v-if="$q.platform.is.mobile">
        <router-view v-slot="{ Component }">
          <transition appear enter-active-class="animated fadeIn" leave-active-class="animated fadeOut" mode="out-in">
            <component :is="Component" :key="$route.path" />
          </transition>
        </router-view>
        <!-- 移动端页脚 -->
        <q-footer class="bg-white text-black border-top">
          <q-toolbar class="q-pa-md">
            <div class="row items-center justify-around full-width">
              <q-btn flat class="col text-center" to="/">
                <div class="column items-center">
                  <q-icon name="home" size="24px" />
                  <div class="text-caption q-mt-xs">首页</div>
                </div>
              </q-btn>
              <q-btn flat class="col text-center" to="/games">
                <div class="column items-center">
                  <q-icon name="sports_esports" size="24px" />
                  <div class="text-caption q-mt-xs">游戏</div>
                </div>
              </q-btn>
              <q-btn flat class="col text-center">
                <div class="column items-center">
                  <q-icon name="shopping_cart" size="24px" />
                  <q-badge color="red" floating>1</q-badge>
                  <div class="text-caption q-mt-xs">购物车</div>
                </div>
              </q-btn>
              <q-btn flat class="col text-center" to="/help">
                <div class="column items-center">
                  <q-icon name="help" size="24px" />
                  <div class="text-caption q-mt-xs">帮助</div>
                </div>
              </q-btn>
              <q-btn flat class="col text-center">
                <div class="column items-center">
                  <q-icon name="account_circle" size="24px" />
                  <div class="text-caption q-mt-xs">我的</div>
                </div>
              </q-btn>
            </div>
          </q-toolbar>
        </q-footer>
      </template>
      <template v-else>
        <CustomScrollContainer height="calc(100vh - 80px)" :top-offset="84">
          <router-view v-slot="{ Component }">
            <transition appear enter-active-class="animated fadeIn" leave-active-class="animated fadeOut" mode="out-in">
              <component :is="Component" :key="$route.path" />
            </transition>
          </router-view>
        </CustomScrollContainer>
      </template>
    </q-page-container>

  </q-layout>
</template>

<script setup>
import { ref } from 'vue'
import CustomScrollContainer from 'src/components/CustomScrollContainer.vue'

// 搜索文本
const searchText = ref('')

// 语言选择相关
const showLanguageDialog = ref(false)
const selectedLanguage = ref('EN')
const selectedCurrency = ref('USD')

// 游戏下拉框相关
const gamesDropdown = ref(null)
const helpDropdown = ref(null)
let hideGameTimeout = null
let hideHelpTimeout = null
// 显示游戏下拉框
const showGamesDropdown = () => {
  if (hideGameTimeout) {
    clearTimeout(hideGameTimeout)
    hideGameTimeout = null
  }
  gamesDropdown.value?.show()
}

// 隐藏游戏下拉框（延迟执行）
const hideGamesDropdown = () => {
  hideGameTimeout = setTimeout(() => {
    gamesDropdown.value?.hide()
  }, 200) // 100ms延迟，给用户时间移动到下拉菜单
}


// 显示游戏下拉框
const showHelpDropdown = () => {
  if (hideHelpTimeout) {
    clearTimeout(hideHelpTimeout)
    hideHelpTimeout = null
  }
  helpDropdown.value?.show()
}

// 隐藏游戏下拉框（延迟执行）
const hideHelpDropdown = () => {
  hideHelpTimeout = setTimeout(() => {
    helpDropdown.value?.hide()
  }, 200) // 100ms延迟，给用户时间移动到下拉菜单
}

// 语言选项
const languageOptions = [
  { label: 'English', value: 'EN' },
  { label: '中文', value: 'CN' },
  { label: '日本語', value: 'JP' },
  { label: '한국어', value: 'KR' }
]

// 货币选项
const currencyOptions = [
  { label: 'USD - 美元', value: 'USD' },
  { label: 'CNY - 人民币', value: 'CNY' },
  { label: 'JPY - 日元', value: 'JPY' },
  { label: 'KRW - 韩元', value: 'KRW' },
  { label: 'EUR - 欧元', value: 'EUR' }
]

// 确认选择
const confirmSelection = () => {
  showLanguageDialog.value = false
  // 这里可以添加保存用户选择的逻辑，比如保存到本地存储或发送到服务器
  console.log('Selected Language:', selectedLanguage.value)
  console.log('Selected Currency:', selectedCurrency.value)
}
</script>

<style scoped>
/* :global(.no-shadow-menu) {
  box-shadow: none !important;
  border-radius: 0 0 4px 4px !important;
} */

.text-color-linear {
  background: linear-gradient(90deg, var(--q-primary) 0%, var(--q-secondary) 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}
</style>
