var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    language: '',
    shareData: {},
    info: {},
    page: 1, // 分页数，当前页数
    isLoadMore: true, // 判断是否要加载更多的标志
    total_page: 0 // 总页数
  },

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

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let language = wx.getStorageSync('langIndex');
    this.setData({ 'language': language.en});
  },

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

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

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

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

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

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: this.data.shareData.share_title,
      imageUrl: this.data.shareData.share_img,
      path: '/packageB/member/group_buy/group_buy_list/group_buy_list'
    };
  },
  _getData() {
    let urlStr = app.getNetAddresss('plugin.together-purchase.api.index.getTogetherData');
    urlStr += '&art=polymerization'; 
    app._getNetWork({
      url: urlStr,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          this.setData({
            isLoadMore: true,
            total_page: res.data.polymerization.last_page
          });
          if (!this.data.total_page) {
            this.setData({
              total_page: 0
            });
          }
          this.setData({
            info: res.data.polymerization.data
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      }
    });
  },
  getShare() {
    let urlStr = app.getNetAddresss('plugin.together-purchase.api.index.getTogetherData');
    urlStr += '&art=getShare'; 
    app._getNetWork({
      url: urlStr,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          this.setData({
            shareData: res.data.getShare
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      }
    });
  },
  // 获取更多数据，加载更多
  getMoreData() {
    this.setData({
      isLoadMore: false
    });
    // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      let page = this.data.page;
      page += 1;
      this.setData({
        page});
      let urlStr = app.getNetAddresss('plugin.together-purchase.api.index.getTogetherData');
      urlStr += '&page=' + this.data.page + '&art=polymerization'; 
      app._getNetWork({
        url: urlStr,
        success: (resdata) => {
          var res = resdata.data;
          this.setData({
            isLoadMore: true
          });
          if (res.result == 1) {
            var nextPageData = res.data.polymerization.data;
            this.setData({
              info: this.data.info.concat(nextPageData)
            });
          // 进行数组拼接
          } else {
            let pages = this.data.page;
            pages--;
            this.setData({
              page: pages,
              isLoadMore: false
            });
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        }
      });
    }
  },
  goGroup(e) {
    let id = e.target.dataset.goods_id || e.currentTarget.dataset.goods_id;
    let aid = e.target.dataset.activity_id || e.currentTarget.dataset.activity_id;
    console.log(id, aid, '参数');

    wx.navigateTo({
      url: '/packageA/detail_v2/detail_v2?id=' + id + '&activity_id='+aid+'&name=group_buy'
    });
  }

});
