var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    language: '',
    info: {},
    page: 1, // 分页数，当前页数
    isLoadMore: true, // 判断是否要加载更多的标志
    total_page: 0, // 总页数
    groupShareStatus:false,
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.types == "zhpGroup") {
      this.setData({
        type: "zhpGroup",
      });
    }
    this._getData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  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("没有更多数据");
      return;
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    var value = wx.getStorageSync('yz_uid');
    var mid = '';
    if (value) {
      mid = value;
    }
    if(res.from == 'button' && res.target.dataset.tag == 'shareTag'){
      if(res.target.dataset.item == '1'){
        let _path = '/packageB/member/group/GroupDetail/GroupDetail?id=' + res.target.dataset.info.id + '&mid=' + mid;
        let _title = res.target.dataset.title;
        if(res.target.dataset.store){
          _path = '/packageB/member/group/GroupDetail/GroupDetail?id=' + res.target.dataset.info.id +'store='+ res.target.dataset.store + '&mid=' + mid;
        }
        if(app.globalData.store_alone_temp == 1 && that.globalData.STORE_ID){
          _path = `${_path}&store_alone_id=${app.globalData.STORE_ID}`; 
        }
        console.log(_path);
        return{
          path:_path,
          title:_title
        };
      }
    }

    if(this.data.type == 'zhpGroup'){
      return {
        title: "拼团列表",
        path: "/packageB/member/group/GroupList/GroupList?types=zhpGroup",
      };
    }
    // 门店独立模板
    if(app.globalData.store_alone_temp == 1 && that.globalData.STORE_ID){
      return {
        path:`/packageB/member/group/GroupList/GroupList?store_alone_id=${app.globalData.STORE_ID}`
      };
    }
  },
  _getData() {
    let urlStr;
    if (this.data.type == "zhpGroup") {
      urlStr = app.getNetAddresss("plugin.zhp-group-lottery.frontend.activity.get-activity-list");
      wx.setNavigationBarTitle({
        title: '拼团列表',
      });
    } else {
      urlStr = app.getNetAddresss("plugin.fight-groups.frontend.controllers.fight-groups.index");
    }
    app._getNetWork({
      url: urlStr,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          this.setData({
            isLoadMore: true,
            total_page: res.data.last_page,
          });
          if (!this.data.total_page) {
            this.setData({
              total_page: 0,
            });
          }
          this.setData({
            info: res.data.data,
          });
        } 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;
      let urlStr;
      page += 1;
      this.setData({
        page,
      });
      if (this.data.type == "zhpGroup") {
        urlStr = app.getNetAddresss("plugin.zhp-group-lottery.frontend.activity.get-activity-list");
      } else {
        urlStr = app.getNetAddresss("plugin.fight-groups.frontend.controllers.fight-groups.index");
      }
      urlStr += "&page=" + this.data.page;
      app._getNetWork({
        url: urlStr,
        success: (resdata) => {
          var res = resdata.data;
          this.setData({
            isLoadMore: true,
          });
          if (res.result == 1) {
            var nextPageData = res.data.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,
            });
          }
        },
      });
    }
  },
  orderGo(e){
    if(e.currentTarget.dataset.key == 'finance'){
      wx.navigateTo({
        url: '/packageI/newGroup/zhpFinance/zhpFinance',
      });
      return;
    }
    wx.navigateTo({
      url: '/packageB/member/group/MyGroups/MyGroups?types=zhpGroup',
    });
  },
  goGroup(e) {
    if (this.data.type == "zhpGroup") {
      let id = e.currentTarget.dataset.id;
      wx.navigateTo({
        url: "/packageA/detail_v2/detail_v2?id=" + id + "&name=zhpGroup",
      });
      return;
    } else {
      let id = e.target.dataset.id || e.currentTarget.dataset.id;
      let store_id = e.target.dataset.store_id || e.currentTarget.dataset.store_id;

      if (store_id) {
        wx.navigateTo({
          url: "/packageA/detail_v2/detail_v2?id=" + id + "/" + store_id + "&name=group",
        });
      } else {
        wx.navigateTo({
          url: "/packageA/detail_v2/detail_v2?id=" + id + "&name=group",
        });
      }
      return;
    }
  },
});
