import Taro, { Component } from '@tarojs/taro'
import getUserPhone from '@utils/getUserPhone'
import { reportClient } from '@services/report'
import './index.scss'

export default class Index extends Component {
  state = {
    name: '',
    recommender: '',
    buildingId: ''
  }
  componentWillMount() {
    const { recommender, buildingId, name } = this.$router.params

    this.setState({
      name,
      recommender,
      buildingId
    })
  }
  handleGetPhone = (e) => {
    const { recommender, buildingId } = this.state
    getUserPhone(e, (phoneNumber) => {
      if (phoneNumber) {
        const payload = {
          realtyConsultant: recommender, //报备人 置业顾问
          buildingId: buildingId,
          phone: phoneNumber
        }
        reportClient(payload).then(res => {
          Taro.redirectTo({
            url: `/pages/card/index?id=${recommender}`
          })
        })

        
      } else {
        Taro.showToast({
          title: '授权失败，请重试',
          icon: 'none'
        })
      }
    })
  }
  render() {
    return (
      <View>
        <View className="userinfo">
          <View className="userinfo-avatar">
            <open-data type="userAvatarUrl"></open-data>
          </View>
          <View className="userinfo-name">
            <open-data type="userNickName"></open-data>
          </View>
        </View>
        <View className='line'></View>
        <View className='content'>
          <Text>是否授权同意成为{this.state.name}的客户</Text>
        </View>
        <Button className='bottom' open-type="getPhoneNumber" onGetphonenumber={this.handleGetPhone}>同意授权</Button>
      </View>
    )
  }
}
