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

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

class Index extends Component {
  onShareAppMessage() {
    const {id, name} = this.$router.params
    return {
      title: `${name}的历史成交价`,
      path: `/pagesHouse/price?id=${id}&name=${name}`,
    }
  }
  onShareTimeline () {
    return {
      title: '让买房，更省心！',
    }
  }

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

  config = {
    navigationBarTitleText: '楼盘成交价',
  }

  componentWillMount () {
    const { name } = this.$router.params
    Taro.setNavigationBarTitle({
      title: `${name}的历史成交价`
    })
    this.getDataList()
  }

  componentDidShow () { }

  componentDidHide () { }


  getDataList () {
    const {id: estate_id} = this.$router.params
    let { page_size, page, dataList, isListEmpty } = this.state
    Taro.api.house.apiestatepricelist({
      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()
      })
    }
  }

  renderList () {
    const { dataList, isListEnd, isListLoading, isListEmpty } = this.state
    const itemsList = dataList.map((item, index) => {
      return (
        <View className="sl-item" key={index}>
          <View className="sl-p1">{item.estate_name}</View>
          <View className="sl-p2">
            <View className="op">
              <View className="k">总面积</View>
              <View className="v">{item.area}㎡</View>
            </View>
            <View className="op t2">
              <View className="k">单价</View>
              <View className="v">￥{item.unit_price}/㎡</View>
            </View>
          </View>
          <View className="sl-p3">签约中介：{item.company}</View>
          <View className="sl-p3">签约日期：{item.sign_at}</View>
          <View className="sl-r1"><View className="n">{item.price}</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>
    )
  }


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

export default Index
