var WxParse = null;
var app = getApp()
var that=null
var that0=null
Page( {
  data: {
    loadingHidden:false,
    nodataHidden:true,
    indicatorDots: true,
        indicatorDots0: true,
        vertical: false,
        vertical0: false,
        autoplay: true,
        interval: 6000, 
        interval0: 8000, 
        duration: 1000,
        duration0: 1000,
        business:[],
        navData:[],
        nav:0,
    Start: 0,
    qkeyword:"请输入关键字",
    keyword:"",
  },onShow: function () {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      that0=this;
      var userId = wx.getStorageSync("userId")
      var userInfodata = wx.getStorageSync("userInfo")
        var sel=2;
        var ifsel=0;
        if(userId&&userInfodata){
          var userInfo = JSON.parse(userInfodata)
         
            ifsel=userInfo.bdshEnable;
        }
        
        if(ifsel==1){
        that0.getTabBar().setData({
        "list": [
          {
            "pagePath": "/pages/index/index",
            "text": "首页",
            "iconPath": "/images/footer-icon-01.png",
            "selectedIconPath": "/images/footer-icon-01-active.png"
          },
          {
            "pagePath": "/pages/news/index",
            "iconPath": "/images/footer-icon-05.png",
            "selectedIconPath": "/images/footer-icon-05-active.png",
            "text": "资讯"
          },

          {
            "pagePath": "/pages/my/index",
            "text": "我的",
            "iconPath": "/images/footer-icon-04.png",
            "selectedIconPath": "/images/footer-icon-04-active.png"
          }
        ]
        })
      }else{
        that0.getTabBar().setData({
          "list": [
            {
              "pagePath": "/pages/index/index",
              "text": "首页",
              "iconPath": "/images/footer-icon-01.png",
              "selectedIconPath": "/images/footer-icon-01-active.png"
            },
            {
              "pagePath": "/pages/news/index",
              "iconPath": "/images/footer-icon-05.png",
              "selectedIconPath": "/images/footer-icon-05-active.png",
              "text": "资讯"
            },
            {
              "pagePath": "/pages/business/index",
              "iconPath": "/images/footer-icon-02.png",
              "selectedIconPath": "/images/footer-icon-02-active.png",
              "text": "门店"
            },
  
            {
              "pagePath": "/pages/my/index",
              "text": "我的",
              "iconPath": "/images/footer-icon-04.png",
              "selectedIconPath": "/images/footer-icon-04-active.png"
            }
          ]
          })
        }
        that0.getTabBar().setData({
          selected: sel
        })
    }
  },

  onLoad: function(options) {
    that = this
    wx.setBackgroundColor({
      backgroundColor: "#00B050", 
      })
    that.clearCache();//清本页缓存
    that.getlist(0);//第一次加载数据
    var sopenFrame0=setInterval(function (){
      clearInterval(sopenFrame0);   
     wx.getLocation({
      type: 'gcj02', // 比较精确
      success: (res) => {
        wx.setStorageSync("latitude", res.latitude)
        wx.setStorageSync("longitude", res.longitude) 
        that.clearCache();//清本页缓存
        that.getlist(0);
      },fail: (err) => {
        /*wx.showToast({
          title: JSON.stringify(err),
          icon: "none",
          duration: 20000
        })*/
      }
      
    })
    },1000);
    },
    
    // 下拉刷新
    onPullDownRefresh: function () {
       //在当前页面显示导航条加载动画
     wx.showNavigationBarLoading(); 
     //显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
     wx.showLoading({
         title: '刷新中...',
     })
    
        that.clearCache();
       that.getlist(1);//第一次加载数据
    },

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

 // 清缓存
 clearCache:function(){
    that.setData({
         Start: 0,
         loadingHidden:false
    }); 
}, getlist: function (gp) {
     
      var userId = wx.getStorageSync("userId")
      var LoginId = wx.getStorageSync("LoginId")
      var latitude = wx.getStorageSync("latitude")
      var longitude = wx.getStorageSync("longitude")
      
      wx.request({
        url: app.globalData.apiurl + "/xapiajax.ashx", 
        data: {
          action:"getBusinessdata",
           userId:(!userId?"":userId),
           LoginId:(!LoginId?"":LoginId),
           keyword:that.data.keyword,
           t:(that.data.nav>0?that.data.nav:""),
           Start:that.data.Start,
           latitude:(!latitude?"":latitude),
           longitude:(!longitude?"":longitude),
        },
        method:"GET",
        dataType:"json", 
        header: { 
          "content-type": "application/json" // 默认值
        },
        success (res) {
          if(res.data.status==1){
              var tmpArr = null;
              if(gp==0||gp==1){
                tmpArr = res.data.Businesslist
              }else{
                 tmpArr = that.data.business;
                 tmpArr.push.apply(tmpArr,res.data.Businesslist);
              }
              that.setData({
                  business: tmpArr,
                  Start: that.data.Start+res.data.Businesslist.length,
                  loadingHidden:true
              }) 
              if(gp==0||gp==1){
              that.setData({
                navData:res.data.lbs
              })
                if(res.data.Businesslist.length==0){
                    that.setData({
                        nodataHidden:false
                    })
                }else{
                  that.setData({
                    nodataHidden:true
                   })
                }
              }
             }
              else{
                if(res.data.msg=="您未登录"){
                    wx.removeStorageSync('userId')
                    wx.removeStorageSync('LoginId')
                }
           }
              if(gp==0||gp==1){
                  //隐藏loading 提示框
          wx.hideLoading();
          //隐藏导航条加载动画
          wx.hideNavigationBarLoading();
          //停止下拉刷新
          wx.stopPullDownRefresh();
               
              }
          }
      })
  },onsearch:function() {
    var keyword=that.data.keyword.replace(/\s+/g, "");
    if(keyword!=""){
      that.setData({
        keyword:keyword
       })
       that.clearCache();//清本页缓存
       that.getlist(0);//第一次加载数据
    }else{
      wx.showToast({
        title: "关键字不能为空",
        icon: "none",
        duration: 3000 
       })
    }
  },swiperchange0:function(e) {

  },closeserver:function(e) {
  that.setData({
    bodyHidden:true
 })
},inserver :function(e) {
   
},onnav:function(e) {
  var index = e.currentTarget.dataset.id
  that.setData({
      nav:index
  })
  
  that.clearCache();
  that.getlist(0);
},onamap:function(e) {
  var address = e.currentTarget.dataset.id
  var lat = e.currentTarget.dataset.lat
  var lon = e.currentTarget.dataset.lon
  wx.navigateTo({  
    url: "../map/index?lat=" + lon + "&lon=" + lat + "&address=" + address
   })
},oncopy0:function(e) {
  var address = e.currentTarget.dataset.id
  wx.setClipboardData({
    data: address,
    success: function (res) { 
      wx.showToast({
        title: "复制成功"
      })
    }
  })
},onview:function(e) {
  var id = e.currentTarget.dataset.id
  wx.navigateTo({  
    url: "../businessview/index?id=" + id
   })
}
})
