import React, { useState, useEffect } from 'react'
import { View, Text, ScrollView } from '@tarojs/components'
import Taro from '@tarojs/taro'
import { useUser } from '@/hooks/useUser'
import { useScrollHeight } from '@/hooks/useScrollHeight'

definePageConfig({
  navigationBarTitleText: '会员中心',
})

const MemberPage: React.FC = () => {
  const { user, isLoggedIn } = useUser()
  const [memberLevel, setMemberLevel] = useState('普通会员')
  const [commission, setCommission] = useState(0)
  const [totalCommission, setTotalCommission] = useState(0)
  const scrollHeight = useScrollHeight(50)


  useEffect(() => {
    if (!isLoggedIn) {
      Taro.navigateTo({ url: '/passport/login' })
    }
  }, [isLoggedIn])

  const memberFeatures = [
    { icon: '💰', label: '我的佣金', value: `¥${commission}`, url: '/pages/user/commission' },
    { icon: '📊', label: '佣金明细', value: '', url: '/pages/user/commission-detail' },
    { icon: '👥', label: '我的团队', value: '', url: '/pages/user/team' },
    { icon: '📋', label: '邀请记录', value: '', url: '/pages/user/invite-record' },
  ]

  const memberBenefits = [
    '享受会员专属折扣',
    '获得分销佣金权益',
    '优先客服支持',
    '专属活动邀请',
  ]

  const handleUpgrade = () => {
    Taro.navigateTo({ url: '/pages/user/member-upgrade' })
  }

  const handleFeatureClick = (url: string) => {
    Taro.navigateTo({ url })
  }

  if (!isLoggedIn) {
    return null
  }

  return (
    <View className='min-h-screen bg-gray-50'>
      <ScrollView scrollY style={{ height: scrollHeight }}>
        {/* 会员卡片 */}
        <View className='mx-3 mt-3 p-4 rounded-xl' style={{ background: 'linear-gradient(135deg, #667eea 0%, #764ba2 100%)' }}>
          <View className='flex items-center justify-between mb-3'>
            <View>
              <Text className='text-white text-lg font-bold'>{user?.nickname || '用户'}</Text>
              <Text className='text-white text-sm opacity-80 block mt-1'>{memberLevel}</Text>
            </View>
            <View
              className='px-3 py-1 rounded-full'
              style={{ backgroundColor: 'rgba(255,255,255,0.3)' }}
              onClick={handleUpgrade}
            >
              <Text className='text-white text-sm'>升级会员</Text>
            </View>
          </View>
          <View className='flex justify-around'>
            <View className='text-center'>
              <Text className='text-white text-xl font-bold block'>{totalCommission}</Text>
              <Text className='text-white text-xs opacity-80'>累计佣金</Text>
            </View>
            <View className='text-center'>
              <Text className='text-white text-xl font-bold block'>{commission}</Text>
              <Text className='text-white text-xs opacity-80'>可提现</Text>
            </View>
          </View>
        </View>

        {/* 会员功能 */}
        <View className='bg-white mx-3 mt-3 rounded-lg overflow-hidden'>
          {memberFeatures.map((item, idx) => (
            <View
              key={item.label}
              className={`flex items-center justify-between px-4 py-3 ${
                idx < memberFeatures.length - 1 ? 'border-b border-gray-50' : ''
              }`}
              onClick={() => handleFeatureClick(item.url)}
            >
              <View className='flex items-center gap-3'>
                <Text className='text-xl'>{item.icon}</Text>
                <Text className='text-sm text-gray-700'>{item.label}</Text>
              </View>
              <View className='flex items-center gap-2'>
                {item.value && (
                  <Text className='text-sm font-medium text-orange-500'>{item.value}</Text>
                )}
                <Text className='text-gray-300 text-sm'>{'>'}</Text>
              </View>
            </View>
          ))}
        </View>

        {/* 会员权益说明 */}
        <View className='bg-white mx-3 mt-3 p-4 rounded-lg'>
          <Text className='text-base font-medium text-gray-800 mb-3 block'>会员权益</Text>
          {memberBenefits.map((benefit, idx) => (
            <View key={idx} className='flex items-center gap-2 mb-2'>
              <Text className='text-green-500'>✓</Text>
              <Text className='text-sm text-gray-600'>{benefit}</Text>
            </View>
          ))}
        </View>

        {/* 分销推广 */}
        <View className='bg-white mx-3 mt-3 p-4 rounded-lg'>
          <Text className='text-base font-medium text-gray-800 mb-3 block'>分销推广</Text>
          <View className='bg-orange-50 rounded-lg p-3'>
            <Text className='text-sm text-orange-600 mb-1 block'>推广赚佣金</Text>
            <Text className='text-xs text-gray-500 mb-2 block'>邀请好友注册消费，获得佣金分成</Text>
            <View
              className='bg-orange-500 rounded-full py-2 text-center'
              onClick={() => Taro.navigateTo({ url: '/pages/user/promotion' })}
            >
              <Text className='text-white text-sm'>立即推广</Text>
            </View>
          </View>
        </View>

        <View className='h-6' />
      </ScrollView>
    </View>
  )
}

export default MemberPage
