// pages/index/vote/index.js
const app = getApp();
import HTTP from "../../../../requestFn/Api"
Page({

  data: {
    garden_id: null,
    vote_list: [],
    page: 1,
    page_size: 10,
    hasMore:true, 
    indicatorDots: false, //小点
    indicatorColor: "white", //指示点颜色
    activeColor: "coral", //当前选中的指示点颜色
    autoplay: true, //是否自动轮播
    interval: 5000, //间隔时间
    duration: 3000, //滑动时间,
    timestamp:0,
  },

  onLoad(options) {
    const that = this;
    const {
      garden_id
    } = app.globalData
    if(garden_id){
      that.setData({
        garden_id,
      })
    }
  },
  onShow(){
    this.init();
  },
  init(){
    var timestamp = Date.parse(new Date());  
    timestamp = timestamp / 1000; 
    this.setData({
      page:1,
      hasMore:true,
      timestamp
    })
    this.getVote_list(true);
  },
  onReachBottom() {
    if(!this.data.hasMore){
      wx.showToast({
        title: '没有更多数据了',
        icon:'none'
      })
      return console.log('没有更多数据了');
    }
    this.getVote_list()
  },
  //投票列表
  async getVote_list(flag=false) {
    const that = this;
    let {
      page,
      page_size,
      vote_list,
      garden_id
    } = that.data;
   let res = await HTTP.GetVote({
      garden_id,
      page,
      page_size
    })
    that.setData({
      vote_list:flag?res.list:[...vote_list,...res.list],
      page:++page,
      hasMore:res.list.length===page_size
    })
  },
  goDetail(e) {
    const that = this;
    const {
      id
    } = e.currentTarget
    const data = that.data.vote_list.filter(item => item.id == id)
    wx.navigateTo({
      url: './detail/index',
      success: function (res) {
        res.eventChannel.emit('acceptDataFromOpenerPage', {
          data
        })
      }
    })
  },
  onPullDownRefresh: function () {
    app.onRefresh();
    this.init();
  },
})