<?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_onlinepay($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']!="" && settype($params['limit'],"integer")){
			$limit = $params['limit'];
		}else{
			$limit = 10;
		}
		$pageSize = $limit;
		$smarty->assign("limit",$limit);
		$limit = "limit $limit";
		$page = getgpc("page");
		if(!$page || !settype($page,"integer")){$page=1;}
		$offset = ($page-1)*$pageSize;
		$limit = "limit $offset,$pageSize";
		if($page && settype($page,"integer")){
			$offset = ($page-1)*$pageSize;
			$limit = "limit $offset,$pageSize";
		}
		$keyTypeId = getgpc("keyTypeId");
		$key = getgpc("key");
		if($key){
				$wheres .= " and (remark like '%$key%'  or outTradeNo = '$key'  or transactionId = '$key' )";
		}
		if(isset($params['userId']) && $params['userId']!="" && settype($params['userId'],"integer")){
			$userId = $params['userId'];
		}else{
			$userId = false;
		}
		
		if($userId){
			$wheres .= "and userId='$userId'";
		}

		$allCount = $GLOBALS['db']->getOne("select count(id) from qk_onlinepay where 1=1 $wheres");
		$smarty->assign("allCount",$allCount);
		$res = $GLOBALS['db']->getAll("select * from qk_onlinepay qk_onlinepay where 1=1 $wheres order by id desc $limit");
		$i = 0;
		foreach($res as $k=>$v){
			$res[$i]['paymentInfo'] = $GLOBALS['db']->getRow("select name,typeId from `qk_payment` where id='".$v['paymentId']."'");
			$res[$i]['loginName'] = $GLOBALS['db']->getOne("select loginName from `qk_user` where id='".$v['userId']."'");
			$res[$i]['status'] = $GLOBALS['db']->getOne("select name from `qk_onlinepay_status` where id='".$v['iStatus']."'");
			$res[$i]['query'] = false;
			$typeId = $res[$i]['paymentInfo']['typeId'];$className='';
			if($typeId){
				$className = $GLOBALS['db']->getOne("select className from `qk_paytype` where id='".$typeId."'");
				$classFile = ROOT_PATH."plugins/pay/".$className.".class.php";
				if($className && file_exists($classFile)){
					require_once($classFile);$pay = new $className(); if($pay->action && $pay->action['query']===true){ $res[$i]['query'] = true; }
				}
			}
			$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;
}

?>