import {
  getGroupList} from '../../services/index.js'
import {
  imgServerUrl
} from '../../config/config.js'
const app = getApp();

Page({
  data: {
    hasShare: true,
    imgServerUrl: imgServerUrl,
    cityCode: '',
    pageNo: 1, //当前分页
    isScroll: true, //是否可以滚动
    clearTimer: false,
    djsEnd: false,
    myFormat: ['天', ':', ':', ' '],
    list: [],
    showNoMore: "没有更多信息...", //显示已无更多
    show_empty: false
  },
  onLoad: function () {
    this.start();
  },
  onReady: function () {

  },
  onShow: function () {
 
  },
  start() {
    let cityName = wx.getStorageSync('city') || '无锡'
    let cityCode = wx.getStorageSync('cityCode') || '320200';
    this.setData({
      cityName: cityName,
      cityCode: cityCode,
      pageNo: 1, //当前分页
      isScroll: true, //是否可以滚动
      list: [],
      show_empty: false
    })
    this.fetchList()
  },
  //下拉刷新
  onPullDownRefresh: function () {
    this.setData({
      pageNo: 1, //当前分页
      isScroll: true, //是否可以滚动
      list: [],
    })
    this.start();
  },
  onReachBottom: function () {
    var pageNo = this.data.pageNo + 1;
    this.setData({
      pageNo
    })
    this.fetchList()
  },
  //获取拼团列表数据
  fetchList() {
    if (!this.data.isScroll) {
      return false
    }
    let paramsObj = {
      cityCode: this.data.cityCode,
      pageNo: this.data.pageNo,
    }
    getGroupList(paramsObj).then(data => {
      var pageNo = data.data.pageNo;
      let setData = {};
      // 是否可以滚动加载数据
      if (data.data.isLast) {
        setData.isScroll = false
      }
      if (data.data.groupPosition.length != 0) {
        setData.list = this.data.list.concat(data.data.groupPosition)
        setData.currentTime = data.data.currentTime;
        setData.pageNo = pageNo;
      } else {
        setData.list = this.data.list;
      }
      this.setData({
        ...setData,
      }, () => {
        this.setData({
          show_empty: !this.data.list.length
        })
      })
    })
  },

  //去详情页
  toDetail(e) {
    const {
      id,
      cate
    } = e.detail;
    wx.navigateTo({
      url: '../detail/index?hpPositionId=' + id + "&cate=" + cate,
    })
  },
  //去申请列表
  toApplyList() {
    wx.navigateTo({
      url: '../apply-list/index?type=pt'
    })
  },
  //倒计时结束回调
  myLinsterner() {
    // this.fetchPt()
    this.setData({
      djsEnd: true
    })
  },
  onShareAppMessage: function () {
    return {
      title: '快来开心工作看看吧',
      path: '/pages/index/index',
      imageUrl: ''
    }
  },
  onError(err) {
    app.aldstat.sendEvent('报错', {
      'err': err
    });
  },
})