import Taro, { Component } from '@tarojs/taro'
import { View } from '@tarojs/components'
import { AtTextarea }  from 'taro-ui'
import './add.scss'

class Index extends Component {
  constructor (props) {
    super(props)
    this.state = {
      commentVal: ''
    }
  }
  config = {
    navigationBarTitleText: '评论',
  }

  componentWillMount () {
    // let pages = getCurrentPages()
    // let prevPage = pages[ pages.length - 2 ]
    // console.log(prevPage)
  }

  commentChange (commentVal) {
    this.setState({
      commentVal
    })
  }

  saveHandle () {
    const { commentVal } = this.state
    const { id, type } = this.$router.params
    if (commentVal) {
      if (type && type === 'school') {
        Taro.api.house.schoolcommentadd({
          school_id: id,
          comm_cont: commentVal,
        }).then(res => {
          Taro.$msg('评论成功~')
          let pages = getCurrentPages()
          let prevPage = pages[ pages.length - 2 ]
          prevPage.$component.getDtl()
          Taro.navigateBack({
            delta: 1
          })
        })
      } else {
        Taro.api.house.estatecommentadd({
          estate_id: id,
          comm_cont: commentVal,
        }).then(res => {
          Taro.$msg('评论成功~')
          let pages = getCurrentPages()
          let prevPage = pages[ pages.length - 2 ]
          prevPage.$component.getDtl()
          Taro.navigateBack({
            delta: 1
          })
        })
      }
    } else {
      Taro.$msg('请输入你的评论')
    }
  }

  render () {
    const { commentVal } = this.state
    const { name } = this.$router.params
    return (
      <View className="l-box">
        <View className="comment-box">
          <View className="cb-title">写下您对{name}的评价</View>
          <AtTextarea
            value={commentVal}
            onChange={this.commentChange.bind(this)}
            maxLength={200}
            height={300}
            placeholder='你的评论...'
          />
        </View>
        <View className="l-floor-footer t2">
          <View className="lff-flex">
            <View className="lff-btn full" onClick={this.saveHandle.bind(this)}>提交评论</View>
          </View>
        </View>
      </View>
    )
  }
}

export default Index
