import React from 'react'
import { View, Text, Image, ScrollView } from '@tarojs/components'
import Taro from '@tarojs/taro'
import { useUser } from '@/hooks/useUser'
import MemberBadge from '@/components/business/MemberBadge'

const UserPage: React.FC = () => {
  const { user, isLoggedIn } = useUser()

  // TabBar 页面列表
  const tabBarPages = ['/pages/index/index', '/pages/shop/index', '/pages/order/list', '/pages/user/user']

  const handleNavigate = (url: string) => {
    if (tabBarPages.includes(url)) {
      Taro.switchTab({ url })
    } else {
      Taro.navigateTo({ url })
    }
  }

  const menuItems = [
    { icon: '📦', label: '我的订单', url: '/pages/order/list' },
    { icon: '📅', label: '预约订单', url: '/pages/booking/list' },
    { icon: '🏆', label: '赛事活动', url: '/pages/event/my/index' },
    { icon: '🎫', label: '优惠券', url: '/pages/user/coupon-list' },
    { icon: '💰', label: '我的钱包', url: '/pages/user/wallet' },
    { icon: '📍', label: '收货地址', url: '/pages/user/address-list' },
    { icon: '⭐', label: '积分明细', url: '/pages/user/points-record' },
    { icon: '🔔', label: '消息通知', url: '/pages/index/notification' },
    { icon: '⚙️', label: '设置', url: '/pages/user/setting' },
  ]

  const handleLogin = () => {
    if (!isLoggedIn) {
      Taro.navigateTo({ url: '/passport/login' })
    }
  }

  return (
    <View className='min-h-screen bg-gray-100 flex flex-col'>
      <ScrollView scrollY className='flex-1'>
        {/* 顶部渐变背景区域 */}
        <View className='relative'>
          {/* 渐变背景 */}
          <View
            className='absolute inset-0'
            style={{
              background: 'linear-gradient(135deg, #667eea 0%, #764ba2 50%, #f093fb 100%)',
            }}
          />
          {/* 装饰圆形 */}
          <View
            className='absolute'
            style={{
              width: '200px',
              height: '200px',
              borderRadius: '100px',
              background: 'rgba(255, 255, 255, 0.1)',
              top: '-80px',
              right: '-60px',
            }}
          />
          <View
            className='absolute'
            style={{
              width: '150px',
              height: '150px',
              borderRadius: '75px',
              background: 'rgba(255, 255, 255, 0.08)',
              top: '-40px',
              left: '-40px',
            }}
          />

          {/* 用户信息卡片 */}
          <View className='relative mx-3 mt-3 p-4 rounded-2xl' style={{
            background: 'rgba(255, 255, 255, 0.95)',
            boxShadow: '0 8px 32px rgba(0, 0, 0, 0.1)',
          }}>
            <View className='flex items-center gap-4' onClick={handleLogin}>
              {/* 头像区域 */}
              <View className='relative'>
                {isLoggedIn && user?.avatar ? (
                  <Image
                    className='w-16 h-16 rounded-full border-4 border-white'
                    style={{ boxShadow: '0 4px 12px rgba(0, 0, 0, 0.15)' }}
                    src={user.avatar}
                    mode='aspectFill'
                  />
                ) : (
                  <View
                    className='w-16 h-16 rounded-full border-4 border-white flex items-center justify-center'
                    style={{ background: 'linear-gradient(135deg, #667eea, #764ba2)', boxShadow: '0 4px 12px rgba(0, 0, 0, 0.15)' }}
                  >
                    <Text className='text-2xl text-white'>👤</Text>
                  </View>
                )}
                {/* VIP标识 */}
                {isLoggedIn && user?.memberLevelName && (
                  <View
                    className='absolute -bottom-1 -right-1 px-1 py-1 rounded text-xs text-white'
                    style={{ background: 'linear-gradient(135deg, #f093fb, #f5576c)' }}
                  >
                    VIP
                  </View>
                )}
              </View>

              {/* 用户信息 */}
              <View className='flex-1'>
                <View className='flex items-center gap-2'>
                  <Text className='text-lg font-bold text-gray-800'>
                    {isLoggedIn ? (user?.nickname || user?.phone || '用户') : '点击登录'}
                  </Text>
                  {isLoggedIn && user?.memberLevelName && (
                    <MemberBadge levelName={user.memberLevelName} />
                  )}
                </View>
                {isLoggedIn ? (
                  <Text className='text-xs text-gray-400 mt-1'>ID: {(user as any)?.id || '暂无'}</Text>
                ) : (
                  <Text className='text-xs text-gray-400 mt-1'>登录后享受更多服务</Text>
                )}
              </View>

              {/* 箭头 */}
              <View className='w-6 h-6 rounded-full bg-gray-100 flex items-center justify-center'>
                <Text className='text-gray-400 text-xs'>{'>'}</Text>
              </View>
            </View>

            {/* 数据概览 */}
            {isLoggedIn && (
              <View
                className='grid grid-cols-3 gap-2 mt-4 pt-4 rounded-xl'
                style={{ background: 'rgba(102, 126, 234, 0.05)' }}
              >
                <View
                  className='text-center py-2 rounded-lg'
                  onClick={() => Taro.switchTab({ url: '/pages/points/index' })}
                >
                  <Text className='text-xl font-bold text-transparent' style={{ background: 'linear-gradient(135deg, #667eea, #764ba2)', WebkitBackgroundClip: 'text', WebkitTextFillColor: 'transparent' }}>
                    {(user as any)?.points || 0}
                  </Text>
                  <Text className='text-xs text-gray-500 mt-1'>积分</Text>
                </View>
                <View
                  className='text-center py-2 rounded-lg'
                  onClick={() => Taro.navigateTo({ url: '/pages/user/coupon-list' })}
                >
                  <Text className='text-xl font-bold text-transparent' style={{ background: 'linear-gradient(135deg, #f093fb, #f5576c)', WebkitBackgroundClip: 'text', WebkitTextFillColor: 'transparent' }}>
                    0
                  </Text>
                  <Text className='text-xs text-gray-500 mt-1'>优惠券</Text>
                </View>
                <View
                  className='text-center py-2 rounded-lg'
                  onClick={() => Taro.switchTab({ url: '/pages/order/list' })}
                >
                  <Text className='text-xl font-bold text-transparent' style={{ background: 'linear-gradient(135deg, #4facfe, #00f2fe)', WebkitBackgroundClip: 'text', WebkitTextFillColor: 'transparent' }}>
                    0
                  </Text>
                  <Text className='text-xs text-gray-500 mt-1'>订单</Text>
                </View>
              </View>
            )}
          </View>

          </View>

        {/* 我的订单快捷入口 */}
        <View className='bg-white rounded-xl mx-3 mt-3 p-4' style={{ boxShadow: '0 2px 8px rgba(0, 0, 0, 0.05)' }}>
          <View className='flex justify-between items-center mb-3'>
            <Text className='text-base font-medium text-gray-800'>我的订单</Text>
            <Text className='text-xs text-gray-400' onClick={() => Taro.switchTab({ url: '/pages/order/list' })}>
              全部订单 {'>'}
            </Text>
          </View>
          <View className='grid grid-cols-4 gap-2'>
            {[
              { icon: '💳', label: '待付款', status: 0, color: '#f093fb' },
              { icon: '📦', label: '待发货', status: 1, color: '#667eea' },
              { icon: '🚚', label: '待收货', status: 2, color: '#4facfe' },
              { icon: '✅', label: '已完成', status: 3, color: '#52c41a' },
            ].map(item => (
              <View
                key={item.status}
                className='flex flex-col items-center py-2 rounded-lg'
                style={{ background: `${item.color}10` }}
                onClick={() => { Taro.setStorageSync('order_tab', item.status); Taro.switchTab({ url: '/pages/order/list' }) }}
              >
                <Text className='text-xl mb-1'>{item.icon}</Text>
                <Text className='text-xs text-gray-600'>{item.label}</Text>
              </View>
            ))}
          </View>
        </View>
        {/* 功能菜单 */}
        <View className='bg-white rounded-xl mx-3 mt-3 overflow-hidden' style={{ boxShadow: '0 2px 8px rgba(0, 0, 0, 0.05)' }}>
          {menuItems.map((item, idx) => (
            <View
              key={item.label}
              className={`flex items-center justify-between px-4 py-3 ${
                idx < menuItems.length - 1 ? 'border-b border-gray-50' : ''
              }`}
              onClick={() => handleNavigate(item.url)}
            >
              <View className='flex items-center gap-3'>
                <Text className='text-base'>{item.icon}</Text>
                <Text className='text-sm text-gray-700'>{item.label}</Text>
              </View>
              <Text className='text-gray-300 text-sm'>{'>'}</Text>
            </View>
          ))}
        </View>
        {/* 底部安全区域 */}
        <View className='h-20' />
      </ScrollView>
    </View>
  )
}

export default UserPage
