<template>
  <view class="custom-tab-bar">
    <view class="tab-bar-content">
      <view
        v-for="(tab, index) in tabs"
        :key="index"
        class="tab-item"
        :class="{ 'tab-active': currentTab === tab.pagePath }"
        @tap="switchTab(tab.pagePath)"
      >
        <image
          :src="currentTab === tab.pagePath ? tab.selectedIconPath : tab.iconPath"
          class="tab-icon"
        ></image>
        <text class="tab-text" :class="{ 'text-active': currentTab === tab.pagePath }">
          {{ tab.text }}
        </text>
      </view>
    </view>
  </view>
</template>

<script setup lang="ts">
import i18n, { t } from '@/locale/index'

const props = withDefaults(
  defineProps<{
    currentTab: string
  }>(),
  {
    currentTab: 'pages/index/index',
  },
)

interface TabItem {
  pagePath: string
  iconPath: string
  selectedIconPath: string
  text: string
}

const tabs = ref<TabItem[]>([])

const updateLists = () => {
  tabs.value = [
    {
      pagePath: 'pages/index/index',
      text: t('tabbar.Home'),
      iconPath: '/static/images/tabbar/home.png',
      selectedIconPath: '/static/images/tabbar/home-l.png',
    },
    {
      pagePath: 'pages/trophy/index',
      text: t('tabbar.Competition'),
      iconPath: '/static/images/tabbar/jiangpai.png',
      selectedIconPath: '/static/images/tabbar/jiangpai-l.png',
    },
    {
      pagePath: 'pages/team/index',
      text: t('tabbar.Team'),
      iconPath: '/static/images/tabbar/team.png',
      selectedIconPath: '/static/images/tabbar/team-l.png',
    },
    {
      pagePath: 'pages/task/index',
      text: t('tabbar.Earn'),
      iconPath: '/static/images/tabbar/task.png',
      selectedIconPath: '/static/images/tabbar/task-l.png',
    },
  ]
}

onMounted(() => {
  updateLists()
})

const switchTab = (pagePath: string) => {
  uni.switchTab({
    url: `/${pagePath}`,
  })
}

watch(() => i18n.global.locale, updateLists)
</script>

<style scoped>
.custom-tab-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 98rpx; /* 调整为更准确的高度 */
  font-family: 'Source Han Sans CN', serif;
}

.tab-bar-content {
  display: flex;
  align-items: center;
  width: 100%;
  height: 100%;
  background-color: #000000; /* 改为黑色背景 */
}

.tab-item {
  display: flex;
  flex: 1;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  padding: 12rpx 0; /* 调整内边距 */
}

.tab-icon {
  width: 42rpx; /* 调整图标大小 */
  height: 42rpx;
  margin-bottom: 8rpx; /* 调整图标和文字间距 */
}

.tab-text {
  font-size: 22rpx; /* 调整字体大小 */
  line-height: 1;
  color: var(--default-color); /* 调整未选中颜色 */
}

.text-active {
  color: var(--primary-color); /* 保持选中颜色 */
}

/* 移除选中时的背景色变化 */
.tab-active {
  background: none;
}

/* 添加顶部边框 */
.tab-bar-content::after {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 1px;
  content: '';
  background-color: rgba(255, 255, 255, 0.1); /* 添加微弱的顶部边框 */
}
</style>
