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

export interface RencaiTabBarLayoutProps {
  children: ReactNode
  activeKey: string
}

const rencaiTabBarItems: TabBarItem[] = [
  {
    key: 'index',
    title: '首页',
    iconClass: 'i-heroicons-home-20-solid',
    selectedIconClass: 'i-heroicons-home-20-solid',
  },
  {
    key: 'attendance',
    title: '考勤',
    iconClass: 'i-heroicons-clock-20-solid',
    selectedIconClass: 'i-heroicons-clock-20-solid',
  },
  {
    key: 'personal-info',
    title: '我的',
    iconClass: 'i-heroicons-user-20-solid',
    selectedIconClass: 'i-heroicons-user-20-solid',
  },
  {
    key: 'settings',
    title: '更多',
    iconClass: 'i-heroicons-squares-2x2-20-solid',
    selectedIconClass: 'i-heroicons-squares-2x2-20-solid',
  },
]

export const RencaiTabBarLayout: React.FC<RencaiTabBarLayoutProps> = ({ children, activeKey }) => {
  const handleTabChange = (key: string) => {
    // 使用 Taro 的导航 API 进行页面跳转
    switch (key) {
      case 'index':
        Taro.switchTab({ url: '/pages/index/index' })
        break
      case 'attendance':
        Taro.switchTab({ url: '/pages/attendance/index' })
        break
      case 'personal-info':
        Taro.switchTab({ url: '/pages/personal-info/index' })
        break
      case 'settings':
        Taro.switchTab({ url: '/pages/settings/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={rencaiTabBarItems}
        activeKey={activeKey}
        onChange={handleTabChange}
        fixed={true}
        safeArea={true}
        color="#999"
        selectedColor="#3b82f6"
        backgroundColor="white"
      />
    </View>
  )
}

export default RencaiTabBarLayout
