// pages/index/suggestion/suggestion.js
const app = getApp();
import HTTP from "../../../../requestFn/Api"
Page({
  data: {
    suggestion_list: [],
    status: {
      1: "待受理",
      2: "受理中",
      3: "已完成"
    },
    page: 1,
    garden_id: null,
    page_size: 10,
    hasMore: true,
  },
  onLoad() {
    const {
      garden_id
    } = app.globalData;
    if (garden_id) {
      this.setData({
        garden_id
      })
    }
  },
  onShow(options) {
    this.init();
  },
  init() {
    this.setData({
      page: 1,
      hasMore: true
    })
    this.getSuggestion(true)
  },
  onReachBottom() {
    if (!this.data.hasMore) {
      wx.showToast({
        title: '没有更多数据了',
        icon: 'none'
      })
      return console.log('没有更多数据了');
    }
    this.getSuggestion();
  },
  async getSuggestion(flag = false) {
    let {
      page,
      suggestion_list,
      garden_id,
      page_size
    } = this.data;
    let res = await HTTP.Suggestion({
      garden_id,
      page,
      page_size
    })
    this.setData({
      suggestion_list: flag ? res.list : [...suggestion_list, ...res.list],
      page: ++page,
      hasMore: res.list.length === page_size
    })
    console.log(res);
  },
  goAddSuggestion() {
    wx.navigateTo({
      url: './addsuggestion/addsuggestion',
    })
  },
  goDetail(e) {
    const {
      id
    } = e.currentTarget
    wx.navigateTo({
      url: `./detail/detail?id=${id}`,
    })
  },
  onPullDownRefresh: function () {
    app.onRefresh();
    this.init();
  },
})