// packageE/healthy/healthyRecord/healthyRecord.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    listData: [],
    isLoadMore: true,
    total_page: 0,
    page: 1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getRecordList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.isLoadMore) {
      this._getMoreData();
    } else {
      console.log("没有更多数据");
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  _getMoreData() {
    let that = this;
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      let urlStr = app.getNetAddresss(
        "plugin.health-assessment.api.index.getData"
      );
      this.data.page++;
      app._postNetWork({
        url: urlStr,
        data: {
          art: "getRecord",
          page: this.data.page,
        },
        success: (resdata) => {
          let res = resdata.data;
          if (res.result != 1) return;
          if (res.data.getRecord.current_page >= res.data.getRecord.last_page) {
            that.setData({
              isLoadMore: false,
            });
          }
          let listData = this.data.listData;
          listData.push(...res.data.getRecord.data);
          this.setData({ listData });
        },
        fail: function (res) {
          console.log(res.msg);
        },
      });
    }
  },
  getRecordList() {
    let that = this;
    let urlStr = app.getNetAddresss(
      "plugin.health-assessment.api.index.getData"
    );
    app._postNetWork({
      url: urlStr,
      data: {
        art: "getRecord",
      },
      success: (resdata) => {
        let res = resdata.data;
        if (res.result != 1) return;
        this.data.total_page = res.data.getRecord.last_page;
        if (res.data.getRecord.current_page >= res.data.getRecord.last_page) {
          that.setData({
            isLoadMore: false,
          });
        }
        this.setData({
          listData: res.data.getRecord.data,
        });
      },
      fail: function (res) {
        console.log(res.msg);
      },
    });
  },
  gotoDetails(event) {
    let item = event.currentTarget.dataset.item;
    wx.navigateTo({
      url:
        "/packageE/healthy/healthyDetails/healthyDetails?record_id=" + item.id,
    });
  },
});
