import React, { ReactNode } from 'react'
import { View } from '@tarojs/components'
import { TabBar, TabBarItem } from '@/components/ui/tab-bar'
import Taro from '@tarojs/taro'

export interface TabBarLayoutProps {
  children: ReactNode
  activeKey: string
}

const tabBarItems: TabBarItem[] = [
  {
    key: 'home',
    title: '首页',
    iconClass: 'i-heroicons-home-20-solid',
    selectedIconClass: 'i-heroicons-home-20-solid',
  },
  {
    key: 'explore',
    title: '发现',
    iconClass: 'i-heroicons-magnifying-glass-20-solid',
    selectedIconClass: 'i-heroicons-magnifying-glass-20-solid',
  },
  {
    key: 'profile',
    title: '我的',
    iconClass: 'i-heroicons-user-20-solid',
    selectedIconClass: 'i-heroicons-user-20-solid',
  },
]

export const TabBarLayout: React.FC<TabBarLayoutProps> = ({ children, activeKey }) => {
  const handleTabChange = (key: string) => {
    // 使用 Taro 的导航 API 进行页面跳转
    switch (key) {
      case 'home':
        Taro.switchTab({ url: '/pages/index/index' })
        break
      case 'explore':
        Taro.switchTab({ url: '/pages/explore/index' })
        break
      case 'profile':
        Taro.switchTab({ url: '/pages/profile/index' })
        break
      default:
        break
    }
  }

  return (
    <View className="min-h-screen bg-gray-50 flex flex-col">
      <View className="flex-1 flex flex-col">
        {children}
      </View>
      <TabBar
        items={tabBarItems}
        activeKey={activeKey}
        onChange={handleTabChange}
        fixed={true}
        safeArea={true}
      />
    </View>
  )
}

export default TabBarLayout