import React from 'react'
import {View, Text} from '@tarojs/components'
import {ConfigProvider, Button, Grid, Avatar} from '@nutui/nutui-react-taro'
import {
  User,
  Shopping
} from '@nutui/icons-react-taro'
import {useDealerUser} from '@/hooks/useDealerUser'
import { useThemeStyles } from '@/hooks/useTheme'
import {gradientUtils} from '@/styles/gradients'
import Taro from '@tarojs/taro'

const DealerIndex: React.FC = () => {
  const {
    dealerUser,
    error,
    refresh,
  } = useDealerUser()

  // 使用主题样式
  const themeStyles = useThemeStyles()

  // 导航到各个功能页面
  const navigateToPage = (url: string) => {
    Taro.navigateTo({url})
  }

  // 格式化金额
  // const formatMoney = (money?: string) => {
  //   if (!money) return '0.00'
  //   return parseFloat(money).toFixed(2)
  // }

  // 格式化时间
  const formatTime = (time?: string) => {
    if (!time) return '-'
    return new Date(time).toLocaleDateString()
  }

  // 获取用户主题
  const userTheme = gradientUtils.getThemeByUserId(dealerUser?.userId)

  // 获取渐变背景
  const getGradientBackground = (themeColor?: string) => {
    if (themeColor) {
      const darkerColor = gradientUtils.adjustColorBrightness(themeColor, -30)
      return gradientUtils.createGradient(themeColor, darkerColor)
    }
    return userTheme.background
  }

  console.log(getGradientBackground(),'getGradientBackground()')

  if (error) {
    return (
      <View className="p-4">
        <View className="bg-red-50 border border-red-200 rounded-lg p-4 mb-4">
          <Text className="text-red-600">{error}</Text>
        </View>
        <Button type="primary" onClick={refresh}>
          重试
        </Button>
      </View>
    )
  }

  return (
    <View className="bg-gray-100 min-h-screen">
      <View>
        {/*头部信息*/}
        {dealerUser && (
          <View className="px-4 py-6 relative overflow-hidden" style={themeStyles.primaryBackground}>
            {/* 装饰性背景元素 - 小程序兼容版本 */}
            <View className="absolute w-32 h-32 rounded-full" style={{
              backgroundColor: 'rgba(255, 255, 255, 0.1)',
              top: '-16px',
              right: '-16px'
            }}></View>
            <View className="absolute w-24 h-24 rounded-full" style={{
              backgroundColor: 'rgba(255, 255, 255, 0.08)',
              bottom: '-12px',
              left: '-12px'
            }}></View>
            <View className="absolute w-16 h-16 rounded-full" style={{
              backgroundColor: 'rgba(255, 255, 255, 0.05)',
              top: '60px',
              left: '120px'
            }}></View>
            <View className="flex items-center justify-between relative z-10 mb-4">
              <Avatar
                size="50"
                src={dealerUser?.qrcode}
                icon={<User/>}
                className="mr-4"
                style={{
                  border: '2px solid rgba(255, 255, 255, 0.3)'
                }}
              />
              <View className="flex-1 flex-col">
                <View className="text-white text-lg font-bold mb-1" style={{
                }}>
                  {dealerUser?.realName || '分销商'}
                </View>
                <View className="text-sm" style={{
                  color: 'rgba(255, 255, 255, 0.8)'
                }}>
                  {dealerUser.mobile}
                </View>
              </View>
              <View className="text-right hidden">
                <Text className="text-xs" style={{
                  color: 'rgba(255, 255, 255, 0.9)'
                }}>加入时间</Text>
                <Text className="text-xs" style={{
                  color: 'rgba(255, 255, 255, 0.7)'
                }}>
                  {formatTime(dealerUser.createTime)}
                </Text>
              </View>
            </View>
          </View>
        )}

        {/* 功能导航 */}
        <View className="bg-white mx-4 mt-4 rounded-xl p-4">
          <View className="font-semibold mb-4 text-gray-800">管理工具</View>
          <ConfigProvider>
            <Grid
              columns={4}
              className="no-border-grid"
              style={{
                '--nutui-grid-border-color': 'transparent',
                '--nutui-grid-item-border-width': '0px',
                border: 'none'
              } as React.CSSProperties}
            >
              <Grid.Item text="成员管理" onClick={() => navigateToPage('/admin/users/index')}>
                <View className="text-center">
                  <View className="w-12 h-12 bg-blue-50 rounded-xl flex items-center justify-center mx-auto mb-2">
                    <Shopping color="#3b82f6" size="20"/>
                  </View>
                </View>
              </Grid.Item>

            </Grid>

          </ConfigProvider>
        </View>
      </View>

      {/* 底部安全区域 */}
      <View className="h-20"></View>
    </View>
  )
}

export default DealerIndex
