import Taro, { Component } from '@tarojs/taro'
import { connect } from '@tarojs/redux'
import * as actions from '@actions/user'
import './index.scss'
import getUserPhone from '@utils/getUserPhone'
import { queryUserInfo, doUserSignin } from '@services/user'

@connect(state => state.user, { ...actions })
export default class Index extends Component {
  config = {
    navigationBarTitleText: '我的'
  }
  state = {
    isLogin: false,
    isPhone: false
  }
  componentWillMount() {
    const { user = {} } = this.props
    this.toggleGrantAvatar()
  }

  toggleGrantAvatar = () => {
    const { userInfo: { person: { avatarurl,tel,phone } } } = this.props

    if (!avatarurl || avatarurl.indexOf('wx.qlogo.cn') === -1) {
      console.log(this.props.userInfo, "this.props")
      this.setState({isLogin:false})
    }
    console.log(avatarurl, "avatarurl")
    this.setState({isLogin:true})
    if(!tel&&!phone){
      this.setState({isPhone:false})
    }
    this.setState({isPhone:true})
  }

  getUserInfo() {
    const { dispatchUpdateUserInfo } = this.props
    const sessionKey = Taro.getStorageSync('sessionKey')
    Taro.getUserInfo({
      lang: 'zh_CN',
      success: res => {
        console.log(res, "res")
        const payload = Object.assign(res, { sessionKey })
        // const { userInfo: { nickName, gender, country, city, province } } = payload
        // App.zhuge.identify(nickName, {
        //   '昵称': nickName,
        //   '性别': gender == 1 ? '男' : '女',
        //   '国家': country,
        //   '省份': province,
        //   '城市': city
        // })
        // 用户信息保存至服务器
        dispatchUpdateUserInfo(payload).then(res => {
          console.log('更新信息')
          this.setState({
          	isLogin:true,
          	
          })
        })
      },
      fail: (err) => {
        console.error(err)

        Taro.showToast({
          title: '获取用户信息失败',
          icon: 'none'
        })
        return
        // this.toPage(false)
      }
    })
  }

  getPhoneNumber(e) {
  	const { userInfo: { person: { personId } } } = this.props
    getUserPhone(e, (phoneNumber) => {
      const { dispatchUpdateUserInfoNew } = this.props
      if (!phoneNumber) {
        Taro.showToast({
          title: '获取手机号失败',
          icon: 'none'
        })
        return
      } else {
        dispatchUpdateUserInfoNew(personId)
        this.setState({isPhone:true})
      }
    })
  }

  render() {
    const { userInfo: { person: { avatarurl,nickname } } } = this.props
    if (!this.state.isLogin) {
      return (
        <View className="avatar-page">
          <View className="content">
            <View className="title">请允许获取昵称、头像、地区及性别</View>
            <Image className="touxiang" src={require('@assets/default-avatar.png')}></Image>
            <View className="name">用户昵称</View>
            <Button className="btn" open-type="getUserInfo" lang="zh_CN" onGetUserInfo={this.getUserInfo}>好</Button>
          </View>
        </View>
      )
    } else if (!this.state.isPhone) {
      return (
        <View className='auth-page'>
          <Image className="avatar_img" src={avatarurl} />
          <View className="user_name">{nickname}</View>
          <Button className='auth-btn' open-type="getPhoneNumber" onGetphonenumber={this.getPhoneNumber}>微信授权一键登录</Button>
          {/* <View className='adver-btn' onClick={this.handleLogin}><Text>我是置业顾问,立即登录~</Text></View> */}
        </View>
      )
    }
    return (
      <View>
        {this.props.children}
      </View>
    )
  }
}
