import Taro, { Component } from '@tarojs/taro'
import './Popup.scss'
export default class SelectSearch extends Component {
  constructor (props) {
    super(props)
    this.state = {}
  }
  
  componentDidUpdate (prevProps, prevState) {}

  getPhoneNumber (e) {
    const dtlObj = e.detail || {}
    const userInfo = Taro.getStorageSync('APP_userInfo')
    let that = this
    Taro.login({
      success: function (res) {
        if (res.code) {
          Taro.api.base.apiwxlogin({
            code: res.code,
            avatar: userInfo.avatarUrl,
            nickname: userInfo.nickName,
            iv: dtlObj.iv,
            encrypted_data: dtlObj.encryptedData,
          }).then(cData => {
            Taro.setStorageSync('APP_token', cData.token)
            const UI = {
              nickName: cData.nickname,
              avatarUrl: cData.avatar,
              phone: cData.phone,
            }
            Taro.setStorageSync('APP_userInfo', UI)
            that.closePopup('success')
          }).catch(err => {
            console.log(err)
          })
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    })
  }

  closePopup (str) {
    const { close } = this.props
    if (close) {
      close(str)
    }
  }

  render () {
    const { show } = this.props
    const bg = require('@img/bg_login.png')
    return (
      <View className={show ? 'l-modal-box' : 'l-modal-box hide'}>
        <View className='lmb-bg' onClick={this.closePopup.bind(this)}></View>
        <View className="lmb-wrap">
          <View className="scoped-login">
            <Image className="sl-bg" src={bg} />
            <View className="sl-p2">关注楼盘、评论楼盘、楼盘浏览记录等需要微信登录后才能使用</View>
          </View>
          <View className="l-floor-footer t2">
            <View className="lff-flex">
              <Button open-type="getPhoneNumber" onGetPhoneNumber={this.getPhoneNumber.bind(this)} className="lff-btn full">微信登录</Button>
            </View>
          </View>
        </View>
      </View>
    )
  }
}