<!-- 代码已包含 CSS：使用 TailwindCSS , 安装 TailwindCSS 后方可看到布局样式效果 -->

<template>
  <view class="tab-bar">
    <view
      v-for="(item, index) in tabBar.list"
      :key="index"
      :class="['tab-item', { active: currentTab === index }]"
      @click="selectTabBar(index, item.pagePath)"
    >
      <view class="icon-wrapper">
        <image v-if="currentTab === index" :src="item.iconShow" class="home-item-isShow"></image>
        <image v-else :src="item.icon" class="home-item-img"></image>
      </view>
      <span class="label">{{ item.text }}</span>
    </view>
  </view>
</template>

<script lang="ts" setup>
  import { tabBar } from '@/pages.json'
  import { t } from '@/locale'
  import { useTabbarStore } from '@/store/tabbar'
  import { ref } from 'vue'

  const tabbarStore = useTabbarStore()
  const currentTab = ref(0)

  function selectTabBar(index: number, url: string) {
    currentTab.value = index
    tabbarStore.setCurIdx(index)
    uni.switchTab({ url })
  }

  onLoad(() => {
    // #ifdef APP-PLUS | H5
    uni.hideTabBar({
      fail(err) {
        console.log('hideTabBar fail: ', err)
      },
      success(res) {
        console.log('hideTabBar success: ', res)
      },
    })
    const routes = getCurrentPages()
    const curRoute = routes[routes.length - 1].route
    for (let i = 0; i < tabBar.list.length; i++) {
      const { pagePath } = tabBar.list[i]
      if (pagePath === curRoute) {
        tabbarStore.setCurIdx(i)
        break
      }
    }

    // #endif
  })
</script>

<style scoped>
  .tab-bar {
    position: fixed;
    right: 0;
    bottom: 0;
    left: 0;
    display: flex;
    align-items: center;
    justify-content: space-around;
    width: 100%;
    height: 80px;
    background: #ffffff;
    box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.05);
  }

  .tab-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 80px;
    padding: 6px 0;
    color: #999999;
    cursor: pointer;
    transition: color 0.3s ease;
  }

  .icon-wrapper {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 45px;
    height: 45px;
    border-radius: 50%;
    transition: all 0.3s ease;
  }

  .tab-item.active {
    color: #60a5fa;
  }

  .tab-item.active .icon-wrapper {
    /* background: #60a5fa; */
    color: #ffffff;
    transform: translateY(-18px);
  }

  .label {
    margin-top: 3px;
    font-size: 12px;
    transition: all 0.3s ease;
  }

  .tab-item.active .label {
    transform: translateY(-8px);
  }

  .home-item-img {
    width: 1.25rem;
    height: 1.25rem;
    object-fit: contain;
  }
</style>
