<?php

include_once ("includes/setup.inc.php");

//Header("Content-type: application/xml");
Header("Cache-Control: no-cache");

$num_errors = 0;

$id_peranan = fetch_postget_val("id_peranan", $_POST, $_GET);
$jenis_sc = fetch_postget_val("jenis_sc", $_POST, $_GET);
$status_sc = fetch_postget_val("status_sc", $_POST, $_GET);
$id_pengguna = fetch_postget_val("id_pengguna", $_POST, $_GET);
$id_program = fetch_postget_val("id_program", $_POST, $_GET);
$tahun_login_year = fetch_postget_val("tahun_login_year", $_POST, $_GET);

$indicator_Query = "SELECT *
					FROM t_peranan
					WHERE aktif = 1";
$PDO_stmt = $PDO->prepare($indicator_Query);
$PDO_stmt->bindValue(1, $myID, PDO::PARAM_INT);
$PDO_stmt->execute();
$peranan_List = [];
while($result = $PDO_stmt->fetch(PDO::FETCH_ASSOC))
{
	if($id_peranan == 0 || $id_peranan == $result['id_peranan'])
	{
		$peranan_List[$result['id_peranan']] = $result['desk_peranan'];
	}
}

$param = '';
$join = '';
if($id_peranan == 10)
{	
	$param = ', C.id_program';
	$join = ' LEFT JOIN t_pemohon B ON B.id_pemohon = A.id_pengguna
			  LEFT JOIN m_pemohon C ON C.id_pemohon = B.id_pemohonsys';	
}

$query = "SELECT MONTH(A.tkh_log) AS month_log, A.id_peranan, count(A.id_loginlog) AS total$param
				FROM s_loginlog A
				$join
				WHERE A.tkh_log BETWEEN ? AND ?";

if($id_peranan > 0)
{
	$query .= ' AND A.id_peranan = '.$id_peranan;		
	
	if($id_program > 0)
	{
		if($id_program == 5)
		{
			$query .= ' AND C.id_program = 0';				
		}
		else
		{
			$query .= ' AND C.id_program = '.$id_program;				
		}
	}
}

if($jenis_sc > 0)
{
	$jenis = ($jenis_sc==1)?'I':'O';
	$query .= " AND A.jenis_log = '".$jenis."'";		
}

if($status_sc > 0)
{
	$status = ($status_sc==1)?'B':'G';
	$query .= " AND A.status_login = '".$status."'";		
}

if($id_pengguna > 0)
{
	$query .= ' AND A.id_pengguna = '.$id_pengguna;		
}

$query .= " GROUP BY MONTH(A.tkh_log), A.id_peranan";

$PDO_stmt = $PDO->prepare($query);

$PDO_stmt->bindValue(1, $tahun_login_year.'-01-01 00:00:00', PDO::PARAM_STR);
$PDO_stmt->bindValue(2, $tahun_login_year.'-12-31 23:59:59', PDO::PARAM_STR);
$PDO_stmt->execute();
$login_all = [];
$login_month = [];
$login_peranan = [];
while($res = $PDO_stmt->fetch(PDO::FETCH_ASSOC))
{
	$login_all[$res['month_log']][$res['id_peranan']] = $res['total'];
	$login_month[$res['month_log']] += $res['total'];
	$login_peranan[$res['id_peranan']] += $res['total'];
}

$name = "statTable_all";
$title = "Laporan Login/Logout Pengguna Sistem";

echo '<h4><label id="'.$name.'_title">'.$title.'</label></h4>';
echo '<div class="table-responsive">';
echo '<table class="table table-striped table-bordered table-hover statTable" id="'.$name.'" name="'.$name.'" style="text-align:center">';
	echo '<thead>';
		echo '<tr>';
			echo '<td>Peranan / Bulan</td>';
			/*
			foreach($peranan_List as $key=>$item)
			{
				echo '<td>'.$item.'</td>';
			}
			*/
			for($num = 1; $num <= 12; $num++)
			{
				echo '<td>'.monthToMalayShort($num).'</td>';
			}
			echo '<td>Jumlah</td>';
		echo '</tr>';
	echo '</thead>';
	echo '<tbody>';
		//for($num = 1; $num <= 12; $num++)
		foreach($peranan_List as $key=>$item)
		{
			echo '<tr>';
				//echo '<td>'.monthToMalayShort($num).'</td>';
				echo '<td>'.$item.'</td>';
				//foreach($peranan_List as $key=>$item)
				for($num = 1; $num <= 12; $num++)
				{
					echo '<td>'.($login_all[$num][$key]>0?$login_all[$num][$key]:0).'</td>';
				}
				//echo '<td>'.($login_month[$num+1]>0?$login_month[$num+1]:0).'</td>';
				echo '<td>'.($login_peranan[$key]>0?$login_peranan[$key]:0).'</td>';
			echo '</tr>';
		}
		echo '<tr style="font-weight:bold">';
			echo '<td>Jumlah Keseluruhan</td>';
			$jumlah = 0;
			//foreach($peranan_List as $key=>$item)
			for($num = 1; $num <= 12; $num++)
			{
				//echo '<td>'.($login_peranan[$key]>0?$login_peranan[$key]:0).'</td>';
				echo '<td>'.($login_month[$num]>0?$login_month[$num]:0).'</td>';
				$jumlah += $login_month[$num];
			}
			echo '<td>'.($jumlah).'</td>';
		echo '</tr>';
	echo '</tbody>';
	echo '</table>';
echo '</div>';

?>