var app = getApp()
var that = null
Page({
    data: {
        navLeftItems: [],
        navRightItems:[],
        products: [],
        Start: 0,
        categoryid: 0,
        categoryid0: 0,
        navindex:0,
        navindex0:-1,
        loadingHidden:false
    }, 
    // 页面加载
   onLoad: function () {
    that = this
    that.getlist(0);//第一次加载数据
 },
 // 下拉刷新
 onPullDownRefresh: function () {
    that.clearCache();
    //在当前页面显示导航条加载动画
wx.showNavigationBarLoading(); 
//显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
wx.showLoading({
 title: '刷新中...',
})
   that.getlist(1);//第一次加载数据
},

 // 页面上拉触底事件（上拉加载更多）
onReachBottom: function () {
    that.setData({
        loadingHidden:false
    }); 
    that.getlist(2);//后台获取新数据并追加渲染
},

// 清缓存
clearCache:function(){
that.setData({
     products: [],
     Start: 0,
     loadingHidden:false
}); 
},
 getlist: function (gp) {
  
        var userId = wx.getStorageSync("userId")
        var LoginId = wx.getStorageSync("LoginId")
        wx.request({
          url: app.globalData.apiurl + "/xapiajax.ashx", 
          data: {
             action:"getshopcategory",
             categoryid:that.data.categoryid,
             categoryid0:that.data.categoryid0,
             Start:that.data.Start,
             userId:(!userId?"":userId),
             LoginId:(!LoginId?"":LoginId)
          },
          method:"GET",
          dataType:"json", 
          header: { 
            "content-type": "application/json" // 默认值
          },
          success (res) { 
           
            if(that.data.Start==0){
                var index=that.data.navindex;
               
                var addlb=[{header:"全部分类",image:"",productlb:[],id:0}];
                var productlb = null;
                productlb = addlb;
                if(productlb,res.data.productlb.length>0){
                   productlb.push.apply(productlb,res.data.productlb);
                }
               
                var productlb2=productlb[index].productlb;
                
                        that.setData({
                            navLeftItems: productlb,
                            navRightItems: productlb2,
                            navindex:index
                        })    
            }
           
            var tmpArr = null;
            tmpArr = that.data.products;
            if(res.data.productlist.length>0){
                  tmpArr.push.apply(tmpArr,res.data.productlist);
                 
            }
           
                    that.setData({
                        products: tmpArr,
                        Start: that.data.Start+res.data.productlist.length,
                        loadingHidden:true
                    })
                    
                    if(gp==0||gp==1){
                        if(res.data.productlist.length==0){
                            that.setData({
                                nodataHidden:false
                            })
                        }
                    }
                    if(gp==1){
                        //隐藏loading 提示框
                        wx.hideLoading();
                        //隐藏导航条加载动画
                        wx.hideNavigationBarLoading();
                        //停止下拉刷新
                        wx.stopPullDownRefresh();
                    }
            }
        })
    },

    //事件处理函数
    switchRightTab: function(e) {
        var index = e.target.dataset.id;
        var categoryid=that.data.navLeftItems[index].id;
		var productlb2=that.data.navLeftItems[index].productlb;
        that.clearCache();
                    that.setData({
                        Start:0,
                        navindex:index,
                        navindex0:-1,
                        categoryid:categoryid,
                        categoryid0:0,
                        navRightItems: productlb2
                    })
                   
                    that.getlist(1);  
    },
    //事件处理函数
    switchRightTab0: function(e) {
          var index = e.target.dataset.id;
          var categoryid0=0;
          if(index>-1){
            var productlb2=that.data.navLeftItems[that.data.navindex].productlb;
            categoryid0=productlb2[index].id;
          }
          that.clearCache();
                that.setData({
                    Start:0,
                    navindex0:index,
                    categoryid0: categoryid0
                })
                that.getlist(1);
             },ondetail:function(e) {
                var userId = wx.getStorageSync("userId")
                var LoginId = wx.getStorageSync("LoginId")
                if(userId&&LoginId){
                    var id = e.currentTarget.dataset.id
                    wx.navigateTo({  
                        url: "../details/index?id="+id
                    })
                 }else{
                     wx.navigateTo({  
                         url: "../login/index"
                     })   
                  }
              }
})