<?php
if(!defined('MOD'))  die("access die");
if($admin_info['groupid'] > 2 ) qbase::purviewCheckDie();
$action = $_GET['action'];
qbase::checkAction( $action);

switch($action){
	case 'manage':
	case 'search':
		$limit = 15;
		$pageno = intval($_GET['pageno']) > 0 ? intval($_GET['pageno']) : 1;
		$offset = ($pageno - 1) * $limit;
		$where = " where 1=1 ";

	break;

	case 'log':
		$limit = 15;
		$pageno = intval($_GET['pageno']) > 0 ? intval($_GET['pageno']) : 1;
		$offset = ($pageno - 1) * $limit;
		$where = " where 1=1 ";	
		if(trim($_GET['username'])){
			$param['username'] = $_GET['username'] = trim(urldecode($_GET['username']));
			$uid = qdb::queryAlone("select uid from ".TABLEPRE."user where username = '{$_GET['username']}' limit 1");
			if($uid > 0){
				$where .= " and p.uid = '{$uid}' ";
			}
		}
		if(intval($_GET['channelid']) > 0 && intval($_GET['id']) > 0){
			$where .= " and p.channelid = '".intval($_GET['channelid'])."' and p.id = '".intval($_GET['id'])."' ";
			$param['channelid'] = intval($_GET['channelid']);
			$param['id'] = intval($_GET['id']);
		}
		if($dateline_start = trim(urldecode($_GET['dateline_start']))){
			$param['dateline_start'] = $dateline_start;
			$start_dateline = strtotime($dateline_start);
			$where .= " and p.pay_dateline >= '{$start_dateline}' ";
		}
		if($dateline_end = trim(urldecode($_GET['dateline_end']))){
			$param['dateline_end'] = $dateline_end;
			$end_dateline = strtotime($dateline_end) + 86400;
			$where .= " and p.pay_dateline <= '{$end_dateline}' ";
		}
		$total = qdb::queryAlone("select count(*) as num from ".TABLEPRE."pay as p {$where}");
		$data = qdb::fetchAll("select p.*, u.username from ".TABLEPRE."pay as p left join ".TABLEPRE."user as u on p.uid = u.uid {$where} order by pid desc limit {$offset}, {$limit}");
		$ids_array = array();
		$ids = array();
		$values = array();
		$temp = array();
		$temp_map = array();
		foreach($data as $val){
			$ids_array[$val['channelid']][] = $val['id'];
			$temp_map[$val['channelid'] .'-'. $val['id']] = &$val;
		}
		foreach($ids_array as $key=>$v){
			$ids[$key] = implode(',', $v); 
		}
		foreach($ids as $key=>$v){
			if(empty($v)) continue;
			$table = $_MODULE[$channel[$key]['channelselect']]['table_name'];
			if(empty($table)) continue;
			$values[$key] = qdb::fetchAll("select * from ".TABLEPRE."{$table}_{$key} where id IN ({$v})");
		}
		$log_ids = array();
		foreach($values as $vv){
			foreach($vv as $v){
				if(false !== ($doc = cmsFormatItem($v))){
					$temp[$doc['channelid'] .'-'. $doc['id']] = $doc;
				}
			}
		}
		foreach($data as &$data_doc){
			$key = $data_doc['channelid'] .'-'. $data_doc['id'];
			if($temp[$key]){
				$data_doc = array_merge($data_doc, $temp[$key]);
			}
		}
		unset($data_doc);
	break;
}

include qbase::template($path . 'pay');
?>
