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
  className?: string
}

const tabBarItems: TabBarItem[] = [
  {
    key: 'home',
    title: '首页',
    iconClass: 'i-heroicons-home-20-solid',
    selectedIconClass: 'i-heroicons-home-20-solid',
  },
  {
    key: 'orders',
    title: '订单',
    iconClass: 'i-heroicons-clipboard-document-list-20-solid',
    selectedIconClass: 'i-heroicons-clipboard-document-list-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, className = '' }) => {
  const handleTabChange = (key: string) => {
    // 使用 Taro 的导航 API 进行页面跳转
    switch (key) {
      case 'home':
        Taro.switchTab({ url: '/pages/home/index' })
        break
      case 'orders':
        Taro.switchTab({ url: '/pages/orders/index' })
        break
      case 'profile':
        Taro.switchTab({ url: '/pages/profile/index' })
        break
      default:
        break
    }
  }

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

export default TabBarLayout