<template>
  <view class="tab-bar">
    <view
      v-for="(item, index) in tabs"
      :key="item.pagePath"
      class="tab-bar-item"
      :class="{ active: selected === index }"
      @click="switchTab(item.pagePath)"
    >
      <image
        v-if="item.icon"
        class="tab-icon"
        :src="selected === index ? item.activeIcon : item.icon"
        mode="aspectFit"
      />
      <view v-else class="tab-icon tab-icon-text">{{ item.iconText }}</view>
      <view class="tab-text">{{ item.text }}</view>
      <view v-if="selected === index" class="tab-active-bar"></view>
    </view>
  </view>
</template>

<script setup lang="ts">
import { computed, onMounted } from 'vue'
import { useAuthStore } from '@/stores/auth'
import { safeHideTabBar } from '@/utils'

interface TabItem {
  pagePath: string
  text: string
  icon: string
  activeIcon: string
  iconText?: string
}

const commonTabs: TabItem[] = [
  { pagePath: 'pages/home/home', text: '首页', icon: '/static/tabbar/home.png', activeIcon: '/static/tabbar/home-active.png' },
  { pagePath: 'pages/task/task', text: '任务', icon: '/static/tabbar/task.png', activeIcon: '/static/tabbar/task-active.png' },
  { pagePath: 'pages/my/my', text: '我的', icon: '/static/tabbar/my.png', activeIcon: '/static/tabbar/my-active.png' },
]

const dispatchTabs: TabItem[] = [
  { pagePath: 'pages/home/home', text: '首页', icon: '/static/tabbar/home.png', activeIcon: '/static/tabbar/home-active.png' },
  { pagePath: 'pages/schedule/schedule', text: '排班', icon: '/static/tabbar/schedule.png', activeIcon: '/static/tabbar/schedule-active.png' },
  { pagePath: 'pages/task/task', text: '任务', icon: '/static/tabbar/task.png', activeIcon: '/static/tabbar/task-active.png' },
  { pagePath: 'pages/my/my', text: '我的', icon: '/static/tabbar/my.png', activeIcon: '/static/tabbar/my-active.png' },
]

const constructionTabs: TabItem[] = [
  { pagePath: 'pages/home/home', text: '首页', icon: '/static/tabbar/home.png', activeIcon: '/static/tabbar/home-active.png' },
  { pagePath: 'pages/task/task', text: '任务', icon: '/static/tabbar/task.png', activeIcon: '/static/tabbar/task-active.png' },
  { pagePath: 'pages/my/my', text: '我的', icon: '/static/tabbar/my.png', activeIcon: '/static/tabbar/my-active.png' },
]

const authStore = useAuthStore()
const role = computed(() => {
  const endpointMap: Record<string, 'sales' | 'dispatch' | 'construction'> = {
    'mp-sales': 'sales',
    'mp-dispatch': 'dispatch',
    'mp-construction': 'construction',
  }
  return authStore.userRole || endpointMap[authStore.userMpEndpoint || ''] || 'sales'
})

const tabs = computed<TabItem[]>(() => {
  if (role.value === 'dispatch') return dispatchTabs
  if (role.value === 'construction') return constructionTabs
  return commonTabs
})

const selected = computed(() => {
  const pages = getCurrentPages()
  const route = pages[pages.length - 1]?.route || ''
  return tabs.value.findIndex((t) => t.pagePath === route)
})

function switchTab(pagePath: string) {
  const url = '/' + pagePath
  // #ifdef APP-PLUS
  // App 端未配置原生 tabBar，使用 reLaunch 模拟切换
  uni.reLaunch({ url })
  // #endif
  // #ifndef APP-PLUS
  uni.switchTab({ url })
  // #endif
}

onMounted(() => {
  // H5/App 默认 tabBar 会覆盖自定义组件，强制隐藏
  // #ifdef H5 || APP-PLUS
  safeHideTabBar({ animation: false })
  // #endif
})
</script>

<style scoped>
.tab-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  min-height: 50px;
  padding-bottom: env(safe-area-inset-bottom);
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  display: flex;
  justify-content: space-around;
  align-items: center;
  border-top: 1px solid rgba(164, 216, 152, 0.4);
  z-index: 9999;
}

.tab-bar-item {
  position: relative;
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 50px;
  color: #709484;
}

.tab-bar-item.active {
  color: #368f6f;
}

.tab-icon {
  width: 24px;
  height: 24px;
  margin-bottom: 2px;
}

.tab-icon-text {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  line-height: 24px;
}

.tab-text {
  font-size: 10px;
  line-height: 14px;
  font-weight: 500;
}

.tab-bar-item.active .tab-text {
  font-weight: 600;
}

.tab-active-bar {
  position: absolute;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 22px;
  height: 3px;
  border-radius: 2px;
  background: linear-gradient(135deg, #368f6f 0%, #5ab8d0 100%);
}
</style>
