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

let pageIndex = 1

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

  config = {
    navigationBarTitleText: '我的活动'
  }

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

  componentWillMount() {
    // App.zhuge.track('查看活动列表页面')
    savePoint({
      event: 'list',
      eventType: 'activity',
      propertyName: '活动列表',
      data: '{}'
    }).then(res => {
      this.setState({
        recordId: res.recordId
      })
      console.log('活动列表')
    })
  }
  componentDidMount() {
  }
  componentWillUnmount() {
    const { recordId } = this.state
    recordId && updatePoint(recordId)

    this.loadList(1)

  }

  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)

  }

  handleItemClick(item) {
    if (item.status == 0) {
      Taro.showToast({
        title: '当前活动已失效',
        icon: 'none'
      })
    } else {
      Taro.navigateTo({
        url: '/pages/activity/detail/index?id=' + item.dynamicId
      })
    }


  }

  onPullDownRefresh = async (rest) => {
    const { records, list, total } = await this.loadList(1);
    const _list = records || list || []
    this.setState({
      list: _list,
      isEmpty: total == 0,
      hasMore: _list.length >= total ? false : true,
      pageIndex: 1
    })
    rest && rest()
  }

  onScrollToLower = async (fn) => {
    const { list } = this.state;
    pageIndex++
    const { records, list: list1, total } = await this.loadList(this.state.pageIndex + 1);
    const _list = records || list1 || []
    const newList = list.concat(_list)
    this.setState({
      list: newList,
      hasMore: newList.length >= total ? false : true,
      pageIndex: this.state.pageIndex + 1
    });
    fn && fn();
  }

  render() {
    const { isEmpty, hasMore, list } = this.state
    return (
      <ListView
        className="wrap"
        needInit
        isEmpty={isEmpty}
        emptyText="暂无活动~"
        hasMore={hasMore}
        onPullDownRefresh={fn => this.onPullDownRefresh(fn)}
        onScrollToLower={fn => this.onScrollToLower(fn)}
      >
        <View className="list">
          {
            list.map(item => (
              <ActivityItem
                data={item}
                key={item.dynamicId}
                onClick={this.handleItemClick}>
              </ActivityItem>
            ))
          }
        </View>
      </ListView>
    );
  }
}