const app = getApp()
const innerAudioContext = wx.createInnerAudioContext();
var t = getApp(),o = t.globalData.setting;
Component({
  properties: {
    object: { type: Object,value: null,},
  },
  options: { addGlobalClass: true },
  data: {
	   top_store:null,  //会员默认的门店
	   url: o.imghost,
	   store_config:null,
	   lat: null, //维度
	   lon: null, //经度	   
	   sort_store:0,
	   choice_sort_store:0,
	   
	   fir_pick_index:0, //5个门店显示选择的下标
  },
  ready: function() {
	  var th=this;
      getApp().getConfig(function(e){
      	 th.setData({store_config:e,});
		 th.init();
      })
  },
  pageLifetimes:{
    //要处理一下，游客登录后的界面的变化,主要还该是改变会员
    show: function () {
      this.init();
	  this.setData({fir_pick_index:0})
    }
  },
  
  methods: {
	 //-- 初始化顶部的门店显示 -- 
     init(){
		 var th=this;
		 
		 var user_id=getApp().globalData.user_id;
		 if(!user_id){
		 	return false;
		 } 
		 
		 //获取顶部的门店
		 getApp().get_user_store(function(e){
				   wx.getLocation({
						type: 'gcj02',
						success: function(res) {
							th.data.lat = res.latitude;
							th.data.lon = res.longitude;
							th.set_fir_store_to_def();  
						},
						fail: function(res) {
							if(res.errCode == 2) {		 
								getApp().confirmBox("请开启GPS定位", null, 25000, !1);		  
							}
							th.set_fir_store_to_def();
						}
					})	

		 })


         getApp().getConfig2(function (e) {
             var json_d = JSON.parse(e.switch_list);
             if(json_d.is_pricing_open_store){
                th.setData({ no_select:1 })
			 }
		 },1)
	 },



	 //-- 设置最近的店为默认的店,一开始加载的时候 --
	 set_fir_store_to_def(){
	    var th=this;
	    var dd = {
	      store_id: o.stoid,
				isstop: 0,
				is_pos: 1,
	      pageSize: 1,
	 		  page:1,
	    };
	 		dd.lat = th.data.lat;
	 		dd.lon = th.data.lon;

            var top_store=getApp().globalData.pk_store;
	 		if(top_store){
	 			dd.pickup_id=top_store.pickup_id;
			}

	 		//----------获取门店,最近的门店----------------
	 		getApp().request.promiseGet("/api/weshop/pickup/list", {
	 		  data: dd,
	 		}).then(res => {
	 		    if(res.data.code==0 && res.data.data && res.data.data.pageData && res.data.data.pageData.length>0){
	 				var top_store=res.data.data.pageData[0];
	 				th.setData({top_store:top_store})
	 			}
	 		})
	 },
	
	 //点击选择门店
	 sele_top_store(){
		var user_id=getApp().globalData.user_id;
		if(!user_id){
			wx.navigateTo({
			  url: '/packageE/pages/togoin/togoin',
			});
			return false;
		} 
		var th=this;

		if(this.data.no_select && getApp().globalData.pk_store ){
			return false;
		}

		//获取属性
        getApp().getConfig2(function (e) {
            var json_d = JSON.parse(e.switch_list);
            if(getApp().globalData.pk_store && json_d.is_pricing_open_store){
               return false;
			}
            th.get_top_sto(function(){
                th.setData({store:1});
            });
        }) 


	 },
	 //选择更多门店
	 more_store: function() {
	   this.setData({
	     sort_store: 1
	   });
	 },
	 		
	 //确定def_pick为选择的门店
	 sure_pick: function() {
	   var th = this,item = null,user_id=getApp().globalData.user_id;
	   if(!user_id) return false;
	   
	   //如果是第一层的时候
	   if (th.data.choice_sort_store == 0) {
		 var index = th.data.fir_pick_index;
		 if (th.data.is_show_sto_cat == 1) {
		   item = th.data.def_pickpu_list[index];
		 } else {
		   item = th.data.only_pk?th.data.only_pk[index]:null; //当没有门店分类的时候
		 }
	   } 
	    //如果是跳转到门店分类里面的时候
	   else {
		 var index = th.data.sec_pick_index;
		 item = th.data.sec_sto.s_arr[index];
	   }

	   if(!item){
          return false;
	   }

	     //--回调函数的用法--
		 th.setData({
		   top_store: item,
		   sto_sele_name: item.pickup_name,
		   sto_sele_id: item.pickup_id,
		   sto_sele_distr: item.distr_type,
		   store: 0,
		   choice_sort_store: 0,
		   fir_pick_index: 0
		 });
		 //全局默认门店
		 getApp().globalData.pk_store=item; 
	     //还原基础属性 
	     th.setData({store: 0,choice_sort_store: 0,sort_store: 0}) 
		 //更新会员的默认的门店
		 getApp().request.put('/api/weshop/users/update', {
		   data: {
		     user_id:user_id,
		     def_pickup_id:item.pickup_id
		   },
		   success: function(res) {}
		 });
			      
	 },
	 
	 //---点击二级之后的选择---
	 choose_for_store: function(e) {
	   var index_c = e.currentTarget.dataset.ind;
	   this.setData({
	     sec_pick_index: index_c,
	     fir_pick_index: index_c
	   })	 
	 },
	 
	 //关闭弹出框
	 close_popup:function(){
		this.setData({store: 0, choice_sort_store: 0,sort_store: 0})
	 },
	 	
	 //---------拿出门店分类和门店------------
	 get_top_sto(func) {
		 //如果有就不重新调用接口了
		 if(this.data.all_pick_list){	
			this.deal_pickup(this.data.all_pick_list,func)			
			return false; 
		 }		 
	     var th = this,that=this;
		 var dd = {
			 store_id: o.stoid,
			 isstop: 0,
			 is_pos: 1,
			 pageSize: 2000
		   }
	     
	     //如果有距离的话
	     if (th.data.lat != null) {
	       dd.lat = th.data.lat;
	       dd.lon = th.data.lon;
	     }
	   	 	 	  
	 	 wx.showLoading({title:'加载中.' });
	     //----------获取门店----------------
	     getApp().request.promiseGet("/api/weshop/pickup/list", {
	       data: dd,
	     }).then(res => {
	       var e = res;	 
	       if (e.data.code == 0 && e.data.data.pageData && e.data.data.pageData.length){
	         th.setData({all_pick_list:e.data.data.pageData});        
	         th.deal_pickup(e.data.data.pageData,func);  //--普通门店排版--
	       }
	    })	  	 
	 },
	 
	 //------------处理门店---------------
	 deal_pickup(e,func){
	    var th=this;
	 	wx.hideLoading(); 	 		
		//-- 如果有默认选择门店的时候，要把默认门店放在第一位 --
		if (th.data.top_store){
		  for (var k = 0; k < e.length; k++) {
		    if (e[k].pickup_id == th.data.top_store.pickup_id) {
		      e.splice(k, 1); //删除
		      break;
		    }
		  }
		  e.splice(0, 0, th.data.top_store); //添加
		}

		var ishas_cate=0;
		for(let i in e){
			let item=e[i];
			if(item.category_id>0) {
                ishas_cate=1;break;
			}
		}


	   //单总量超出5个的时候
	   if (e.length > 10 && ishas_cate) {
	     getApp().request.get("/api/weshop/storagecategory/page", {
	       data: {
	         store_id: o.stoid,
	         is_show: 1,
	         pageSize: 1000,
	 		  orderField:"sort",
	 		  orderType:'asc',
	       },
	       success: function(ee) {
	         if (ee.data.code == 0) {		
	           if (ee.data.data && ee.data.data.pageData &&  ee.data.data.pageData.length > 0) {
	             var def_arr = new Array();
	             var ishas = 0;
	             //-- 开始就看5个门店 --
	             for (var k = 0; k < 5; k++) {
	               if (k == e.length) break;
	               def_arr.push(e[k]);
	             }
	 
	             th.setData({
	               def_pickpu_list: def_arr,
	               pickpu_list: ee.data.data.pageData
	             });
	             var sto_cate = ee.data.data.pageData;
	             var sto_arr = e;
	             var newarr = new Array();
	             var qita = new Array();
	 
	             //----要进行门店分组--------
	             for (var i = 0; i < sto_arr.length; i++) {
	               //找一下这个门店有没有在分类数组内
	               var find2 = 0, find2name = "",sort=0;
	               for (var m = 0; m < sto_cate.length; m++) {
	                 if (sto_arr[i].category_id == sto_cate[m].cat_id) {
	                   find2 = sto_cate[m].cat_id;
	                   find2name = sto_cate[m].cat_name;
	                   sort = sto_cate[m].sort;
	                   break;
	                 }
	               }
	 
	               if (newarr.length > 0) {
	                 var find = 0;
	                 //如果有找到，那门店就在这个分组内，否则，分类就要排在其他
	                 if (find2 != 0) {
	                   for (var ii = 0; ii < newarr.length; ii++) {
	                     if (sto_arr[i].category_id == newarr[ii].cat_id) {
	                       newarr[ii].s_arr.push(sto_arr[i]);
	                       find = 1;
	                       break;
	                     }
	                   }
	                   if (find == 0) {
	                     var arr0 = new Array();
	                     arr0.push(sto_arr[i]);
	                     var item = {
	                       cat_id: find2,
	                       name: find2name,
	 						sort:sort,
	                       s_arr: arr0
	                     };
	                     newarr.push(item);
	                   }
	                 } else {
	                   qita.push(sto_arr[i]);
	                 }
	               } else {
	                 //如果有找到，那门店就在这个分组内，否则，分类就要排在其他
	                 if (find2 != 0) {
	                   var arr0 = new Array();
	                   arr0.push(sto_arr[i]);
	                   var item = {
	                     cat_id: find2,
	                     name: find2name,
	 					 sort:sort,
	                     s_arr: arr0
	                   };
	                   newarr.push(item);
	                 } else {
	                   qita.push(sto_arr[i]);
	                 }
	               }
	             }
	 			  
	 			  //门店分类要排序下
	 			  function compare(property){
	 			      return function(a,b){
	 			          var value1 = a[property];
	 			          var value2 = b[property];
	 			          return value1 - value2;
	 			      }
	 			  }
	 			  if(newarr.length>0) newarr.sort(compare("sort"));
	 			 	 
	             //----安排其他的分类-----
	             if (qita.length > 0) {
	               var item = {
	                 cat_id: -1,
	                 name: "其他",
	                 s_arr: qita
	               };
	               newarr.push(item);
	             }
	 			  			  
	             th.setData({
	               is_show_sto_cat: 1,
	               all_sto: newarr
	             });
	 
	           } else {
	             th.setData({is_show_sto_cat: -1,only_pk: e});	             
	           }
	         } else {
	           th.setData({is_show_sto_cat: -1,only_pk: e});
	         }
			 func();  //回调
		   }
	     });
	   } else {	
	     th.setData({is_show_sto_cat: 0,only_pk: e});    
		 func(); //回调
	   }   
	 },
	 
	 //---选择分类门店---
	 choice_sort_store: function(e) {
	   var index = e.currentTarget.dataset.index;
	   var region_name = e.currentTarget.dataset.region;
	   var item = this.data.all_sto[index];
	   this.setData({
	     region_name: region_name,
	     sort_store: 0,
	     choice_sort_store: 1,
	     sec_sto: item,
	     sec_pick_index: 0
	   });
	 },
	 //返回按钮
	 returns: function() {
	   this.setData({
	     sort_store: 0,
	     choice_sort_store: 0
	   });
	 },
	 
	 //第一层选择门店的情况
	 choose_for_store_fir: function(e) {
	   var index_c = e.currentTarget.dataset.ind;
	   var th=this;
	   th.setData({ fir_pick_index: index_c }) 
	 },
  },
  
 
  
})