// packageH/stock/details/details.js
const App = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    detailId: null,
    goods: null,
    details: [],
    isMoreData: true,
    loading: false,
    page: 1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const id = parseInt(options.id);
    if (!id) {
      wx.showToast({
        title: "库存明细不存在",
      });
      return;
    }
    wx.setNavigationBarTitle({
      title: '库存明细',
    });
    this.setData({
      detailId: id,
    });
    this.getDetails();
  },
  onShareAppMessage() {},
  onShareTimeline() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getDetails();
  },
  getDetails() {
    if (this.data.loading === true || this.data.isMoreData === false) {
      return;
    }
    this.setData({
      loading: true,
    });
    wx.showLoading({
      title: "加载中",
      mask: true,
    });
    let urlStr = App.getNetAddresss("plugin.agency.api.stock.log-list");
    urlStr += "&page=" + this.data.page + "&goods_id=" + this.data.detailId;
    App._postNetWork({
      url: urlStr,
      success: ({ data: { data, result, msg } }) => {
        if (result === 0) {
          wx.hideLoading();
          wx.showToast({
            title: msg,
            success: () => {
              wx.redirectTo({
                url: "/packageG/member_v2/member_v2",
              });
            },
          });
          return;
        }

        const setData = {
          loading: false,
        };
        if (this.data.goods === null) {
          setData["goods"] = data.goods;
        }
        if (
          data.current_page === data.last_page ||
          data.data.length === 0 ||
          data.data.length < data.per_page
        ) {
          setData["isMoreData"] = false;
        }
        let details = this.data.details;
        details.push(...data.data);
        setData["details"] = details;
        setData["page"] = this.data.page + 1;
        this.setData(setData);
        wx.hideLoading();
      },
    });
  },
});
