import Taro, { Component } from '@tarojs/taro'
import { View, Image, Input } from '@tarojs/components'
import ListMore from '@/c/pageDataList/listMore'
import './dtlSub.scss'
import { strTrim } from '@utils'

class Index extends Component {
  onShareAppMessage() {
    const { curObj } = this.state
    return {
      title: `来看看这些评论，有点意思`,
      path: `/pagesQa/dtl?id=${curObj.question_id}`
    }
  }
  onShareTimeline () {
    const { curObj } = this.state
    return {
      title: `来看看这些洪楼评论，有点意思`,
      path: `/pagesQa/dtl?id=${curObj.question_id}`
    }
  }


  constructor (props) {
    super(props)
    this.state = {
      page_size: 10,
      page: 1,
      isListEnd: false,
      isListLoading: false,
      isListEmpty: false,
      dataList: [],
      curObj: {},
      commentVal: '',
      tempObj: {},
      isAnswerHide: false,
    }
  }

  config = {
    navigationBarTitleText: '评论详情',
  }

  componentWillMount () {
    const { name } = this.$router.params
    Taro.setNavigationBarTitle({
      title: name || '评论详情'
    })
    this.getDataList()
  }

  componentDidShow () { }

  componentDidHide () { }


  renderTop () {
    const { curObj } = this.state
    const HideMan = require('@img/icon_hide_man.png')
    const zan1Icon = require('@img/i_g_zan.png')
    const zan2Icon = require('@img/i_g_zan2.png')
    return (
      <View className="l-comment-box">
        <View className="lcb-item" onClick={this.sendChangeHandle.bind(this, curObj)}>
          <View className="lb-img">
            <Image className="img" src={curObj.is_anon === '1' ? HideMan : curObj.avatar} />
          </View>
          <View className="lb-info">
            <View className="lb-p1">{curObj.nickname}
              <View onClick={this.linkDtl.bind(this)} className="more">{'《返回问答详情》'}</View>
            </View>
            <View className="lb-p2">{curObj.answer_cont}</View>
            <View className="lb-time">{curObj.create_at}</View>
          </View>
          <View className="lb-zan" onClick={this.zanHandle.bind(this, curObj.answer_id, curObj.is_zan)}>
            {
              curObj.is_zan
              ?
              <Image className="i" src={zan2Icon}/>
              :
              <Image className="i" src={zan1Icon}/>
            }
            <View className={curObj.is_zan ? 'n cur' : 'n'}>{curObj.zan_count}</View>
          </View>
        </View>
      </View>
    )
  }
  linkDtl () {
    const { curObj } = this.state
    Taro.navigateTo({url: '/pagesQa/dtl?id=' + curObj.question_id})
  }
  zanHandle (answer_id, flag) {
    let apiStr = flag ? 'apianswerunzan' : 'apianswerzan'
    Taro.api.news[apiStr]({
      answer_id
    }).then(res => {
      this.getDataList()
    })
  }

  getDataList () {
    const HideMan = require('@img/icon_hide_man.png')
    let { page_size, page, dataList, isListEmpty } = this.state
    const { id: answer_id } = this.$router.params
    let apiStr = 'apiansweronelevel'
    Taro.api.news[apiStr]({
      page,
      page_size,
      answer_id,
    }).then(res => {
      let curObj = res.one_level || {}
      if (curObj.is_anon === '1') curObj.nickname = '匿名粉丝'
      let curData = res.list || []
      let isListEnd = false
      if (curData.length > 0) {
        if (page === 1) {
          dataList = curData
        } else {
          dataList = dataList.concat(curData)
        }
        if (curData.length === page_size && res.total !== curData.length) {
          isListEnd = false
        } else {
          isListEnd = true
        }
      }
      if (curData.length === 0 && page === 1) {
        isListEmpty = true
        dataList = []
      } else {
        isListEmpty = false
      }
      dataList.map(item => {
        if (item.is_anon === '1') {
          item.nickname = '匿名粉丝'
          item.avatar = HideMan
        }
        if (item.child && item.child.length > 0) {
          let child = item.child
          child.map(sub => {
            if (sub.is_anon === '1') {
              sub.nickname = '匿名粉丝'
              sub.avatar = HideMan
            }
          })
        }
      })
      this.setState({
        curObj,
        tempObj: curObj,
        dataList,
        isListEnd,
        isListEmpty,
        isListLoading: false
      })
    })
  }
  onScrollToLower (e) {
    let { isListEnd, isListLoading, page } = this.state
    if (!isListEnd && !isListLoading) {
      page++
      this.setState({
        page,
        isListLoading: true
      }, () => {
        this.getDataList()
      })
    }
  }

