import { useState } from '@tarojs/taro'
import { Block } from "@tarojs/components"
import AchievePhone from '@/components/achievePhone'
import AchieveAvatar from '@/components/achieveAvatar'
import { isEmpty } from '@/utils/tools'

function noop(){}

export default function GrantProfile(props) {
  if (isEmpty(props.person)) return <Block />;

  const { tel, phone, avatarurl } = props.person || {}

  const noPhone = !(tel || phone)
  const noAvatar = !avatarurl || avatarurl.indexOf('wx.qlogo.cn') === -1
  
  const grantPhone = (props.phone || props.both) && noPhone
  const grantAvatar = (props.avatar || props.both) && noAvatar
  
  if (!grantPhone && !grantAvatar) {
    if (props.onSuccess) {
      props.onSuccess()
    }
  }
  
  return (
    <Block>
      {
        grantPhone &&
        (<AchievePhone user={props.person} onSuccess={noop}></AchievePhone>)
      }

      {
        grantAvatar && !grantPhone &&
        (<AchieveAvatar user={props.person} onSuccess={noop} ></AchieveAvatar>)
      }
    </Block>
  )
}
