import Taro, { Component } from '@tarojs/taro'
import { View, Image } from '@tarojs/components'
import './remindCust.scss'
export default class Popup extends Component {
  constructor (props) {
    super(props)
    this.state = {
      dataList: [],
      isPopupShow: false,
      total: 0,
    }
  }


  componentWillMount () {
    
    this.getData()
  }

  getData () {
    const uoObj = Taro.getStorageSync('APP_userInfo')
    if (uoObj) {
      Taro.api.base.apiuserhouselist().then(res => {
        const dataList = res.list || []
        this.setState({
          dataList,
          total: res.total || 0,
          isPopupShow: dataList.length > 0 ? true : false
        })
      })
    }
  }

  openPopup () {
    this.setState({
      isPopupShow: true
    })
  }
  closePopup () {
    this.setState({
      isPopupShow: false
    })
  }

  linkRoomDtl (item) {
    Taro.navigateTo({
      url: `/pagesMore/center/uploadRoom2?cid=${item.id}`
    })
  }

  callHandle (phone) {
    Taro.makePhoneCall({
      phoneNumber: phone
    })
  }


  previewImageHandle (item) {
    const imgArr = item.images && item.images.length > 0 ? item.images.split(',') : []
    const urls = [item.house_cert, ...imgArr]
    Taro.previewImage({
      current: item.house_cert,
      urls
    })
  }

  verifyHandle (item) {
    let that = this
    Taro.$msgConfirm('确定已上传该房源吗，确定后将隐藏该条记录？', () => {
      Taro.api.base.apiuserhouseverify({id: item.id}).then(res => {
        Taro.$msg('已隐藏！')
        that.getData()
      })
    })
  }

  render () {
    const { isPopupShow, dataList, total } = this.state
    const bgClose = require('@img/icon_g_close2.png')
    return (
      <View className={isPopupShow ? 'l-modal-box' : 'l-modal-box hide'}>
        <View className='lmb-bg'></View>
        <View className='lmb-body'>
          <View className="scoped-box">
            <View className="sb-main">
              <View className="title">已分配房源({total})</View>
              {
                dataList.map(item => {
                  return(
                    <View key={item.id} className="op">
                      <View className="p1">
                        <View onClick={this.callHandle.bind(this, item.phone)} className="text">[{item.id}]挂价{item.price}w,电:{item.phone}</View>
                        <Image src={item.house_cert} className="img" onClick={this.previewImageHandle.bind(this, item)} />
                      </View>
                      <View onClick={this.callHandle.bind(this, item.phone)} className="p3">{item.remark}</View>
                      <View className="p2">
                        <View onClick={this.linkRoomDtl.bind(this, item)} className="t">去上传房源</View>
                        <View onClick={this.verifyHandle.bind(this, item)} className="t t2">已上传</View>
                      </View>
                    </View>
                  )
                })
              }
            </View>
            <Image src={bgClose} className="close" onClick={this.closePopup.bind(this)} />
          </View>
        </View>
      </View>
    )
  }
}