  renderList () {
    const { dataList, isListEnd, isListLoading, isListEmpty, curObj } = this.state
    const curItems = dataList.map((item, index) => {
      return (
        <View className="lcb-item" key={index} onClick={this.sendChangeHandle.bind(this, item)}>
          <View className="lb-img">
            <Image className="img" src={item.avatar} />
          </View>
          <View className="lb-info">
            <View className="lb-p1">{item.nickname}</View>
            {
              item.parent_id === curObj.answer_id
              ?
              <View className="lb-p2">{item.answer_cont}</View>
              :
              <View className="lb-p2">回复:<View className="link">{item.parent_user_name}</View>{item.answer_cont}</View>
            }
            <View className="lb-time">{item.create_at}</View>
          </View>
        </View>
      )
    })
    return (
      <ScrollView
        className='l-scroll-view'
        scrollY
        scrollWithAnimation
        scrollTop="0"
        lowerThreshold="30"
        onScrollToLower={this.onScrollToLower.bind(this)}
      >
        <View className="l-comment-box">
          {curItems}
        </View>
        <ListMore isListEnd={isListEnd} isListLoading={isListLoading} isListEmpty={isListEmpty} text="暂无回复"/>
      </ScrollView>
    )
  }

  sendChangeHandle (tempObj) {
    this.setState({
      commentVal: '',
      tempObj
    })
  }

  sendHandle () {
    const { curObj, commentVal, tempObj, isAnswerHide } = this.state
    if (commentVal) {
      let params = {
        question_id: curObj.question_id,
        answer_cont: commentVal,
        parent_id: tempObj.answer_id || '',
        parent_user_id: tempObj.user_id || '',
        parent_user_name: tempObj.nickname || '',
      }
      if (isAnswerHide) {
        params.is_anon = '1'
      }
      Taro.api.news.apiansweradd(params).then(res => {
        Taro.$msg('回答成功~')
        this.setState({
          commentVal: ''
        })
        this.getDataList()
      })
    } else {
      Taro.$msg('请输入你的回答')
    }
  }

  changeInput (e) {
    const val = strTrim(e.target.value)
    this.setState({
      commentVal: val
    })
  }

  linkHome () {
    Taro.reLaunch({
      url: '/pages/index/index'
    })
  }

  answerHideChange () {
    let { isAnswerHide } = this.state
    isAnswerHide = !isAnswerHide
    this.setState({
      isAnswerHide
    })
  }


  render () {
    const { tempObj, commentVal, dataList, isAnswerHide } = this.state
    const hideN = require('@img/icon_hide2.png')
    const hideY = require('@img/icon_hide3.png')
    return (
      <View className="l-box has-footer">
        {this.renderTop()}
        <View className="scoped-main">
          <View className="sm-title">
            全部回复
            <View className="t">({dataList.length}条)</View>
          </View>
          {this.renderList()}
        </View>
        <View className="l-floor-footer">
          <View className="scoped-footer">
            <View className="sf-input">
            {/* focus */}
              {/* <Input className="input" placeholder={`我来回答`} value={commentVal}  onInput={this.changeInput.bind(this)} /> */}
              <Input className="input" placeholder={`回复:${tempObj.nickname}`} value={commentVal}  onInput={this.changeInput.bind(this)} />
              <View className="more" onClick={this.answerHideChange.bind(this)}>
                {
                  isAnswerHide
                  ? 
                  <Image className="i" src={hideY} />
                  :
                  <Image className="i" src={hideN} />
                }
              </View>
              <View className="btn" onClick={this.sendHandle.bind(this)}>发送</View>
            </View>
          </View>
        </View>
      </View>
    )
  }
}

export default Index
