<?php
!function_exists('adminmsg') && exit('Forbidden');
$basename="$admin_file?adminjob=orderlist";

if (!$action) {

	InitGP(array('state','page'),'GP',2);
	if ($state == 1) {
		$sqladd = "WHERE state=0 OR state=1";
	} elseif ($state == 2) {
		$sqladd = "WHERE state=2";
	} else {
		$sqladd = '';
	}
	include_once(R_P.'require/forum.php');
	require_once(R_P.'require/credit.php');

	$page < 1 && $page = 1;
	$limit = pwLimit(($page-1)*$db_perpage,$db_perpage);
	$rt    = $db->get_one("SELECT COUNT(*) AS sum FROM pw_clientorder $sqladd");
	$pages = numofpage($rt['sum'],$page,ceil($rt['sum']/$db_perpage),"$basename&state=$state&");

	$orderdb = array();
	$query = $db->query("SELECT c.*,m.username FROM pw_clientorder c LEFT JOIN pw_members m USING(uid) $sqladd ORDER BY date DESC $limit");
	while ($rt = $db->fetch_array($query)) {
		$rt['date'] = get_date($rt['date']);
		$orderdb[]  = $rt;
	}
	include PrintEot('orderlist');exit;

} elseif ($action == 'pay') {

	InitGP(array('id','step','payemail'));
	$rt = $db->get_one("SELECT c.*,m.username FROM pw_clientorder c LEFT JOIN pw_members m USING(uid) WHERE id=".pwEscape($id)." AND state=0");
	!$rt && adminmsg('undefined_action');

	if (!$step) {

		$rt['date'] = get_date($rt['date']);
		include PrintEot('orderlist');exit;

	} else {

		if (!$payemail) {
			$basename = "javascript:history.go(-1);";
			adminmsg('no_payemail');
		}
		$rmbrate = $db_creditpay[$rt['paycredit']]['rmbrate'];
		!$rmbrate && $rmbrate = 10;
		$currency = $rt['number'] * $rmbrate;

		require_once(R_P.'require/credit.php');
		$credit->addLog('main_olpay',array($rt['paycredit'] => $currency),array(
			'uid'		=> $rt['uid'],
			'username'	=> $rt['username'],
			'ip'		=> $onlineip,
			'number'	=> $rt['number']
		));
		$credit->set($rt['uid'],$rt['paycredit'],$currency);

		$db->update("UPDATE pw_clientorder SET payemail=".pwEscape($payemail).',state=2 WHERE id='.pwEscape($id));

		require_once(R_P.'require/msg.php');
		$message = array(
			'toUser'	=> $rt['username'],
			'subject'	=> 'olpay_title',
			'content'	=> 'olpay_content_2',
			'other'		=> array(
				'currency'	=> $currency,
				'cname'		=> $credit->cType[$rt['paycredit']],
				'number'	=> $rt['number']
			)
		);
		pwSendMsg($message);

		adminmsg('operate_success');
	}
} elseif ($_POST['action'] == 'del') {

	InitGP(array('selid'),'P');
	if (!$selid = checkselid($selid)) {
		$basename = "javascript:history.go(-1);";
		adminmsg('operate_error');
	}
	$db->update("DELETE FROM pw_clientorder WHERE id IN($selid)");
	adminmsg('operate_success');
}
?>