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

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

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

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

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

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

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

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

  _getMoreData() {
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.data.isLoadMore = false;
      let urlStr = app.getNetAddresss(
        "plugin.health-assessment.api.index.getData"
      );
      this.data.page++;
      let obj = {};
      obj.art = "index";
      obj.page = this.data.page;
      if (this.data.searchText != "") obj.keyword = this.data.searchText;
      app._postNetWork({
        url: urlStr,
        showToastIn: true,
        maskIn: false,
        data: obj,
        success: (resdata) => {
          var res = resdata.data;
          if (res.result != 1) {
            return;
          }
          this.data.isLoadMore = true;
          let indexData = res.data.index;
          let listData = this.data.listData;
          listData.push(...indexData.health.data);
          this.setData({ listData });

          if (indexData.health.current_page >= indexData.health.last_page) {
            this.data.isLoadMore = false;
          }
        },
        fail: function (res) {
          console.log(res.msg);
        },
      });
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  gotoRecord() {
    wx.navigateTo({
      url: "/packageE/healthy/healthyRecord/healthyRecord",
    });
  },
  gotoHealthyEv(event) {
    let id = event.currentTarget.dataset.id;
    wx.navigateTo({
      url: "/packageE/healthy/healthyEv/healthyEv?health_id=" + id,
    });
  },
  searchHealthy(event) {
    let value = event.detail.value;
    if (value == this.data.searchText) return;
    this.data.searchText = value;
    this.getIndexData(value);
  },
  getIndexData() {
    let urlStr = app.getNetAddresss(
      "plugin.health-assessment.api.index.getData"
    );
    let obj = {};
    obj.art = "index";
    if (this.data.searchText != "") obj.keyword = this.data.searchText;
    this.data.isLoadMore = false;
    this.data.page = 1;
    app._postNetWork({
      url: urlStr,
      showToastIn: true,
      maskIn: false,
      data: obj,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result != 1) {
          return;
        }
        this.data.isLoadMore = true;
        let indexData = res.data.index;
        this.setData({
          indexBannerImg: indexData.banner.health_banner,
          indexBannerPage: indexData.banner.health_banner_small,
          listData: indexData.health.data,
        });
        this.data.total_page = indexData.health.last_page;
        if (indexData.health.current_page >= indexData.health.last_page) {
          this.data.isLoadMore = false;
        }
        console.log(indexData.health.data);
      },
      fail: function (res) {
        console.log(res.msg);
      },
    });
  },
});
