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

  /**
   * 页面的初始数据
   */
  data: {
    info: {},
    goodsList: [],
    //more
    isLoadMore: true,
    page: 1,
    total_page: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData();
  },
  getData() {
    let that = this;
    let urlStr = app.getNetAddresss('plugin.new-retail.frontend.index.index');
    app._postNetWork({
      url: urlStr,
      data: {},
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          that.setData({
            info: res.data
          });
          wx.setNavigationBarTitle({
            title: res.data.project_name
          });
          that.getGood();
        } else {
          wx.showToast({
            title: res.msg,
            duration: 1000,
            icon: 'none'
          });
        }
      },
      fail: function (res) {
        wx.hideLoading();
        console.log(res.msg);
      }
    });
  },
  getGood() {
    let that = this;
    let urlStr = app.getNetAddresss('plugin.new-retail.frontend.index.goodsList');
    app._postNetWork({
      url: urlStr,
      data: {},
      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.showToast({
            title: res.msg,
            duration: 1000,
            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 = app.getNetAddresss('plugin.new-retail.frontend.index.goodsList');
      app._postNetWork({
        url: urlStr,
        data: {
          page: this.data.page
        },
        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;
          }
        }
      });
    }
  },
  gotoUrl(e) {
    let tag = e.currentTarget.dataset.tag;
    if (tag == 0) {
      wx.navigateTo({
        url: '/packageH/newRetail/newRetailSalesExtension/newRetailSalesExtension'
      });
    }else if (tag == 1) {
      wx.navigateTo({
        url: '/packageH/newRetail/newRetailSearch/newRetailSearch?t=2'
      });
    }else if(tag == 2) {
      wx.navigateTo({
        url: '/packageH/newRetail/newRetailSearch/newRetailSearch?t=3'
      });
    }else if(tag == 3) {
      wx.navigateTo({
        url: '/packageH/newRetail/newRetailInventory/newRetailInventory'
      });
    }else if(tag == 4) {
      wx.navigateTo({
        url: '/packageH/newRetail/newRetailSalesRecord/newRetailSalesRecord'
      });
    }else if(tag == 5) {
      wx.navigateTo({
        url: '/packageA/member/myOrder_v2/myOrder_v2?status=0&orderType=newRetail'
      });
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

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

  },

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

  },

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

  },

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

  },

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

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

  }
});