import { View, Button, Text } from '@tarojs/components'
import Taro from '@tarojs/taro'
import { distributorRegister } from '../../api'
import './register.scss'

export default function Register() {
  const handleRegister = async () => {
    try {
      await distributorRegister({})
      Taro.showToast({ title: '申请已提交', icon: 'success' })
      setTimeout(() => {
        Taro.redirectTo({ url: '/subpackages/distributor/index' })
      }, 1500)
    } catch (err: any) {
      if (err.data?.error === 'Already registered') {
         Taro.redirectTo({ url: '/subpackages/distributor/index' })
      } else {
         Taro.showToast({ title: '申请失败', icon: 'none' })
      }
    }
  }

  return (
    <View className='page-container'>
       <View className='card'>
          <Text className='title'>加入我们</Text>
          <Text className='desc'>成为分销员，分享赚取佣金</Text>
          <Button className='btn-register' onClick={handleRegister}>立即申请</Button>
       </View>
    </View>
  )
}
