var app = getApp()
var that = null
Page({
    data: {
        products: [],
        categoryid: 0,
        keyword:"",
        Recount:0,
        Start: 0,
        t:0, 
        onsearchHidden:true,
        loadingHidden:false,
        nodataHidden:true
    },
    onLoad: function(options) {
            that = this
            that.clearCache();//清本页缓存
            if(options.id&&options.title){
                that.setData({
                    categoryid:options.id
                })
                wx.setNavigationBarTitle({
                    title: options.title 
                })
            }
            if(options.t){
              that.setData({
               t:options.t
              })
            }
            if(options.keyword){
                that.setData({
                    keyword:options.keyword,
                    onsearchHidden:false
                })
                wx.setNavigationBarTitle({
                    title: "搜索商品"
                }) 
            }
            
            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:"getwxshoplist",
             userId:(!userId?"":userId),
             LoginId:(!LoginId?"":LoginId),
             categoryid:that.data.categoryid,
             keyword:that.data.keyword,
             Start:that.data.Start
          },
          method:"GET",
          dataType:"json", 
          header: { 
            "content-type": "application/json" // 默认值
          },
          success (res) {
                var tmpArr = null;
                tmpArr = that.data.products;
                tmpArr.push.apply(tmpArr,res.data.productlist);
                that.setData({
                    products: tmpArr,
                    Start: that.data.Start+res.data.productlist.length,
                    Recount:res.data.Recount,
                    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();
                }
            }
        })
    },gotodetail: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"
             })   
          }
      }

})