<?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."/_lib/auth.php");
require(root."/".adminFolder."/_lib/bootstraps.php");
$pageSubOf = "Reports";
$pageName = "report_members";
$pageTitle = "Report Members";
$thisUrl = "reports/members";
$ajax_page = "ajax_report_members.php";
$pageUrl = "/".adminFolder."/$thisUrl";
$a = cekpost(cleanQuery($_GET["a"]), "");
$err = "";
if ($a=="section_main") {
	if (!authorizedModule("report_members_main", $ses_kduser)) {
		unAuthorizedAccess("report_members_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_telp = trim(cekpost(cleanQuery($_REQUEST["fil_telp"]), ""));
		$fil_email = trim(cekpost(cleanQuery($_REQUEST["fil_email"]), ""));
		$swhere = '';
		$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 b.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_telp!="") { $swhere .= " AND a.telp LIKE '%$fil_telp%' "; }
		if ($fil_email!="") { $swhere .= " AND a.email LIKE '%$fil_email%' "; }
		$q = "SELECT a.*, namabank, inisial, username, d.nama AS nama_game FROM members a
			LEFT JOIN accounts b ON b.kdaccount=a.kdaccount
			LEFT JOIN banks c ON c.kdbank=a.kdbank
			LEFT JOIN games d ON d.kdgame=a.kdgame
			WHERE 1=1
			$swhere
			AND UNIX_TIMESTAMP(FROM_UNIXTIME(tanggal_daftar))>=UNIX_TIMESTAMP(FROM_UNIXTIME(".$q_stgl.")) 
			AND UNIX_TIMESTAMP(FROM_UNIXTIME(tanggal_daftar))< UNIX_TIMESTAMP(FROM_UNIXTIME(".$q_etgl.")) 
			ORDER BY tanggal_daftar DESC";
		callRS($rs, $q);
		if (count($rs)!=0) {
			$ld = 50;
			$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">#ID</th>');
						if (authorizedModule("report_members_element_games", $ses_kduser)) {
							cout('<th class="text-center">Games</th>');
						}
						if (authorizedModule("report_members_element_username", $ses_kduser)) {
							cout('<th class="text-center">UserID</th>');
						}
						if (authorizedModule("report_members_element_banks", $ses_kduser)) {
							cout('<th class="text-center">Banks</th>');
						}
						if (authorizedModule("report_members_element_bank_account_number", $ses_kduser)) {
							cout('<th class="text-center">Bank Account Number</th>');
						}
						if (authorizedModule("report_members_element_bank_account_holder", $ses_kduser)) {
							cout('<th class="text-center">Bank Account Holder</th>');
						}
						if (authorizedModule("report_members_element_email", $ses_kduser)) {
							cout('<th class="text-center">Email</th>');
						}
						if (authorizedModule("report_members_element_phone", $ses_kduser)) {
							cout('<th class="text-center">Phone</th>');
						}
						if (authorizedModule("report_members_element_registration_time", $ses_kduser)) {
							cout('<th class="text-center">Reg. Date</th>');
						}
						if (authorizedModule("report_members_element_active", $ses_kduser)) {
							cout('<th class="text-center">Status?</th>');
						}
						if (authorizedModule("report_members_element_assign", $ses_kduser)) {
							cout('<th class="text-center">Assigned?</th>');
						}
					cout('</tr>');
				cout('</thead>');
				cout('<tbody>');
				$cnt = 0;
				for ($i=$sr;$i<count($rs) && $cnt<$ld;$i++) {
					$kdmember = $rs[$i]["kdmember"];
					$cnt++;
					$status = intval($rs[$i]["isactive"])==0?'Non-Active':'Active';
					$assigned = intval($rs[$i]["kdaccount"])==0?'Not Assigned Yet':'Assigned';
					cout('<tr class="data-ke-'.$kdmember.'">');
						cout('<td class="text-center">'.$kdmember.'</td>');
						if (authorizedModule("report_members_element_games", $ses_kduser)) {
							cout('<td class="text-center">'.$rs[$i]["nama_game"].'</td>');
						
						}
						if (authorizedModule("report_members_element_username", $ses_kduser)) {
							cout('<td class="text-center">'.$rs[$i]["username"].'</td>');
						
						}
						if (authorizedModule("report_members_element_banks", $ses_kduser)) {
							cout('<td class="text-center">'.$rs[$i]["inisial"].'</td>');
						
						}
						if (authorizedModule("report_members_element_bank_account_number", $ses_kduser)) {
							cout('<td class="text-center">'.$rs[$i]["norek"].'</td>');
						}
						if (authorizedModule("report_members_element_bank_account_holder", $ses_kduser)) {
							cout('<td class="text-center">'.$rs[$i]["namarek"].'</td>');
						}
						if (authorizedModule("report_members_element_email", $ses_kduser)) {
							cout('<td class="text-center">'.$rs[$i]["email"].'</td>');
						}
						if (authorizedModule("report_members_element_phone", $ses_kduser)) {
							cout('<td class="text-center">'.$rs[$i]["telp"].'</td>');
						}
						if (authorizedModule("report_members_element_registration_time", $ses_kduser)) {
							cout('<td class="text-center">'.date('d-M-Y H:i:s', $rs[$i]["tanggal_daftar"]).'</td>');
						}
						if (authorizedModule("report_members_element_active", $ses_kduser)) {
							cout('<td class="text-center">'.$status.'</td>');
						}
						if (authorizedModule("report_members_element_assign", $ses_kduser)) {
							cout('<td class="text-center">'.$assigned.'</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 = "Report Members: 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> Please search more players to join ^.^');
			cout('</div>');
		}
	}
}
?>