// pages/index/notice/index.js
import HTTP from "../../../../requestFn/Api"
const app = getApp()
Page({
  data: {
    garden_id: null,
    notice_list: [],
    page: 1,
    page_size: 10,
    has_more: true,
  },
  onLoad() {
    const {
      garden_id
    } = app.globalData;
    if (garden_id) {
      this.setData({
        garden_id
      })
    }
  },
  onShow(options) {
    this.init();
  },
  init() {
    this.setData({
      page: 1,
      has_more: true,
    });
    this.getNotice(true)
  },
  //获取公告通知
  getNotice(flag = false) {
    const that = this;
    let {
      garden_id,
      page,
      page_size,
      notice_list
    } = that.data;
    HTTP.GetAnnouncement({
      garden_id,
      page,
      page_size
    }).then(res => {
      that.setData({
        notice_list: flag ? res.list : [...notice_list, ...res.list],
        page: ++page,
        has_more: page_size === res.list.length,
      })
    })
  },
  goDetail(e) {
    const that = this;
    const {
      id
    } = e.currentTarget
    const data = that.data.notice_list.filter(item => item.id == id)
    wx.navigateTo({
      url: './detail/index',
      success: function (res) {
        res.eventChannel.emit('acceptDataFromOpenerPage', {
          data
        })
      }
    })
  },
  onReachBottom() {
    if (!this.data.has_more) {
      wx.showToast({
        title: '没有更多数据了',
        icon: 'none'
      })
      return console.log('没有更多数据了');
    }
    this.getNotice()
  },
  onPullDownRefresh: function () {
    app.onRefresh();
    this.init();
  },
})