// packageH/cpsCard/cpsCard.js
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    hot: [],
    category: [],
    acindex: 0,
    tabsHeight: "",
    topHeight: "",
    currBox: "tab0",
    heightArray: [],
    toview: "ind0",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getdata();
  },
  goDetail(e) {
    console.log(e);
    let id = e.currentTarget.dataset.id;
    let name = e.currentTarget.dataset.name;
    wx.navigateTo({
      url: "/packageH/cpsList/cpsList?id=" + `${id}&name=${name}`,
    });
    // wx.navigateTo({
    //   url: 'packageD/buy/myOrder_v2/myOrder_v2?tag=-2'`&goodsId=${id}&optionsId=&total=${num}`,
    // })
  },
  taptest(e) {
    let ind = e.currentTarget.dataset.ind;
    this.setData({
      acindex: ind,
    });
    this.setData({
      toview: `ind${ind}`,
    });
  },
  tabScroll(e) {},
  bincroll(e) {
    // console.log(e.detail.scrollTop)
    let scrollTop = e.detail.scrollTop;

    if (scrollTop <= this.data.topHeight && this.data.curr) {
      this.setData({
        curr: false,
      });
    } else if (scrollTop > this.data.topHeight && !this.data.curr) {
      this.setData({
        curr: true,
      });
    }
    for (let i = 0; i < this.data.category.length; i++) {
      if (this.data.heightArray[1] >= e.detail.scrollTop) {
        this.setData({
          acindex: 0,
          currBox: "tab0",
        });
      } else if (
        this.data.heightArray[i] < e.detail.scrollTop + 50 &&
        e.detail.scrollTop + 50 < this.data.heightArray[i + 1]
      ) {
        this.setData({
          acindex: i,
          currBox: "tab" + i,
        });
        break;
      } else if (
        this.data.heightArray[this.data.category.length - 1] <=
        e.detail.scrollTop + 50
      ) {
        this.setData({
          acindex: this.data.category.length - 1,
          currBox: "tab" + this.data.category.length - 1,
        });
      }
      // console.log(this.data.heightArray[i],e.detail.scrollTop,this.data.heightArray[i+1],i)
    }
  },
  testTabs() {
    // 获取全部分类box高度   -100？
    let that = this;
    let heightArray = [];
    for (let i = 0; i < this.data.category.length; i++) {
      wx.createSelectorQuery()
        .select(`#ind${i}`)
        .boundingClientRect(function (rect) {
          heightArray.push(rect.top - that.data.topHeight - 100);
          if (i == that.data.category.length - 1) {
            that.setData({
              heightArray: heightArray,
            });
          }
          // console.log(heightArray)
        })
        .exec();
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let that = this;
    wx.createSelectorQuery()
      .select(".page-section-spacing")
      .boundingClientRect(function (rect) {
        console.log(rect);
        that.setData({
          tabsHeight: rect.top,
        });
      })
      .exec();
  },

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

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

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

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

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  getdata() {
    wx.showLoading({
      title: "加载中",
    });
    let that = this;
    let urlStr = app.getNetAddresss("plugin.aggregation-cps.api.equity");
    app._getNetWork({
      url: urlStr,
      success: (resdata) => {
        let res = resdata.data;
        wx.hideLoading();
        if (res.result == 1) {
          that.setData({
            hot: res.data.hot,
            category: res.data.category,
            brand: res.data.category[0].brand,
          });
          setTimeout(() => {
            wx.createSelectorQuery()
              .select(".top")
              .boundingClientRect(function (rect) {
                that.setData({
                  topHeight: rect.height,
                });
              })
              .exec();
            that.testTabs();
          }, 200);
        } else {
          wx.showToast({
            title: res.msg,
            duration: 1000,
            icon: "none",
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      },
    });
  },
});
