<script lang="ts" setup>
import icon2 from '@/static/tab/index1.png'
import icon1 from '@/static/tab/index2.png'

const router = useRouter()
const route = useRoute()
const { opcity } = storeToRefs(useSysStore())

const { activeTabbar, getTabbarItemValue, setTabbarItemActive, tabbarList } = useTabbar()

function handleTabbarChange({ value }: { value: string }) {
  setTabbarItemActive(value)
  router.pushTab({ name: value })
}

onMounted(() => {
  // #ifdef APP-PLUS
  uni.hideTabBar()
  // #endif
  nextTick(() => {
    if (route.name && route.name !== activeTabbar.value.name) {
      setTabbarItemActive(route.name)
    }
  })
})
function handleClick() {
  uni.pageScrollTo({
    duration: 50,
    scrollTop: 0,
  })
}
</script>

<script lang="ts">
export default {
  options: {
    addGlobalClass: true,
    virtualHost: true,
    styleIsolation: 'shared',
  },
}
</script>

<template>
  <slot />
  <view class="page-tab">
    <wd-tabbar
      :model-value="activeTabbar.name" safe-area-inset-bottom placeholder fixed :bordered="false"
      custom-class="custom-tab" :custom-style="`box-shadow:${activeTabbar.name == 'smqjh-cart' ? '' : ' 0rpx -6rpx 12rpx 2rpx rgba(0, 0, 0, 0.09)'}`"
      @change="handleTabbarChange"
    >
      <wd-tabbar-item
        v-for="(item, index) in tabbarList" :key="index" :name="item.name"
        :value="getTabbarItemValue(item.name)" :title="index == 0 && item.active ? '' : item.title"
      >
        <template #icon="{ active }">
          <template v-if="index == 0 && !active">
            <image src="@/static/tab/index.png" class="h44rpx w44rpx" />
          </template>
          <template v-else-if="index == 0 && active">
            <image v-if="opcity == 1" :src="icon1" class="h76rpx w76rpx" @click="handleClick" />
            <image v-else :src="icon2" class="h76rpx w76rpx" />
          </template>
          <template v-else-if="index != 0">
            <image v-if="active" :src="item.icon2" class="h44rpx w44rpx" />
            <image v-else :src="item.icon1" class="h44rpx w44rpx" />
          </template>
        </template>
      </wd-tabbar-item>
    </wd-tabbar>
  </view>
</template>

<style scoped lang="scss">
.page-tab {
  :deep() {
    .custom-tab {
      border-radius: 28rpx 28rpx 0 0;
    }
  }
}
</style>
