// packageI/brand_resources/brandResourcesIndex/brandResourcesIndex.js
const app = getApp();
let isScroll=true;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeName:0,
    banner: [],
    recommend: [],
    category: [],
  },

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

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

  },

  tapUrl(e) {
    let link = e.currentTarget.dataset.url;
    if (link == "") return false;
    try {
      wx.navigateTo({
        url: link,
      });
    } catch (err) {
      console.log(err);
    }
  },
  toGood(evt) {
    let id = evt.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/packageA/detail_v2/detail_v2?id='+id,
    });
  },
  toCate(evt) {
    let id = evt.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/packageB/member/category/search_v2/search_v2?id='+id,
    });
  },
  getData(){
    let urlStr = app.getNetAddresss('plugin.yz-supply-camilo-resources.frontend.index.index');
    app._postNetWork({
      url: urlStr,
      success: (resdata) => {
        let res = resdata.data;
        if(res.result != 1){
          return app.tips(res.msg);
        }
        let changeData={};
        changeData.banner = res.data.banner || [];
        changeData.recommend = res.data.recommend || [];
        changeData.category=[];
        if(res.data.category.recommand_category && res.data.category.recommand_category.length>0){
          changeData.category.push({
            id: 0,
            name: '推荐分类',
            children: res.data.category.recommand_category
          });
        }
        res.data.category.categorys.forEach((item)=>{
          if(item.children && item.children.length>0){
            changeData.category.push(item);
          }
        });
        this.setData(changeData);
      },
      fail: function (res) {
        console.log(res.msg);
      }
    });
  },

  ondisplay(evt) {
    if(!isScroll) return;
    let index = evt.detail.index;
    let display = evt.detail.display;
    let type = evt.detail.type;
    if(type=="up"){
      if(display){
        this.setData({
          activeName:index
        });
      }else {
        this.setData({
          activeName:index+1
        });
      }
    }

  },

  changeTabName(evt){
    console.log(evt);
    let index = evt.detail.index;
    if(index == this.data.activeName) return ;
    this.setData({
      activeName:evt.detail.index
    });
    
    wx.pageScrollTo({
      selector: '.category'+index,
      duration: 500
    });
    isScroll = false;
    setTimeout(()=>{
      isScroll = true;
    },500);

  },

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

  },

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

  },

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

  },

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

  },

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

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
});