// packageE/tabcontrol/tabcontrol.js
var app = getApp();
Component({
  properties: {
    datas: {
      type: null,
    },
    component_id: {
      type: null,
    },
    page_id: {
      type: null,
    },
    bottomShowBol: {
      type: null,
    },
    index: {
      type: null,
    },
    bottomShowBolIndex: {
      type: null,
    },
  },
  // 私有数据，可用于模板渲染
  data: {
    emptyImage: "https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/image.png",
    clientWidth: "375",
    U_tabcontrol: true,
    isLast: false,

    active: 0,
    show_text: "没有数据",
    show_page: true,

    currentTime: parseInt(new Date().getTime() / 1000),
    joinListData: [],
    tabIndexActivityId: 0,
    page: 1, //分页数，当前页数
    isLoadMore: true, //判断是否要加载更多的标志
    total_page: 0, //总页数

    group_number: 100, //不设置最多显示100条

    activeIds: [],
  },

  lifetimes: {
    // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
    attached() {},
    moved() {},
    detached() {},
  },

  // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
  attached() {},
  // 此处attached的声明会被lifetimes字段中的声明覆盖
  ready() {
    this.setData({
      clientWidth: wx.getSystemInfoSync().windowWidth,
    });

    if (this.data.bottomShowBolIndex == this.data.index) {
      this.setData({
        isLast: true,
      });
    }
    if (this.data.datas.list.length > 0) {
      this.data.group_number = this.data.datas.group_number
        ? this.data.datas.group_number
        : 100;
      this.data.datas.list.forEach((element) => {
        this.data.activeIds.push(element.activity_id);
      });
      //this.data.tabIndexActivityId = this.data.datas.list[0].activity_id;
      this.getData();
      //this.datas.list[0].activity_id
    }
  },

  pageLifetimes: {
    // 组件所在页面的生命周期函数
    show() {},
    hide() {},
    resize() {},
  },
  methods: {
    init() {
      this.setData({
        isLoadMore: true,
        allLoaded: false,
        page: 1,
      });
    },
    onClick(e) {
      this.init();
      let index = e.detail.index;
      //this.data.tabIndexActivityId = this.data.datas.list[index].activity_id;
      this.setData({
        active: index,
      });
      this.getData();
    },
    gotoStarGroupDetails(evt) {
      let id = evt.currentTarget.dataset.id;
      wx.navigateTo({
        url:
          "/packageH/starMusic/starMusicGroupDetails/starMusicGroupDetails?id=" +
          id,
      });
    },
    getData() {
      let urlStr = app.getNetAddresss(
        "plugin.decorate.admin.decorate-common.searchOpenList"
      );
      app._postNetWork({
        url: urlStr,
        data: {
          activity_id: this.data.activeIds,
          type: this.data.active,
          show_num: 15,
        },
        success: (resdata) => {
          var res = resdata.data;
          if (res.result != 1) {
            return;
          }

          this.data.total_page = res.data.last_page;
          if (!this.data.total_page) {
            this.data.total_page = 0;
          }
          if (res.data.data.length > this.data.group_number) {
            res.data.data.splice(
              this.data.group_number,
              res.data.data.length - this.data.group_number
            );
            this.setData({
              isLoadMore: false,
              allLoaded: true,
            });
          }
          if (
            res.data.data.length < 15 ||
            res.data.last_page == this.data.page
          ) {
            this.setData({
              isLoadMore: false,
              allLoaded: true,
            });
          }
          this.setData({
            joinListData: res.data.data,
          });
        },
      });
    },
    getMoreData() {
      this.setData({
        isLoadMore: false,
      });
      if (this.data.page >= this.data.total_page) {
        return;
      } else {
        this.data.page++;
        let urlStr = app.getNetAddresss(
          "plugin.decorate.admin.decorate-common.searchOpenList"
        );
        app._postNetWork({
          url: urlStr,
          data: {
            page: this.data.page,
            activity_id: this.data.activeIds,
            type: this.data.active,
            show_num: 15,
          },
          success: (resdata) => {
            var res = resdata.data;
            if (res.result != 1) {
              return;
            }

            if (this.data.page < this.data.total_page) {
              this.setData({
                isLoadMore: true,
              });
            } else {
              this.setData({
                allLoaded: true,
              });
            }
            var nextPageData = res.data.data;
            let data = this.data.joinListData.concat(nextPageData);
            if (data.length > this.data.group_number) {
              data.splice(
                this.data.group_number,
                data.length - this.data.group_number
              );
              this.setData({
                isLoadMore: false,
                allLoaded: true,
              });
            }
            if (
              res.data.data.length < 15 ||
              res.data.last_page == this.data.page
            ) {
              this.setData({
                isLoadMore: false,
                allLoaded: true,
              });
            }

            this.setData({
              joinListData: data,
            });
          },
        });
      }
    },
  },
  observers: {
    bottomShowBol: function (newVal) {
      if (newVal && this.data.isLast && this.data.isLoadMore) {
        this.getMoreData();
      }
    },
  },
});
