import Taro, { Component } from '@tarojs/taro';
import './index.scss'
import { connect } from '@tarojs/redux'
import ready from '@/utils/ready'
import { addPreselection, cancelPreselection } from '@/services/project'
import ContactConsultant from '../../components/ContactConsultant'

@connect(
  ({ user }) => ({ ...user })
)

export default class resultPage extends Component {
  config = {
    navigationBarTitleText: '',
  }

  state = {
    status: 2, // 1为成功 2为处理中 3为失败
    statusText: '',// 文案
    statusText2: null// 文案2
  }

  componentWillMount() {

    ready.queue(() => {
      const { type, id } = this.$router.params
      const { userInfo: { person: { personId, orgId } } } = this.props
      const params = {
        houseId: id,
        personId,
        orgId,
      }
      const params2 = {
        houseId: id,
        personId,
      }
      if (type == 'add') {
        Taro.setNavigationBarTitle({ title: '预选结果' })
        addPreselection(params).then(res => {
          this.setState({
            status: 1,
            statusText: '您已成功预选此房源',
            statusText2: '以后可以到个人中心查看选房记录',
          })
        }).catch(err => {
          this.setState({
            status: 3,
            statusText: err.message
          })
        })
      } else {
        Taro.setNavigationBarTitle({ title: '取消预选' })
        cancelPreselection(params2).then(res => {
          this.setState({
            status: 1,
            statusText: '您已成功取消预选此房源'
          })
        }).catch(err => {
          this.setState({
            status: 3,
            statusText: err.message
          })
        })
      }
    })
  }

  render() {
    const { status, statusText2 } = this.state
    const { type, buildingId } = this.$router.params
    return (
      <View className="result">
        {status == 1 && <Image className="result-img" src={require('../../assets/success2.png')}></Image>}
        {status == 2 && <Image className="result-img" src={require('../../assets/going.png')}></Image>}
        {status == 3 && <Image className="result-img" src={require('../../assets/fail2.png')}></Image>}
        {type == 'add' && <View className="status">{status == 1 ? '预选成功' : status == 2 ? '正在处理' : '预选失败'}</View>}
        {type != 'add' && <View className="status">{status == 1 ? '取消成功' : status == 2 ? '正在处理' : '取消失败'}</View>}
        <View className="tip" style="margin-bottom:10rpx">{statusText}</View>
        {statusText2 && <View className="tip">{statusText2}</View>}
        <ContactConsultant buildingId={buildingId} style=" position: absolute;bottom: 8vh;" />
      </View>

    );
  }
}
