<template>
  <div class="bottom-nav" :style="{ backgroundImage: 'url(' + backgroundImage + ')' }">
    <!-- 融豆匹配 -->
    <div 
      class="nav-item" 
      :class="{ active: isActive('/matching') }"
      @click="handleNavClick('/matching')"
    >
      <img 
        src="/imgs/bottomnav/rongdoupipei.png"
        alt="融豆匹配" 
        class="nav-image"
        :class="{ 'nav-image-active': isActive('/matching') }"
      />
      <span class="nav-text" :class="{ 'nav-text-active': isActive('/matching') }">融豆匹配</span>
    </div>
    <!-- 融豆明细 -->
    <div 
      class="nav-item" 
      :class="{ active: isActive('/transfers') }"
      @click="handleNavClick('/transfers')"
    >
      <img 
        src="/imgs/bottomnav/rongdoumingxi.png"
        alt="融豆明细" 
        class="nav-image"
        :class="{ 'nav-image-active': isActive('/transfers') }"
      />
      <span class="nav-text" :class="{ 'nav-text-active': isActive('/transfers') }">融豆明细</span>
    </div>
    <!-- 主页 -->
    <div 
      class="nav-item" 
      :class="{ active: isActive('/mainpage') }"
      @click="handleNavClick('/mainpage')"
    >
      <img 
        src="/imgs/bottomnav/zhuye.png"
        alt="主页" 
        class="nav-image"
        :class="{ 'nav-image-active': isActive('/mainpage') }"
      />
      <span class="nav-text" :class="{ 'nav-text-active': isActive('/mainpage') }">主页</span>
    </div>
    <!-- 积分商城 -->
    <div 
      class="nav-item" 
      :class="{ active: isActive('/myshop') }"
      @click="handleNavClick('/myshop')"
    >
      <img 
        src="/imgs/bottomnav/jifenshangcheng.png"
        alt="积分商城" 
        class="nav-image"
        :class="{ 'nav-image-active': isActive('/myshop') }"
      />
      <span class="nav-text" :class="{ 'nav-text-active': isActive('/myshop') }">积分商城</span>
    </div>
    <!-- 个人中心 -->
    <div 
      class="nav-item" 
      :class="{ active: isActive('/myprofile') }"
      @click="handleNavClick('/myprofile')"
    >
      <img 
        src="/imgs/bottomnav/gerenzhongxin.png"
        alt="个人中心" 
        class="nav-image"
        :class="{ 'nav-image-active': isActive('/myprofile') }"
      />
      <span class="nav-text" :class="{ 'nav-text-active': isActive('/myprofile') }">个人中心</span>
    </div>
  </div>
</template>

<script setup>
import { useRouter, useRoute } from 'vue-router'
import { computed } from 'vue'
import { useUserStore } from '@/stores/user'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()

const isActive = (path) => {
  return route.path === path
}

const handleNavClick = (path) => {
  if (!userStore.isAuthenticated) {
    router.push('/mylogin')
    return
  }
  router.push(path)
}

// 根据当前路由计算背景图片
const backgroundImage = computed(() => {
  const currentPath = route.path
  switch (currentPath) {
    case '/matching':
      // return '/imgs/bottomnav/融豆匹配背景.png'
      return '/imgs/bottomnav/rongdoupipei-background.png'
    case '/transfers':
      // return '/imgs/bottomnav/融豆明细背景.png'
      return '/imgs/bottomnav/rongdoumingxi-background.png'
    case '/mainpage':
      // return '/imgs/bottomnav/主页背景.png'
      return '/imgs/bottomnav/zhuye-background.png'
    case '/myshop':
      // return '/imgs/bottomnav/积分商城背景.png'
      return '/imgs/bottomnav/jifenshangcheng-background.png'
    case '/myprofile':
      // return '/imgs/bottomnav/个人中心背景.png'
      return '/imgs/bottomnav/gerenzhongxin-background.png'
    default:
      // return '/imgs/bottomnav/默认背景.png'
      return '/imgs/bottomnav/moren-background.png'
  }
})
</script>

<style scoped>
.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  display: flex;
  justify-content: space-around;
  align-items: center;
  padding: 20px 0;
  z-index: 1000;
  padding-bottom: env(safe-area-inset-bottom);
  height: 80px;
}

.nav-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.3s ease;
  min-width: 60px;
  height: 60px;
}

.nav-image {
  width: 50px;
  height: 50px;
  object-fit: contain;
  transition: all 0.3s ease;
}

.nav-image-active {
  transform: translateY(-50px);
}

.nav-text {
  font-size: 12px;
  color: white;
  margin-top: 4px;
  transition: all 0.3s ease;
}

.nav-text-active {
  color: white;
  font-weight: bold;
}

/* 最左边小球向右偏移 */
.nav-item:first-child {
  transform: translateX(10px);
}

/* 最右边小球向左偏移 */
.nav-item:last-child {
  transform: translateX(-10px);
}

/* 移动端适配 */
@media (max-width: 768px) {
  .bottom-nav {
    padding: 15px 0;
    padding-bottom: max(15px, env(safe-area-inset-bottom));
    height: 70px;
  }
  
  .nav-item {
    min-width: 50px;
    height: 50px;
  }
  
  .nav-image {
    width: 45px;
    height: 45px;
  }
  
  .nav-image-active {
    transform: translateY(-40px);
  }
  
  .nav-text {
    font-size: 11px;
  }
}

@media (max-width: 480px) {
  .bottom-nav {
    padding: 10px 0;
    padding-bottom: max(10px, env(safe-area-inset-bottom));
    height: 60px;
  }
  
  .nav-item {
    min-width: 45px;
    height: 45px;
  }
  
  .nav-image {
    width: 40px;
    height: 40px;
  }
  
  .nav-image-active {
    transform: translateY(-20px);
  }
  
  .nav-text {
    font-size: 10px;
  }
}

/* 横屏适配 */
@media (max-height: 500px) and (orientation: landscape) {
  .bottom-nav {
    padding: 8px 0;
    height: 50px;
  }
  
  .nav-item {
    height: 40px;
  }
  
  .nav-image {
    width: 35px;
    height: 35px;
  }
  
  .nav-image-active {
    transform: translateY(-30px);
  }
  
  .nav-text {
    font-size: 9px;
  }
}
</style>