<?php
 /**
 *      [XCECP] (C) COLNET Inc.
 *      This is NOT a freeware, use is subject to license terms
 */
if(!defined("IN_QKWEB")){exit("Access Denied");}
function smarty_block_coupontype($params, $content, $smarty, &$repeat)
{ 
	
  	if(!isset($params['name']) || $params['name']==""){
		$params['name'] = "item";
	}
	$dataindex = md5(__FUNCTION__ . md5(serialize($params)));
	if(!isset($smarty->block_data)){$smarty->block_data=array();}
	if(!isset($smarty->block_data[$dataindex])){
		$page = getgpc("page");
		$wheres = "";
		if(isset($params['limit']) && $params['limit']!=""){		
			if($params['limit']!=="false"){			
				$limit = $params['limit'];
				if(settype($limit,"integer")){
					$limit = $limit;
				}else{
					$limit = 10;
				}
			}else{
				$limit =false;
			}
		}else{
			$limit = 10;
		}
		$pageSize = $limit;
		$smarty->assign("limit",$limit);
		$limit = "limit $limit";
		$page = getgpc("page");
		if(!$page || !settype($page,"integer") || $params['pager']!="true"){$page=1;}
		if($page && settype($page,"integer")){
			$offset = ($page-1)*$pageSize;
			$limit = "limit $offset,$pageSize";
		}
		$offset = ($page-1)*$pageSize;
		$limit = "limit $offset,$pageSize";
		if($params['limit']=="false"){
			$limit = "";
		}

		$ids = "";
		if(isset($params['catId']) && $params['catId']!="" ){
			$catId = $params['catId'];
			$catIdArr = explode(",",$catId);
			//print_r($catIdArr);
			$i=0;
			$j=0;
			foreach($catIdArr as $item){
				$i++;
				$j++;
				if(settype($item,"integer")){
					$ids .= getCats($item);
				}
				if($j<count($catIdArr)){
						$ids .= ",";
				}
			}
			unset($i);
		}
		//echo $ids;
		if($ids){
			$wheres .= " and catId in (".$ids.")";
		}
		if(isset($params['brandId']) && $params['brandId']!=="" && settype($params['brandId'],"integer")){
			$brandId = $params['brandId'];
		}else{
			$brandId = false;
		}

		
		if($brandId){
			$wheres .= " and brandId in (".$brandId.")";
		}else if($brandId===0){
			$wheres .= " and brandId !=0";
		}
		if(!isset($param['iStatus'])){
			$iStatus=getgpc("iStatus");
			if($iStatus=='1' || $iStatus=='0'){
				$params['iStatus'] = $iStatus;
			}
		}
		if(isset($params['iStatus'])){
			$iStatus = $params['iStatus'];
			if($iStatus==''){}else if(settype($iStatus,"integer")){$wheres.=" and iStatus='$iStatus'";}
		}else{
			$wheres .= " and iStatus=1";	
		}

		$key = getgpc("key");
		if(isset($params['key']) && $params['key']!=""){
			$key = $params['key'];
		}
		if($key){
			$sqlSku = "select goodsId from qk_goods_attr_val where attrId=2 and value like '%$key%'";
			$sku = $GLOBALS['db']->getAll($sqlSku);
			$i=0;
			$skuId="";
			foreach($sku as $item){
				$skuId .= $item['goodsId'];
				$i++;
				if($i<count($sku)){
					$skuId .= ",";
				}
			}
			if($skuId){$skuId = "or id in($skuId)";}
			$wheres .= " and (name like '%$key%' or content like '%$key%' $skuId)";
		}

		if(isset($params['promotion']) && $params['promotion']!="" && settype($params['promotion'],"integer")){
			$promotion = $params['promotion'];
		}else{
			$promotion = false;
		}
		if($promotion){
			$wheres .= " and promotion ='".$promotion."'";
		}

		if(isset($params['isHot']) && $params['isHot']!="" && settype($params['isHot'],"integer")){
			$isHot = $params['isHot'];
		}else{
			$isHot = false;
		}
		if($isHot){
			$wheres .= " and isHot ='".$isHot."'";
		}

		if(isset($params['buyWay']) && $params['buyWay']!="" && settype($params['buyWay'],"integer")){
			$buyWay = $params['buyWay'];
			if($buyWay==2){
				$wheres.=" and buyWay=2 and scorePrice>0";
			}
		}

		if(isset($params['isNew']) && $params['isNew']!="" && settype($params['isNew'],"integer")){
			$isNew = $params['isNew'];
		}else{
			$isNew = false;
		}
		if($isNew){
			$wheres .= " and isNew ='".$isNew."'";
		}

		if(isset($params['isLively']) && $params['isLively']!="" && settype($params['isLively'],"integer")){
			$isLively = $params['isLively'];
		}else{
			$isLively = false;
		}
		if($isLively){
			$wheres .= " and isLively ='".$isLively."'";
		}

		if(isset($params['isTuan']) && $params['isTuan']!="" && settype($params['isTuan'],"integer") && $params['isTuan']==1){
			$wheres .= " and isTuan =1";
		}else{
			//$wheres .= " and isTuan =0";
		}

		if(isset($params['temaiId']) && $params['temaiId']!="" && settype($params['temaiId'],"integer")){
			$sql = "select brandId from qk_temai where id='".$params['temaiId']."'";
			$brandId = $GLOBALS['db']->getOne($sql);
			if($brandId){
				$wheres.=" and brandId='$brandId' and promotion=1";
			}
		}
	
		if(isset($params['type']) && $params['type']!=""){
			$type = $params['type'];
		}else{
			$type = false;
		}
		if($type){
			$wheres = " and 1=0";
		}

		if(isset($params['thumb'])){
			if($params['thumb']=="true"){
				$wheres .= " and thumbId<>0";
			}else if($params['thumb']=="false"){
				$wheres .= " and thumbId=0";
			}
		}
		
		$filter = getgpc("filter");
		if($catId && $filter){
			load_class("category");
			$cm = new CategoryModel();
			$catInfo = $cm->getInfo($catId);
			$filterAttr = $catInfo['filter'];
			$filterValue = explode("-",$filter);
			if(count($filterAttr) == count($filterValue)){
				$i=0;
				$j=0;
				
				foreach($filterAttr as $attr){
					if(intval($filterValue[$i])>0){
						$goods[$i]=array();
						$sql = "select goodsId from qk_goods_attr_val where attrId='".$attr."' and extId='".$filterValue[$i]."'";
						//echo $sql;
						$res = $GLOBALS['db']->getAll($sql);
						//print_r($res);
						foreach($res as $item){
							$goods[$i][] = $item['goodsId'];
						}
						//print_r($goods[$i]);
						//if(count($goods[$i])>0){
							if($j==0){
								$goodsAll = $goods[$i];
							}else if($i>0){
								$goodsAll = array_intersect($goodsAll,$goods[$i]);
							}
							$j++;
							
						//}
					}
					$i++;
				}
				//$goodsAll = array_intersect($goods[0],$goods[1],$goods[2]);
				//print_r($goodsAll);
				//exit;
				unset($i);
			}
		}
		if(isset($goodsAll) && is_array($goodsAll)){
			if(count($goodsAll)>0){
				$goodsId = implode(",",$goodsAll);
				$wheres .= " and id in (".$goodsId.")";
			}else{
				$wheres .= " and id=null";
			}
		}

		$sqlOrderBy = "";
		if(isset($params['orderBy']) && $params['orderBy']!=""){
			$orderbys = $params['orderBy'];
			$orderbyArr = explode(",",$orderbys);
			$orderbys = "";
			foreach($orderbyArr as $val){
				$orderArr = explode(" ",$val);
				if($orderArr[0] && isset($orderArr[1]) && in_array($orderArr[1],array("asc","desc"))){
					if($orderArr[0]!=""){
						if($sqlOrderBy==""){
							$sqlOrderBy .= $orderArr[0]." ".$orderArr[1];
						}else{
							$sqlOrderBy .= ",".$orderArr[0]." ".$orderArr[1];
						}
					}
				}
			}
		}else{
			$sqlOrderBy = " id desc";
		}
		$sort= getgpc("sort");
		if(in_array($sort,array("price","sales","time"))){
			switch($sort){
				case "price":
					$sqlOrderBy = " price asc";break;
				case "sales":
					$sqlOrderBy = " sales desc";break;
				case "time":
					$sqlOrderBy = " posttime desc";break;
			}
		}
//SELECT qk_goods.id,(select count(*) from qk_order_goods where qk_goods.id=qk_order_goods.goodsId) sales FROM `qk_goods` left join qk_order_goods on qk_order_goods.goodsId=qk_goods.id

		//echo $wheres;exit;
		$sql = "select count(id) from qk_coupon_type where 1=1 $wheres";
		$allCount = $GLOBALS['db']->getOne($sql);
		if($params['pager']=="true"){
			$smarty->assign("allCount",$allCount);
		}
		$sql = "select * from qk_coupon_type where 1=1 $wheres order by $sqlOrderBy $limit";
		$res = $GLOBALS['db']->getAll($sql);
		$i = 0;
	//	load_class("coupon");
	//	$cm = new CouponModel();
		foreach($res as $k=>$v){
			//$res[$i]  = $cm->getInfo($v['id']);
			//$res[$i]['discount'] = sprintf("%.2f",$v['price']*10/$v['marketPrice']);
			
			$res[$i]['index']  = $i;
			$i++;
		}
		$smarty->block_data[$dataindex]=$res;
	}
	if(is_array($smarty->block_data[$dataindex]) && $smarty->block_data[$dataindex]){
		$item = array_shift($smarty->block_data[$dataindex]);
		$smarty->assign($params['name'], $item);
		if(count($smarty->block_data[$dataindex])==0){
			$smarty->block_data[$dataindex] = false;
		}
		$repeat= true;
	}else{
		unset($smarty->block_data[$dataindex]);
		$repeat = false;
	}
	echo $content;
}

?>