import Taro, { Component } from '@tarojs/taro';
import ListView from '@/components/ListView'
import './index.scss'
import { queryNewsList } from '@/services/news'
import NewsItem from './item'
import { savePoint, updatePoint } from '@/services/common'
import { connect } from '@tarojs/redux'

@connect(state => state.city)
export default class Index extends Component {

  config = {
    navigationBarTitleText: '资讯列表'
  }

  state = {
    list: [],
    hasMore: true,
    isEmpty: false,
    recordId: null,
    pageIndex: 1
  }

  refreshing = false

  componentWillMount() {
    // App.zhuge.track('查看资讯列表页面')
    savePoint({
      event: 'list',
      eventType: 'news',
      propertyName: '资讯列表',
      data: '{}'
    }).then(res => {
      this.setState({
        recordId: res.recordId
      })
      console.log('资讯列表')
    })
  }

  componentDidShow() {
    // // this.onPullDownRefresh()
    // console.log('----', this)
    this.loadData(1)
  }

  componentWillUnmount() {
    const { recordId } = this.state
    recordId && updatePoint(recordId)
  }

  loadList(pageNumber) {
    const { curCity: { id: cityId } } = this.props
    const payload = { pageSize: 10, pageNumber }

    const { params: { buildingId, from } } = this.$router
    if (cityId) {
      payload.cityId = cityId
    }
    // 从个人中心过来
    console.log(this.router, "from")
    if (from === 'mine') {
      payload.mine = true
    }
    // // 从项目详情过来
    if (buildingId) {
      payload.buildingId = buildingId
    }
    // return queryActivityList(payload)
    return queryNewsList(payload)
  }

  handleItemClick = (item) => {
    Taro.navigateTo({
      url: '/pages/news/detail/index?id=' + item.newsId
    })
  }

  onPullDownRefresh = (rest) => {
    // debugger
    if (this.refreshing) return
    this.refreshing = true

    this.loadData(1)

    // debugger
    rest && rest()

    this.refreshing = false
  }

  onScrollToLower = async (fn) => {
    const { pageIndex } = this.state;
    this.loadData(pageIndex + 1)
    fn && fn();
  }

  loadData = (page) => {
    this.loadList(page).then(res => {
      const { records, list, total, current, pages } = res || {}
      const _list = records || list || []
      const newList = current <= 1 ? _list : this.state.list.concat(_list)
      console.log(res, '---res-------')
      this.setState({
        list: newList,
        isEmpty: total == 0,
        hasMore: current < pages,
        pageIndex: current >= pages ? pages : current
      })
    })
  }

  render() {
    const { isEmpty, hasMore, list } = this.state
    return (
      <ListView
        className="wrap"
        style="height: calc(100vh - 118rpx);"
        needInit
        isEmpty={isEmpty}
        emptyText="暂无资讯~"
        hasMore={hasMore}
        onPullDownRefresh={fn => this.onPullDownRefresh(fn)}
        onScrollToLower={fn => this.onScrollToLower(fn)}
      >
        <View className="list">
          {
            list.map(item => (
              <NewsItem
                key={item.newsId}
                data={item}
                onClick={this.handleItemClick}
              ></NewsItem>
            ))
          }
        </View>
      </ListView>
    );
  }
}