// packageH/starMusic/starMusicPersonalGroup/starMusicPersonalGroup.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    nowTime: parseInt(new Date().getTime() / 1000),
    pages: "open",

    nickname: "",
    dividend_name: "",
    avatar: "",
    is_self: 0,
    title: "开团",
    listData: [],
    page: 1, //分页数，当前页数
    isLoadMore: true, //判断是否要加载更多的标志
    total_page: 0, //总页数

    member_id: 1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.mid) {
      app._setMid(option.mid);
    }
    if (options.pages && options.pages == "join") {
      this.data.pages = options.pages;
      this.data.title = "参团";
      wx.setNavigationBarTitle({
        title: this.data.title,
      });
    }
    if (options.member_id && options.member_id > 0) {
      this.data.member_id = options.member_id;
    } else {
      this.data.member_id = wx.getStorageSync("yz_uid");
    }
    this.getData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

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

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

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

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

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

  gotoOrderDetail(evt) {
    let id = evt.currentTarget.dataset.id;
    wx.navigateTo({
      url:
        "/packageA/member/orderdetail/orderdetail?orderType=shop&order_id=" +
        id,
    });
  },

  getData() {
    let urlStr = app.getNetAddresss(
      "plugin.star-spell.frontend.index.myOpenTeam"
    );
    if (this.data.pages == "join") {
      urlStr = app.getNetAddresss(
        "plugin.star-spell.frontend.index.myJoinTeam"
      );
    }
    app._getNetWork({
      url: urlStr,
      data: {
        member_id: this.data.member_id,
      },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result != 1) return app.tips(res.msg);

        if (res.data.is_self == 1) {
          this.data.title = "我的" + this.data.title;
          wx.setNavigationBarTitle({ title: this.data.title });
        }

        this.data.isLoadMore = true;
        this.data.total_page = res.data.last_page;
        if (!this.data.total_page) {
          this.data.total_page = 0;
        }
        this.setData({
          avatar: res.data.avatar,
          dividend_name: res.data.dividend_name,
          nickname: res.data.nickname,
          is_self: res.data.is_self,
          listData: res.data.data,
        });
      },
    });
  },

  //加载更多数据
  _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.star-spell.frontend.index.myOpenTeam"
      );
      if (this.data.pages == "join") {
        urlStr = app.getNetAddresss(
          '"plugin.star-spell.frontend.index.myJoinTeam'
        );
      }
      app._getNetWork({
        url: urlStr,
        data: {
          member_id: this.data.member_id,
          page: this.data.page,
        },
        success: (resdata) => {
          let res = resdata.data;
          this.data.isLoadMore = true;
          if (res.result === 1) {
            var nextPageData = res.data.data;
            let listData = this.data.listData.concat(nextPageData);
            this.setData({ listData });
          } else {
            this.data.page = this.data.page - 1;
            this.data.isLoadMore = false;
          }
        },
      });
    }
  },

  gotoStarMusicGroupDetails(evt) {
    console.log(evt.currentTarget.dataset);
    let leaderid = evt.currentTarget.dataset.leaderid;
    wx.navigateTo({
      url:
        "/packageH/starMusic/starMusicGroupDetails/starMusicGroupDetails?id=" +
        leaderid,
    });
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let path =
      "/packageH/starMusic/starMusicPersonalGroup/starMusicPersonalGroup?member_id=" +
      this.data.member_id;
    console.log("pages", this.data.pages);
    if (this.data.pages == "join") {
      path += "&pages=join";
    }

    let value = wx.getStorageSync("yz_uid");
    let mid = "";
    if (value) {
      mid = value;
      page += "&mid=" + mid;
    }

    return {
      path,
    };
  },
});
