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

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    this.getGoods();
    wx.setNavigationBarTitle({
      title: '我的库存',
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getGoods();
  },
  onShareAppMessage() {},
  onShareTimeline() {},
  viewDetails(event) {
    const id = event.target.dataset.id;
    wx.navigateTo({
      url: "/packageH/stock/details/details?id=" + id,
    });
  },
  getGoods() {
    if (this.data.loading || this.data.isMoreData === false) {
      return;
    }
    this.setData({
      loading: true,
    });
    let urlStr = App.getNetAddresss("plugin.agency.api.stock.get-list");
    urlStr += "&page=" + this.data.page;
    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 (
          data.current_page === data.last_page ||
          data.data.length === 0 ||
          data.data.length < data.per_page
        ) {
          setData["isMoreData"] = false;
        }
        let goodsList = this.data.goodsList;
        goodsList.push(...data.data);
        setData["goodsList"] = goodsList;
        setData["page"] = this.data.page + 1;
        this.setData(setData);
        wx.hideLoading();
      },
    });
  },
  searchGoods(event) {
    const keyword = event.detail;
    let searchResult = this.data.goodsList.filter((item) => {
      return item.title.indexOf(keyword) !== -1;
    });
    this.setData({
      searchGoodsList: searchResult,
      searchInputKeyword: keyword,
    });
  },
  cancelSearch() {
    this.setData({
      searchGoodsList: null,
    });
  },
});
