// pages/index/event/index.js
import HTTP from "../../../../requestFn/Api"
const app = getApp();
Page({
  data: {
    event_list: [],
    indicatorDots: false, //小点
    indicatorColor: "white", //指示点颜色
    activeColor: "coral", //当前选中的指示点颜色
    autoplay: true, //是否自动轮播
    interval: 5000, //间隔时间
    duration: 3000, //滑动时间,
    page: 1,
    page_size:5,
    hasMore:true, 
  },
  onLoad(options) {
    const that = this;
    const {
      garden_id
    } = app.globalData
    if(garden_id){
      that.setData({
        garden_id
      })
    }
  },
  onShow(){
     this.init();
  },
  init(){
    this.setData({
      page:1,
      hasMore:true,
    })
    this.getEvent(true);
  },
  onPullDownRefresh() {
    app.onRefresh();
    this.init();
  },
  onReachBottom() {
    if(!this.data.hasMore){
      wx.showToast({
        title: '没有更多数据了',
        icon:'none'
      })
      return console.log('没有更多数据了');
    }
    this.getEvent()
  },
  async getEvent(flag=false) {
    const that = this;
    let {
      page,page_size,garden_id,event_list
    } = that.data 
   let res = await HTTP.GetEvent({
      garden_id,
      page,
      page_size
    })
    that.setData({ 
      event_list:flag?res.list:[...event_list,...res.list],
      page:++page,
      hasMore:res.list.length===page_size
    })
  },
  goDetail(e) {
    const that = this;
    const {
      id
    } = e.currentTarget
    const data = that.data.event_list.filter(item => item.id == id)
    wx.navigateTo({
      url: './detail/index',
      success: function (res) {
        res.eventChannel.emit('acceptDataFromOpenerPage', {
          data
        })
      }
    })
  }
})