<?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_coupon($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 = "";
		}

		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";	
		}

		$sql = "select count(id) from qk_coupon where 1=1 $wheres";
		$allCount = $GLOBALS['db']->getOne($sql);
		if($params['pager']=="true"){
			$smarty->assign("allCount",$allCount);
		}
		$sql = "select id from qk_coupon where 1=1 $wheres order by id desc $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;
}

?>