const ut = require("../../utils/util.js");
var regeneratorRuntime = require('../../utils/runtime.js');
var oo = getApp().globalData.setting;
module.exports = {
	//下拉排序
	bind_pk_sort:function (e,th){
		var index=e.detail.value;
		var ob=th.data.more_sort_arr[index];

		var url = th.data.requestUrl;
		if (th.data.tabname!=ob.value){

			var txt='desc';
            if(ob.value=='sort') txt='asc'
			th.setData({ tabname: ob.value, adname:txt });
		}
		th.resetData();

		//开始点击分类
		if(th.requestGoodsList) {
			//-- 开始点击分类 --
			th.requestGoodsList(url);
		}else if(th.requestSearch){
			//-- 重新开始搜索 --
			th.requestSearch(url);
		}

	},

	//-- 获取列表 --
	set_screen:async function (url,th){
		if(!url) url=th.data.requestUrl;

		var arr=url.split('1=1&');
		url='/api/weshop/goods/goodsBrandGroup?'+arr[1];

		var screen_brand=null;
		var screen_nation=null;
		var screen_price=null;
		var screen_cate=null;
		//如果有分类的话
		if( th.data.cat_id && !th.data.screen_cate){
			var c_url="/api/weshop/goodscategory/get/"+oo.stoid+"/"+ th.data.cat_id;
			var f_cate=null;
			await getApp().request.promiseGet(c_url,{
			}).then(res=>{
				if(res.data.code==0 && res.data.data){
					f_cate=res.data.data;
				}
			})

			//分类必须有parent_id_path值
			if(f_cate && f_cate['parent_id_path']){
				var new_arr=f_cate['parent_id_path'].split('_');
				c_url='/api/weshop/goodscategory/page?store_id='+oo.stoid+'&parent_id_path='+(new_arr[0]+"_"+new_arr['1']);
				await getApp().request.promiseGet(c_url,{
					data:{level:2,pageSize:1000}
				}).then(res=>{
					if(ut.ajax_ok(res) ){
						screen_cate=res.data.data.pageData.filter(function(e){
							return e.name
						});
					}
				})
			}

		}


		if(url.indexOf('brand_id')==-1) {
			var url1 = url + "&grouptype=1";
			await getApp().request.promiseGet(url1, {
				data: {is_mainshow: 1, isonsale: 1, store_id: oo.stoid}
			}).then(res => {
				if (res.data.code == 0 && res.data.data && res.data.data.length) {
					screen_brand = res.data.data.filter(function(e){
						return e.name
					});
				}
			})
		}

		if(url.indexOf('nation_id')==-1) {
			url1 = url + "&grouptype=2";
			await getApp().request.promiseGet(url1, {
				data: {is_mainshow: 1, isonsale: 1, store_id: oo.stoid}
			}).then(res => {
				if (res.data.code == 0 && res.data.data && res.data.data.length) {
					screen_nation = res.data.data.filter(function(e){
						return e.name;
					});
				}
			})
		}


		if(url.indexOf('startprice')==-1) {
			url1 = url + "&grouptype=3";
			await getApp().request.promiseGet(url1, {
				data: {is_mainshow: 1, isonsale: 1, store_id: oo.stoid}
			}).then(res => {
				if (res.data.code == 0 && res.data.data && res.data.data.length) {
					var price = res.data.data;
					var max_price = price[0].final_price;
					var psize = Math.ceil(max_price / 5); // 每一段累积的价钱
					var parr = [];
					for (var i = 0; i < 5; i++) {
						var start = i * psize;
						var end = start + psize;
						// 如果没有这个价格范围的商品则不列出来
						var ii = false;
						for (var v in price) {
							var item_p = price[v];
							if (item_p.final_price > start && item_p.final_price <= end) {
								ii = true;
							}
						}
						if (ii == false) continue;
						var obj = {};
						obj['price'] = start + "-" + end;
						if (i == 0) obj['name'] = end + "元以下";
						else if (i == 4) obj['name'] = start + "元以上";
						else obj['name'] = start + "-" + end + '元';
						parr.push(obj)
					}
					screen_price = parr;
				}
			})
		}

		th.setData({screen_brand,screen_nation,screen_price,screen_cate})

	},


	filterGoods:function (e,th){

		var index=e.currentTarget.dataset.index;
		var type=e.currentTarget.dataset.type;
		var url=th.data.requestUrl;

		switch (type){
			case "1"://分类
				var item=th.data.screen_cate[index];
				if( th.data.cat_id){
					var arr_m=url.split("1=1&");
					var arr_m2=arr_m[1].split('&');
					var url="";
					for(var jj in arr_m2){
						if(arr_m2[jj].indexOf('cat_id')>=-1){
							url+="&cat_id="+item.id;
						}else if(arr_m2[jj].indexOf('pid')>=-1){
							url+="&pid="+item.parent_id;
						}else{
							url+="&"+arr_m2[jj];
						}
					}
				}else {
					url+="&cat_id="+item.id+"&pid="+item.parent_id;
				}
				url=arr_m[0]+"1=1&"+url;
				break;
			case "2"://国别
				var item=th.data.screen_nation[index];
				url+="&nation_id="+item.nation_id;
				break;
			case "3"://品牌
				var item=th.data.screen_brand[index];
				url+="&brand_id="+item.brand_id;
				break;
			case "4"://价格
				var item=th.data.screen_price[index];
				var p_arr=item.price.split('-');
				url+="&startprice="+p_arr[0]+"&endprice="+p_arr[1];
				break;
		}

		th.setData({screen_brand:null,screen_nation:null,openFilterModal:0,is_go:0,
			screen_price:null,screen_cate:null,requestData:null})
		th.data.currentPage=1;
		//进行筛选
		this.set_screen(url,th);

		th.setData({
			tabname:"sort",   //排序的字段
			adname:"asc",        //升降的字段
		})

		//开始点击分类
		if(th.requestGoodsList) {
			//-- 开始点击分类 --
			th.requestGoodsList(url);
		}else if(th.requestSearch){
			//-- 重新开始搜索 --
			th.requestSearch(url);
		}
	},

	//-- 展开和收起的控制 --
	open_more:function(e,th){
		var type=e.currentTarget.dataset.type;
		switch(type){
			case "1":
				if(th.data.fil_cate_state==0 || th.data.fil_cate_state==2) th.setData({fil_cate_state:1})
				else th.setData({fil_cate_state:2})
				break;
			case "2":
				if(th.data.fil_nation_state==0 || th.data.fil_nation_state==2) th.setData({fil_nation_state:1})
				else th.setData({fil_nation_state:2})
				break;
			case "3":
				if(th.data.fil_brand_state==0 || th.data.fil_brand_state==2) th.setData({fil_brand_state:1})
				else th.setData({fil_brand_state:2})
				break;
			case "4":
				if(th.data.fil_price_state==0 || th.data.fil_price_state==2) th.setData({fil_price_state:1})
				else th.setData({fil_price_state:2})
				break;

		}
	},

	clear_fil(th){
		var url=th.data.old_req_url;
		th.setData({screen_brand:null,screen_nation:null,openFilterModal:0,is_go:0,
			screen_price:null,screen_cate:null,requestData:null})
		th.data.currentPage=1;

		//进行筛选
		this.set_screen(url,th);
		if(th.requestGoodsList) {
			//-- 开始点击分类 --
			th.requestGoodsList(url);
		}else if(th.requestSearch){
			//-- 重新开始搜索 --
			th.requestSearch(url);
		}
	}


}