<?php
	define('thisscript','transactionhistory');

	require_once 'globalad.php';	
	require_once '../class/TransactionHistory.php';	
	$th = new TransactionHistory();
	
	if (empty($userid)){
		redirecting("index.php");
	}
	
	if (empty($useraccess['manage_transaction_history'])){
		redirecting('index.php');
	}
	
	/* sanitize post, get, request */
	$_POST['replayedit'] = ((empty($_POST['replayedit']))?"":$_POST['replayedit']);
	$_POST['do'] = ((empty($_POST['do']))?"":$_POST['do']);
	$_GET['getlist'] = ((empty($_GET['getlist']))?"":$_GET['getlist']);
	$_GET['keyword'] = ((empty($_GET['keyword']))?array():$_GET['keyword']);
	$_GET['field'] = ((empty($_GET['field']))?array():$_GET['field']);
	$_POST['cek'] = ((empty($_POST['cek']))?array():$_POST['cek']);
	$_POST['does'] = ((empty($_POST['does']))?"":$_POST['does']);
	$_GET['error'] = ((empty($_GET['error']))?"":$_GET['error']);		
	$_GET['actions'] = ((empty($_GET['actions']))?"":$_GET['actions']);
	$_GET['type'] = ((empty($_GET['type']))?"":$_GET['type']);
	$_POST['show_table'] = ((empty($_POST['show_table']))?"":$_POST['show_table']); 
	/* end sanitize */
	
	if (empty($general['showperpageadmin'])){
		$general['showperpageadmin'] = 20;
	}
	
	$pagelink = '';
	$listr = '';
	$listaction = '';
	$show_table_list = false;
	
	if ($_GET['getlist'] == 'pagenav'){
		$keywords = $_GET['keyword'];
		$fields = $_GET['field'];
		$alls = $th->searchHistory($keywords,$fields,'','','pagenav',$_GET['p']);
		echo $alls;
		exit;
	}
	else if ($_GET['getlist'] == 'ajax'){
		$keywords = $_GET['keyword'];
		$fields = $_GET['field'];
		$alls = $th->searchHistory($keywords,$fields,'','','data');
		$totalrows = sizeof($alls);
		$totalpgs = ceil($totalrows / $general['showperpageadmin']);
		$pgs = handlepage($_GET['p'],$totalpgs);
		$dblast = $th->searchHistory($keywords,$fields,$_GET['sortf'],$_GET['sortd'],'data',$pgs);
		$listtr = '';

		if (sizeof($dblast) > 0){
			foreach ($dblast as $result){
				if($_GET['type'] == 'bonus'){
					$listr .= '
						<tr>
							<td height="25" align="left">
							'.htmlspecialchars($result['notes']).'</td>
							<td height="25" align="center">
							'.htmlspecialchars($result['email']).'</td>
							<td height="25" align="left">
							'.htmlspecialchars($result['firstname'].' '.$result['lastname']).'</td>
							<td height="25" align="center">
							'.date('d-m-Y H:i:s', strtotime($result['transactiondate'])).'</td>
							<td height="25" align="right">'.number_format($result['nominal'], 2, $decimalseparator,$thousandseparator).'</td>
							
						</tr>
					';
					
				}
				else{
					$listr .= '
						<tr>
							<td height="25" align="center">
							'.(empty($result['transactionnumber'])?'-':htmlspecialchars($result['transactionnumber'])).'</td>
							<td height="25" align="left">
							'.htmlspecialchars($result['notes']).'</td>
							<td height="25" align="center">
							'.htmlspecialchars($result['email']).'</td>
							<td height="25" align="left">
							'.htmlspecialchars($result['firstname'].' '.$result['lastname']).'</td>
							<td height="25" align="center">
							'.date('d-m-Y H:i:s', strtotime($result['transactiondate'])).'</td>
							'.(($result['transaction_type'] == 'withdraw-bonus')?'
							<td height="25" align="right">-</td>
							<td height="25" align="right">'.number_format(abs($result['nominal']), 2, $decimalseparator,$thousandseparator).'</td>
							':'
							<td height="25" align="right">'.number_format($result['nominal'], 2, $decimalseparator,$thousandseparator).'</td>
							<td height="25" align="right">-</td>
							
							').'
						</tr>
					';
				}
			}
 		}
		else{
			$listr = '<tr><td colspan="6" align="center">NO DATA</td></tr>';
		}
			
		echo $listr;
		exit;
	}
	
	$type = '';
	if($_POST['do'] == 'filter_history')
	{
		$show_table_list = true;
		
		if(!empty($_POST['startdate']))
		{
			$start_date = date('Y-m-d', strtotime($_POST['startdate']));
		}
		if(!empty($_POST['enddate']))
		{
			$end_date = date('Y-m-d', strtotime($_POST['enddate']));
		}
		if(!empty($_POST['email']))
		{
			$email = $_POST['email'];
		}
		if(!empty($_POST['type']))
		{
			$type = $_POST['type'];
		}
		
	}
	
	/* $listaction = '
		<div align="right">
			With selected : 
			<select name="replayedit" style="text-align: right">
			<option value="2">Aktif di Website</option>
			<option value="3">Tidak Aktif di Website</option>
			<option value="4">Hapus Bank</option>		
			</select>&nbsp;&nbsp;<input type="submit" value="Go" class="btn btn-success btn-sm"></div><br>
		  '; */
	
	/* $menu = $_POST['replayedit'];
	if ($menu == 2){
		$alldep = $_POST['cek'];
		$sizealldep = sizeof($alldep);
		if ($sizealldep > 0){
			for ($ax = 0; $ax < $sizealldep; $ax++){
				 $db->query("UPDATE bank SET status=1 WHERE account_id ='".$alldep[$ax]."'");
			}
		}
	}
 	else if ($menu == 3){
		$alldep = $_POST['cek'];
		$sizealldep = sizeof($alldep);
		if ($sizealldep > 0){
			for ($ax = 0; $ax < $sizealldep; $ax++){
				 $db->query("UPDATE bank SET status=2 WHERE account_id='".$alldep[$ax]."'");
			}
		}
	}
	else if ($menu ==4){
		$alldep = $_POST['cek'];
		$sizealldep = sizeof($alldep);
		if ($sizealldep > 0){
			for ($ax = 0; $ax < $sizealldep; $ax++){
				$deposit->setId($alldep[$ax]);
				$deposit->deleteBank();
			}
		}
	} */
 
	require_once 'incl/global_template.php';
		
	$theme = gettemplate('transactionhistory');
	eval("\$theme = \"$theme\";");
	echo $theme;
?>