<?php
if (!defined("root")) {
	define("root", $_SERVER['DOCUMENT_ROOT']);
}
require(root."/___simple_config.php");
session_start();
require(root."/".adminFolder."/_common.php");
require(root."/".adminFolder."/_lib/db.php");
require(root."/".adminFolder."/_lib/main.php");
require(root."/".adminFolder."/PHPMailer/PHPMailerAutoload.php");
require(root."/".adminFolder."/_lib/mail.php");
require(root."/".adminFolder."/_lib/auth.php");
require(root."/".adminFolder."/_lib/bootstraps.php");
$pageSubOf = "pages";
$pageName = "deposits";
$pageTitle = "Deposits";
$thisUrl = "deposits";
$ajax_page = "ajax_deposits.php";
$pageUrl = "/".adminFolder."/$thisUrl";
$a = cekpost(cleanQuery($_GET["a"]), "");
$err = "";
if ($a=="section_main") {
	if (!authorizedModule("deposits_main", $ses_kduser)) {
		unAuthorizedAccess("deposits_main");
	} else {
		$fil_stgl = trim(cekpost(cleanQuery($_REQUEST["fil_stgl"]), ""));
		$fil_etgl = trim(cekpost(cleanQuery($_REQUEST["fil_etgl"]), ""));
		$fil_username = trim(cekpost(cleanQuery($_REQUEST["fil_username"]), ""));
		$fil_kdbank = trim(cekpost(cleanQuery($_REQUEST["fil_kdbank"]), ""));
		$fil_kdgame = trim(cekpost(cleanQuery($_REQUEST["fil_kdgame"]), ""));
		$fil_norek = trim(cekpost(cleanQuery($_REQUEST["fil_norek"]), ""));
		$fil_namarek = trim(cekpost(cleanQuery($_REQUEST["fil_namarek"]), ""));
		$fil_status = trim(cekpost(cleanQuery($_REQUEST["fil_status"]), ""));
		$fil_sort = trim(cekpost(cleanQuery($_REQUEST["fil_sort"]), ""));
		$swhere = '';
		$sorder = '';
		$stgl = cekpost(cleanQuery($_REQUEST["fil_stgl"]), date('d-m-Y', time()));
		$etgl = cekpost(cleanQuery($_REQUEST["fil_etgl"]), date('d-m-Y', time()));
		$q_stgl = strtotime($stgl);
		$q_etgl = strtotime($etgl)+86400;
		$fil_stgl = strtotime($stgl);
		$fil_etgl = strtotime($etgl);
		if ($fil_kdbank!="") { $swhere .= " AND a.kdbank='$fil_kdbank' "; }
		if ($fil_kdgame!="") { $swhere .= " AND a.kdgame='$fil_kdgame' "; }
		if ($fil_username!="") { $swhere .= " AND c.username LIKE '%$fil_username%' "; }
		if ($fil_norek!="") { $swhere .= " AND a.norek LIKE '%$fil_norek%' "; }
		if ($fil_namarek!="") { $swhere .= " AND a.namarek LIKE '%$fil_namarek%' "; }
		if ($fil_status!="0") {
			if ($fil_status==1) { $swhere .= " AND a.ispending='1' "; }
			if ($fil_status==2) { $swhere .= " AND a.clear='1' "; }
			if ($fil_status==3) { $swhere .= " AND a.isrejected='1' "; }
		}
		if ($fil_sort!="0") {
			if ($fil_sort==1) { $sorder .= " ORDER BY jumlah DESC, tanggal DESC"; }
			if ($fil_sort==2) { $sorder .= " ORDER BY jumlah, tanggal DESC"; }
		} else {
			$sorder .= " ORDER BY tanggal DESC";
		}
		$q = "SELECT UNIX_TIMESTAMP(FROM_UNIXTIME(cleartime)) AS x, 
			UNIX_TIMESTAMP(FROM_UNIXTIME(".$q_stgl.")) AS y,
			UNIX_TIMESTAMP(FROM_UNIXTIME(".$q_etgl.")) AS z,
			a.*, a.username AS username_default, c.username, namabank, inisial, e.nama AS nama_game, COALESCE(depo.jdepo,0) AS jdepox, COALESCE(wd.jwd,0) AS jwdx
			FROM deposits a
			LEFT JOIN members b ON b.kdmember=a.kdmember
			LEFT JOIN accounts c ON c.kdaccount=b.kdaccount
			LEFT JOIN banks d ON d.kdbank=a.kdbank
			LEFT JOIN games e ON e.kdgame=a.kdgame
			LEFT JOIN (
				SELECT COUNT(kddeposit) AS jdepo, kdmember FROM deposits WHERE isclear=1
				GROUP BY kdmember
			) AS depo ON depo.kdmember = b.kdmember
			LEFT JOIN (
				SELECT COUNT(kdwithdraw) AS jwd, kdmember FROM withdraws WHERE isclear=1
				GROUP BY kdmember
			) AS wd ON wd.kdmember = b.kdmember
			WHERE 1=1
			$swhere
			AND UNIX_TIMESTAMP(FROM_UNIXTIME(tanggal))>=UNIX_TIMESTAMP(FROM_UNIXTIME(".$q_stgl.")) 
			AND UNIX_TIMESTAMP(FROM_UNIXTIME(tanggal))< UNIX_TIMESTAMP(FROM_UNIXTIME(".$q_etgl.")) 
			$sorder";
		callRS($rs, $q);
		if (count($rs)!=0) {
			$ld = 200;
			$cp = cekpost(cleanQuery($_GET["cp"]), "1");
			$tp = intval(count($rs) / $ld) + (count($rs)%$ld==0?0:1);
			$sr = ($cp-1) * $ld;
			$pagination = '';
			if ($tp!=1) {
				$pagination .= ('<ul class="pagination pagination-sm">');
					$disabled = '';
					$purl = '#';
					$onclick = '';
					if ($cp<=1) {
						$disabled = 'disabled';
						$onclick = ' onclick="return false;" ';
					}
					$pagination .= ('<li class="'.$disabled.'"><a href="'.$purl.'" '.($onclick!=""?$onclick:'onclick="filterReload(\''.$pageName.'\', 1);return false;"').'>&lt;&lt;</a></li>');
					$pagination .= ('<li class="'.$disabled.'"><a href="'.$purl.'" '.($onclick!=""?$onclick:'onclick="filterReload(\''.$pageName.'\', '.($cp-1).');return false;"').'>&lt;</a></li>');
					for ($c=1;$c<=$tp;$c++) {
						$active = '';
						$purl = '#';
						$disabled = '';
						$onclick = '';
						if ($c==$cp) {
							$active = 'active';
						} else {
							$purl = '#';
							$onclick = ' onclick="filterReload(\''.$pageName.'\', '.$c.');return false;" ';
						}
						$pagination .= ('<li class="'.$active.' '.$disabled.'"><a href="'.$purl.'" '.$onclick.'>'.$c.'</a></li>');
					}
					$disabled = '';
					$onclick = '';
					if ($cp>=$tp) {
						$disabled = 'disabled';
						$onclick = ' onclick="return false;" ';
					}
					$pagination .= ('<li class="'.$disabled.'"><a href="#" '.($onclick!=""?$onclick:'onclick="filterReload(\''.$pageName.'\', '.($cp+1).');return false;"').'>&gt;</a></li>');
					$pagination .= ('<li class="'.$disabled.'"><a href="#" '.($onclick!=""?$onclick:'onclick="filterReload(\''.$pageName.'\', '.$tp.');return false;"').'>&gt;&gt;</a></li>');
				$pagination .= ('</ul>');
			}
			cout('<div class="alert alert-info" role="alert"><strong>Total whole data</strong> <span class="badge">'.count($rs).'</span></div>');
			
			if ($tp!=1) {
				cout($pagination);
			}
			cout('<table class="table table-striped table-bordered">');
				cout('<thead>');
					cout('<tr>');
						cout('<th class="text-center">TR#ID</th>');
							if (authorizedModule("deposits_element_games", $ses_kduser)) {
								cout('<th class="text-center">Games</th>');
							}
							if (authorizedModule("deposits_element_username", $ses_kduser)) {
								cout('<th class="text-center">UserID</th>');
							}
							if (authorizedModule("deposits_element_banks", $ses_kduser)) {
								cout('<th class="text-center">Banks</th>');
							}
							if (authorizedModule("deposits_element_bank_account_number", $ses_kduser)) {
								cout('<th class="text-center">Bank Information</th>');
							}
							if (authorizedModule("deposits_element_amount", $ses_kduser)) {
								cout('<th class="text-center" colspan="2">Nominal</th>');
							}
							if (authorizedModule("deposits_element_date", $ses_kduser)) {
								cout('<th class="text-center">Date</th>');
							}
							if (authorizedModule("deposits_element_status", $ses_kduser)) {
								cout('<th class="text-center">Status</th>');
							}
							cout('<th class="text-center"></th>');
						cout('<th></th>');
					cout('</tr>');
				cout('</thead>');
				cout('<tbody>');
				$cnt = 0;
				for ($i=$sr;$i<count($rs) && $cnt<$ld;$i++) {
					$kddeposit = $rs[$i]["kddeposit"];
					$jdepox = $rs[$i]["jdepox"];
					$jwdx = $rs[$i]["jwdx"];
					$cnt++;
					$status = '';
					$status_time = '';
					$title_status = '';
					$cekUser = ($_SESSION["user"]["kduser"]=="1" || $_SESSION["user"]["kduser"]=="0")?1:0;
					$cekUser = 1;
					if (intval($rs[$i]["ispending"])==1) {
						$title_status = ($cekUser==1)?'Set Pending by '.$rs[$i]["pendingby"]."\n".'('.date('d-M-Y H:i:s', $rs[$i]["pendingtime"]).')':'';
						$status = '<div class="st-pending" title="'.$title_status.'">Pending</div>';
						$status_time = $rs[$i]["pendingtime"]!=""?convertSecToStr($rs[$i]["pendingtime"] - $rs[$i]["tanggal"]):'-';
					}
					if (intval($rs[$i]["isrejected"])==1) {
						$title_status = ($cekUser==1)?'Rejected by '.$rs[$i]["rejectedby"]."\n".'('.date('d-M-Y H:i:s', $rs[$i]["rejectedtime"]).')':'';
						$status = '<div class="st-reject" title="'.$title_status.'">Rejected</div>';
						$status_time = $rs[$i]["rejectedtime"]!=""?convertSecToStr($rs[$i]["rejectedtime"] - $rs[$i]["tanggal"]):'-';
					}
					if (intval($rs[$i]["isclear"])==1) {
						$title_status = ($cekUser==1)?'Approved by '.$rs[$i]["clearby"]."\n".'('.date('d-M-Y H:i:s', $rs[$i]["cleartime"]).')':'';
						$status = '<div class="st-clear" title="'.$title_status.'">Approved</div>';
						$status_time = $rs[$i]["cleartime"]!=""?convertSecToStr($rs[$i]["cleartime"] - $rs[$i]["tanggal"]):'-';
					}
					cout('<tr class="data-ke-'.$kddeposit.'">');
						cout('<td class="text-center">'.$kddeposit.'</td>');
						if (authorizedModule("deposits_element_games", $ses_kduser)) {
							cout('<td class="text-center">'.$rs[$i]["nama_game"].'</td>');
						}
						if (authorizedModule("deposits_element_username", $ses_kduser)) {
							cout('<td class="text-center">
								'.$rs[$i]["username_default"].'
								<div>Appr. DP cnt: <strong>'.$jdepox.'</strong></div>
								<div>Appr. WD cnt: <strong>'.$jwdx.'</strong></div>
							</td>');
						}
						if (authorizedModule("deposits_element_banks", $ses_kduser)) {
							cout('<td class="text-center">'.$rs[$i]["inisial"].'</td>');
						}
						if (authorizedModule("deposits_element_bank_account_number", $ses_kduser)) {
							cout('<td class="text-center">'.$rs[$i]["norek"].'<br/>'.$rs[$i]["namarek"].'</td>');
						}
						if (authorizedModule("deposits_element_amount", $ses_kduser)) {
							cout('<td class="text-center">Rp.</td>');
							cout('<td class="text-center num">'.number_format($rs[$i]["jumlah"]).'</td>');
						}
						if (authorizedModule("deposits_element_date", $ses_kduser)) {
							cout('<td class="text-center">'.date('d-M-Y', $rs[$i]["tanggal"]).'<br/>'.date('H:i:s A', $rs[$i]["tanggal"]).'<br/>created by:<br/>'.$rs[$i]["creby"].'</td>');
						}
						if (authorizedModule("deposits_element_status", $ses_kduser)) {
							cout('<td class="text-center"><span class="nfstatus_time'.$kddeposit.'">'.$status.''.str_replace("\n", '<br/>', $title_status).'<br/>'.$status_time.'</span></td>');
						}
						cout('<td class="text-center">');
							cout('<div class="options'.$kddeposit.' row">');
								cout('<div class="col-xs-4">');
									if (authorizedModule("deposits_approve", $ses_kduser)) {
										$onclick = 'changeStatus(\'clear\', '.$kddeposit.');return false;';
										cout('<a href="#'.$onclick.'" onclick="'.$onclick.'" data-toggle="tooltip" title="Approve"><span class="glyphicon glyphicon-ok"></span><span class="hidden-xs hidden-sm"> Approve</span></a>');
									}
								cout('</div>');
								cout('<div class="col-xs-4">');
									if (authorizedModule("deposits_pending", $ses_kduser)) {
										$onclick = 'changeStatus(\'pending\', '.$kddeposit.');return false;';
										cout('<a href="#'.$onclick.'" onclick="'.$onclick.'" data-toggle="tooltip" title="Pending"><span class="glyphicon glyphicon-time"></span><span class="hidden-xs hidden-sm"> Pending</span></a>');
									}
								cout('</div>');
								cout('<div class="col-xs-4">');
									if (authorizedModule("deposits_reject", $ses_kduser)) {
										$onclick = 'changeStatus(\'reject\', '.$kddeposit.');return false;';
										cout('<a href="#'.$onclick.'" onclick="'.$onclick.'" data-toggle="tooltip" title="Reject"><span class="glyphicon glyphicon-remove"></span><span class="hidden-xs hidden-sm"> Reject</span></a>');
									}
								cout('</div>');
								cout('<div class="col-xs-12">');
									cout('<br/><br/>');
									if (authorizedModule("deposits_approve", $ses_kduser)) {
										$onclick = 'changeStatus(\'clear2\', '.$kddeposit.');return false;';
										cout('<a href="#'.$onclick.'" onclick="'.$onclick.'" data-toggle="tooltip" title="Approve No SMS"><span class="glyphicon glyphicon-ok"></span><span class="hidden-xs hidden-sm"> Approve No SMS</span></a>');
									}
								cout('</div>');
							cout('</div>');
						cout('</td>');
					cout('</tr>');
				}
				cout('</tbody>');
			cout('</table>');
			
			if ($tp!=1) {
				cout($pagination);
			}
			cout('<div class="alert alert-info" role="alert"><strong>Total whole data</strong> <span class="badge">'.count($rs).'</span></div>');
			$act_ket = "Deposit: Main List";
			if ($swhere!="") { $act_ket .= ": ".cleanQuery($swhere).""; }
			updateUserActivities($act_ket);
		} else {
			cout('<div class="alert alert-info" role="alert">');
				cout('<strong>No data available.</strong> Wait some players to make some deposits.');
			cout('</div>');
		}
	}
}
if ($a=="changestatus") {
	$type = cekpost(cleanQuery($_REQUEST["type"]), "");
	$kddeposit = cekpost(cleanQuery($_REQUEST["kddeposit"]), "");
	$rs=array();$q="SELECT * FROM deposits WHERE kddeposit='$kddeposit'";
	callRS($rs, $q);
	$target = '';
	$cekUser = ($_SESSION["user"]["kduser"]=="1" || $_SESSION["user"]["kduser"]=="0")?1:0;
	$cekUser = 1;
	$title_status = '';
	if ($type=="clear" || $type=="clear2") {
		$changeby = $_SESSION["user"]["inisial"];
		$changetime = time();
		$ispending = 0;
		$isrejected = 0;
		$isclear = 1;
		$qu = "UPDATE deposits SET ispending='$ispending', isrejected='$isrejected', isclear='$isclear', clearby='$changeby', cleartime='$changetime' WHERE kddeposit='$kddeposit'";
		execRS($qu);
		$title_status = ($cekUser==1)?'Approved by '.$rs[0]["clearby"].'<br/>('.date('d-M-Y H:i:s', $rs[0]["cleartime"]).')':'';
		$target .= 'nfstatus_time'.$kddeposit.'$$<div class="st-clear" title="'.$title_status.'">Approved</div>'.str_replace("\n", '<br/>', $title_status).'<br/>'.convertSecToStr($changetime-$rs[0]["tanggal"]).'';
	}
	if ($type=="pending") {
		$changeby = $_SESSION["user"]["inisial"];
		$changetime = time();
		$ispending = 1;
		$isrejected = 0;
		$isclear = 0;
		$qu = "UPDATE deposits SET ispending='$ispending', isrejected='$isrejected', isclear='$isclear', pendingby='$changeby', pendingtime='$changetime' WHERE kddeposit='$kddeposit'";
		execRS($qu);
		$title_status = ($cekUser==1)?'Set Pending by '.$rs[0]["pendingby"].'<br/>('.date('d-M-Y H:i:s', $rs[0]["pendingtime"]).')':'';
		$target .= 'nfstatus_time'.$kddeposit.'$$<div class="st-pending" title="'.$title_status.'">Pending</div>'.str_replace("\n", '<br/>', $title_status).'<br/>'.convertSecToStr($changetime-$rs[0]["tanggal"]).'';
	}
	if ($type=="reject") {
		$changeby = $_SESSION["user"]["inisial"];
		$changetime = time();
		$ispending = 0;
		$isrejected = 1;
		$isclear = 0;
		$qu = "UPDATE deposits SET ispending='$ispending', isrejected='$isrejected', isclear='$isclear', rejectedby='$changeby', rejectedtime='$changetime' WHERE kddeposit='$kddeposit'";
		execRS($qu);
		$title_status = ($cekUser==1)?'Rejected by '.$rs[0]["rejectedby"].'<br/>('.date('d-M-Y H:i:s', $rs[0]["rejectedtime"]).')':'';
		$target .= 'nfstatus_time'.$kddeposit.'$$<div class="st-reject" title="'.$title_status.'">Rejected</div>'.str_replace("\n", '<br/>', $title_status).'<br/>'.convertSecToStr($changetime-$rs[0]["tanggal"]).'';
	}
	## do email & sms
		if ($type=="clear") {
			newSendMail("deposit_".$type, $kddeposit);
			#sendSMS("deposit_".$type, $kddeposit);
			sendSMSCP("deposit_".$type, $kddeposit);
		}
	## end do email & sms
	echo $target;
}
?>