var o=getApp().globalData.setting;
Page({
  data:{
	  is_get_local_ok:0,
	  is_gps:0,
	  lat:null,
	  lon:null,
	  page:1,
	  cat_list:null,
	  pick_list:null,
	  url:o.imghost,
	  is_no_more:0,
	  key_word:'',   //关键字搜索
	  cat_id:0,      //分类ID
	  islading:0,
  },	

  //调用视频接口
  onLoad:function(e){
	  var th=this;
	   wx.getLocation({
	     type: 'gcj02',
	     success: function(res) {	   
	        th.data.lat = res.latitude;
	        th.data.lon = res.longitude;
	        th.data.is_get_local_ok = 1;	     	      
	     },
	     fail: function(res) {
	   	    th.data.is_get_local_ok = 1;	   
	        if (res.errCode == 2) {
	             getApp().confirmBox("请开启GPS定位", null, 25000, !1);	        
	        }
	   
	     }
	   }) 
	   //--获取门店分类---
	   getApp().request.get("/api/weshop/storagecategory/page", {
		   data: {
			 store_id: o.stoid,
			 is_show: 1,
			 pageSize: 300
		   },
		   success:function(res){
			   if(res.data.code==0){
				   th.setData({cat_list:res.data.data.pageData})
			   }
		   },
	   })
	   this.get_sto();
  },
  
  //---------拿出门店分类和门店------------
  get_sto() {
    var th = this;
    var timer_get = setInterval(function() {
      if (th.data.is_get_local_ok == 0) return false;    
      clearInterval(timer_get);
	  th.get_list();     
    }, 500)
  
  },
  
  get_list:function(){
	  if(this.data.is_no_more==1) return false;
	  if(this.data.islading==1) return false;
	  this.data.islading=1;
	  var th=this,req = getApp().request;
	  var  dd = {
	      store_id: o.stoid,
	      isstop: 0,
	      pageSize: 10,
		  page:th.data.page
	  }  	   
	  if(th.data.key_word!="" && th.data.key_word!=undefined) {
		  dd.keyword=th.data.key_word;
	  }
	  if(th.data.cat_id){
		  dd.category_id=th.data.cat_id;
	  }
	  if(th.data.lat){
		  dd.lat=th.data.lat;
		  dd.lon=th.data.lon;
	  }
	  
	   
	  wx.showLoading();
	  //----------获取门店---------
	  req.promiseGet("/api/weshop/pickup/list", {
	    data: dd,
	  }).then(res => {
		  this.data.islading=0;
		  wx.hideLoading();
		  if(res.data.code==0){			  
			  if(res.data.data.pageData.length<=0){
				  th.data.is_no_more=1; return false;
			  }  
			  
			  th.data.page=dd.page+1
			  var pick_list=th.data.pick_list;
			  if(!pick_list) pick_list=[];
			  pick_list=pick_list.concat(res.data.data.pageData);		  
			  th.setData({
				  pick_list:pick_list
			  })
		  }
	  })
  },
  
    //---加载更多是靠这个函数----
    onReachBottom: function() {
		this.get_list();
	},
	
	lose_focus:function(e){
		 var key_word = e.detail.value;
		 this.data.key_word=key_word;
	},
	
	
	phone:function(e){
		 var index=e.currentTarget.dataset.index;
		 var item=this.data.pick_list[index];		
		 wx.makePhoneCall({		
		      phoneNumber: item.pickup_phone,
         })
	},
	
	  //分类的下拉
	 bindPickerChange: function (e) {
	    var index = e.detail.value;//这里会获取他的索引值
	    var self = this;
	    var list = self.data.cat_list;
	    var item = list[index];		
		this.data.cat_id=item['cat_id'];
	    self.setData({
	      periodsname: item['cat_name'],
	      cid: item['cat_id']
	    })
		this.data.page=1;
	    this.data.is_no_more=0;
		this.setData({ pick_list:null})
		this.get_list();
		
	  },
	  
	  //搜索关键字
	  sear:function(){
			this.data.page=1;
			this.data.is_no_more=0;
		  this.setData({ pick_list:null})
		  this.get_list();
	  },
	  
	
	map: function (e) {
	   var index = e.currentTarget.dataset.index;//这里会获取他的索引值
	   var self = this;
	   var list = self.data.pick_list;
	   var item = list[index];		
	 	   
	   wx.openLocation({
	             //当前经纬度
	             latitude:  parseFloat(item.lat),
	             longitude: parseFloat(item.lon),
	             //缩放级别默认28
	             scale: 28,
	             //位置名
	             name:item.pickup_name,
	             //详细地址
	             address: item.pickup_address,
	             //成功打印信息
	             success: function(res) {},
	             //失败打印信息
	             fail: function(err) {},
	             //完成打印信息
	             complete: function(info){},
	           })	   	  
	 },
  
  

  
})