import Taro, { Component } from '@tarojs/taro'
import { View, Image } from '@tarojs/components'

import ListMore from '@/c/pageDataList/listMore'
import './news.scss'

class Index extends Component {
  onShareAppMessage() {
    const {id, name} = this.$router.params
    return {
      title: `${name}-楼盘动态`,
      path: `/pagesHouse/indexDtl?id=${id}&name=${name}`,
    }
  }
  onShareTimeline () {
    const {id, name} = this.$router.params
    return {
      title: `${name}-楼盘动态`,
      path: `/pagesHouse/indexDtl?id=${id}&name=${name}`,
    }
  }

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

  config = {
    navigationBarTitleText: '楼盘动态',
  }

  componentWillMount () {
    this.getDataList()
  }

  componentDidShow () { }

  componentDidHide () { }


  getDataList () {
    const {id: estate_id} = this.$router.params
    let { page_size, page, dataList, isListEmpty } = this.state
    Taro.api.house.apiestatenewslist({
      page,
      page_size,
      estate_id
    }).then(res => {
      const 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
      }
      this.setState({
        dataList,
        isListEnd,
        isListEmpty,
        isListLoading: false
      })
    })
  }
  onScrollToLower (e) {
    let { isListEnd, isListLoading, page } = this.state
    if (!isListEnd && !isListLoading) {
      page++
      this.setState({
        page,
        isListLoading: true
      }, () => {
        this.getDataList()
      })
    }
  }

  renderTop () {
    const { name } = this.$router.params
    const bg = require('@img/images/bg_house.jpg')
    return (
      <View className="scoped-bg-top">
        <View className="sbt-wrap" onClick={this.urlDtlLink.bind(this)}>
          <View className="t">{name}</View>
          <Image src={bg} className="bg"></Image>
        </View>
      </View>
    )
  }
  urlDtlLink () {
    const { id } = this.$router.params
    Taro.navigateTo({url: '/pagesHouse/indexDtl?id=' + id})
  }

  renderList () {
    const exImg = require('@img/images/ex_banner.jpg')
    const { dataList, isListEnd, isListLoading, isListEmpty } = this.state
    const itemsList = dataList.map((item, index) => {
      const imgArr = (item.images || '').split(',')
      return (
        <View className="sl-item" key={index}>
          <View className="sl-info">
            <View className="sl-p1">{item.news_at}</View>
            <View className="sl-p2">{item.title}</View>
            <View className={item.isMoreAllShow ? 'sl-p3' : 'sl-p3s'}>{item.trends_cont}</View>
            {
              item.trends_cont.length > 50
              ?
              item.isMoreAllShow
              ?
              <View className="sl-more t2" onClick={this.changeMoreHandle.bind(this, index)}>收起
                <View className="sign"></View>
              </View>
              :
              <View className="sl-more" onClick={this.changeMoreHandle.bind(this, index)}>全文
                <View className="sign"></View>
              </View>
              : ''
            }
            {
              item.images
              &&
              <View className="sl-img">
                {
                  imgArr.map((img, imgIndex) => { 
                    return (
                      <Image className="img" src={img + '_xs'} key={imgIndex}  onClick={this.previewImageHandle.bind(this, img, imgArr)}  />
                    )
                  })
                }
              </View>
            }
          </View>
          <View className="sl-footer">
            <Image className="img" src={item.avatar} />
            <View className="t">洪楼Plus</View>
          </View>
        </View>
      )
    })
    return (
      <ScrollView
        className='l-scroll-view'
        scrollY
        scrollWithAnimation
        scrollTop="0"
        lowerThreshold="30"
        onScrollToLower={this.onScrollToLower.bind(this)}
      >
        <View className="scoped-list">
          {itemsList}
        </View>
        <ListMore isListEnd={isListEnd} isListLoading={isListLoading} isListEmpty={isListEmpty} />
      </ScrollView>
    )
  }

  previewImageHandle (cur, arr) {
    const current = `${cur}_plus`
    const urls = arr.map(item => {
      return `${item}_plus`
    })
    Taro.previewImage({
      current,
      urls
    })
  }

  changeMoreHandle (index) {
    const { dataList } = this.state
    let mList = dataList || []
    mList.map((item, i) => {
      if (index === i) item.isMoreAllShow = !item.isMoreAllShow
    })
    this.setState({
      dataList: [...mList]
    })
  }

  render () {
    return (
      <View className="l-box">
        {this.renderTop()}
        {this.renderList()}
      </View>
    )
  }
}

export default Index
