// packageH/newRetail/newRetailSearch/newRetailSearch.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageType: null,
    titleName: "",
    searchValue: "",
    goodsList: [],
    //more
    page: 1, //分页数，当前页数
    isLoadMore: true, //判断是否要加载更多的标志
    total_page: 0 //总页数
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.t) {
      this.setData({
        pageType: options.t
      });
      let titleName = options.t == 2 ? "代理套餐" : "进货";
      wx.setNavigationBarTitle({
        title: titleName
      });
    }
    this.getGood();
  },
  onSearchChange(e){
    this.setData({
      searchValue: e.detail,
    });
  },
  getGood() {
    let that = this;
    let urlStr = null;
    if (this.data.pageType == 2) {
      urlStr = app.getNetAddresss("plugin.new-retail.frontend.index.AgentPackage");
      urlStr += `&goods_name=${this.data.searchValue || ''}`;
    } else {
      urlStr = app.getNetAddresss("plugin.new-retail.frontend.index.purchaseIndependently");
      urlStr += `&search[goods_name]=${this.data.searchValue || ''}`;
    }

    app._getNetWork({
      url: urlStr,
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          that.setData({
            goodsList: res.data.data,
            isLoadMore: true,
            total_page: res.data.last_page
          });
        } else {
          wx.navigateBack({
            delta: 1
          });
          wx.showToast({
            title: res.msg,
            duration: 2000,
            icon: 'none'
          });
        }
      },
      fail: function (res) {
        wx.hideLoading();
        console.log(res.msg);
      }
    });
  },
  _getMoreData() {
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      // that.loading = true;
      return;
    } else {
      this.data.page += 1;
      let urlStr = null;
      if (this.data.pageType == 2) {
        urlStr = app.getNetAddresss("plugin.new-retail.frontend.index.AgentPackage");
        urlStr += `&goods_name=${this.data.searchValue || ''}`;
      } else {
        urlStr = app.getNetAddresss("plugin.new-retail.frontend.index.purchaseIndependently");
        urlStr += `&search[goods_name]=${this.data.searchValue || ''}`;
      }
      urlStr += `&page=${this.data.page}`;
      app._postNetWork({
        url: urlStr,
        success: (resdata) => {
          let res = resdata.data;
          this.data.isLoadMore = true;
          if (res.result === 1) {
            var nextPageData = res.data.data;
            let goodsList = this.data.goodsList.concat(nextPageData);
            this.setData({
              goodsList
            });
          } else {
            this.data.page = this.data.page - 1;
            this.data.isLoadMore = false;
          }
        }
      });
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

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

  },

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

  },

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

  },

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

  },

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

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
});