<?php
/**
 * @link         http://www.openzc.cn/
 * @author       hexipeng | e-mail:2017412656@qq.com
 * @copyright    Copyright (c) 2020, Openzc, Inc.
 * @license      http://www.openzc.cn/license.txt
 */
class filterModel{
	function Run(&$atts, &$refObj, &$fields){
		global $category_class,$product_class,$filter_class,$init;
		$attlist = "item=,space=0,section=,options_id=,no_options_id=,type=,flag=,no_flag=,mul=,values=,slide=false,min=,max=,field=";
		FillAtts($atts,$attlist);
		FillFields($atts,$fields,$refObj);
		extract($atts, EXTR_OVERWRITE);
		$GET=$_GET;
		unset($GET['action'],$GET['zenid'],$GET['products_id'],$GET['number_of_uploads']);
		if($GET['main_page']==FILENAME_PRODUCT_INFO){$GET['main_page']="index";}
	    if($item){
			$GLOBALS['filter_item']=$item;
		}else{
			$item=$GLOBALS['filter_item'];
		}

		if($item=="brand"){

			$filter[0]['filter_item']="brand";
			if(array_key_exists("filter_brand",$GLOBALS)){
				$filter_brand_values=$GLOBALS['filter_brand'];
			}else{
				$filter_brand_values=$filter_class->get_brand_bar($GET,$mul);
				$GLOBALS['filter_brand']=$filter_brand_values;
			}
			
			if($filter_brand_values==false){
				$filter=array();
			}
			
			if($type=="son"){
				$filter=$filter_brand_values;
				foreach($filter as $k => $v){
					$filter[$k]['form_value']=$v['filter_id'];
				}
			}
		}
		
		if($item=="slideprice"){
			if($GLOBALS['listCount']==0){return false;}
			global $currencies;
			$filter=$filter_class->get_price_bar($GET,false,false,true);
			$filter[0]['filter_item']="slide price";
			$filter[0]['range']=$filter[0]['min'].",".$filter[0]['max'];
			$filter[0]['min_name']=$currencies->format($filter[0]['min']);
			$filter[0]['max_name']=$currencies->format($filter[0]['max']);
			if($field){return $filter[0][$field];}
			
			return $filter;
		}
		if($item=="price"){
			if($GLOBALS['listCount']==0){return false;}
			switch($slide){
				case "true":
					$filter=$filter_class->get_price_bar($GET,false,false,true);
					$filter[0]['filter_item']="slide price";
					$filter[0]['range']=$filter[0]['min'].",".$filter[0]['max'];
					if($field){return $filter[0][$field];}
					
				break;
				case "false":
					if($section){
						$range=$filter_class->get_price_bar($GET,"getrange",false,true);
						$space=($range['max']-$range['min'])/($section-1);
						$space=ceil($space);
						//echo $space;exit;
					}
					if($space){
						$GLOBALS['filter_price_space']=$space;
					}else{
						$GLOBALS['filter_price_space']=null?'0':'0';
						$space=$GLOBALS['filter_price_space'];
					}
					$filter[0]['filter_item']="price";

					$filter[0]['filter_price_values']=$filter_class->get_price_bar($GET,$space,false);
					if(count($filter[0]['filter_price_values'])==0){return false;}
					if($type=="son"){
						$filter=$fields['filter_price_values'];
						foreach($filter as $k => $v){
							$filter[$k]['form_value']=$v['min'].",".$v['max'];
						}
					}
					
				break;
			}
			
		}
		if($item=="attr"){
			if(!$options_id && !$flag){echo "“Error,Filter 属性参数不完整！”";return false;}
		
			$GLOBALS['attr_mul']=$mul;
			$filter=$filter_class->get_attr_bar($GET,$options_id,$mul,$no_options_id,$flag,$no_flag);
			
			if(count($filter)==0){return false;}
			foreach($filter as $a => $b){
				if(count($b['values'])==0){
					unset($filter[$a]);
				}
			}
			if($type=="son"){
				
				$mul=$GLOBALS['attr_mul'];
				$options_id=$fields["options_id"];
				$filter=$filter[$options_id]['values'];
				if($values){
					$values=array_filter(explode(",",$values));
					foreach($values as $k => $v){
						$v=explode(":",$v);
						$filter[$v[0]]['filter_value']=$v[1];
					}
				}
				
				foreach($filter as $k => $v){
					$filter[$k]['form_value']=$v['options_values_id'];
				}
				
			}
			
		}
		if($item=="active"){
			if(array_key_exists("filter_active",$GLOBALS)){
				$filter=$GLOBALS['filter_active'];
				
			}else{
				$GLOBALS['filter_active']=$filter=$filter_class->get_active_bar($GET);
			}
			
			if($type=="son"){
				$item_id=$fields["item_id"];
				$filter=$filter[$item_id]['values'];
			}
			
		}
		if($item=="clear"){
			$filter=$filter_class->get_filter_clear();
		}
		
		return $filter;
	}
	
}

