var app = getApp()
var that=null
Page( {
  data: {
    loadingHidden:false,
    myaddress:[]
  },

  onLoad: function() {
      that = this
      wx.setBackgroundColor({
        backgroundColor: "#96C6F5", 
        })
      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);
      // var addresss = JSON.parse(wx.getStorageSync("addresss"))
      // that.setData({
      //     loadingHidden:true,
      //     myaddress:addresss
      // })
    }
   ,select:function(e) {
          var index = e.currentTarget.dataset.id
          var addressId=that.data.myaddress[index].id
          var type=that.data.myaddress[index].type
          var contact=that.data.myaddress[index].contact
          var tel=that.data.myaddress[index].tel
          var area=that.data.myaddress[index].area
          var address=that.data.myaddress[index].address
          var distance=that.data.myaddress[index].distance
          app.globalData.selladdress0={addressId:addressId,type:type,contact:contact,tel:tel,area:area,address:address,distance:distance}
          app.globalData.IFRefreshsell0=1
          wx.navigateBack({})
     
    },
     // 清缓存
 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);
            }
            // 转换Businesslist数据为myaddress期望的格式
            var myaddressData = res.data.Businesslist.map(function(item) {
              return {
                id: item._id,
                contact: item.name,
                tel: item.phone,
                area: '',
                address: item.address,
                distance: item.distance
              };
            });
            that.setData({
                business: tmpArr,
                myaddress: myaddressData,
                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();
             
            }
        }
    })
},
})
