<?php
require_once "functions.php";

$results = dbw_query($db_conn,"SELECT * FROM GROUPS"); //All groups

while ($group = dbw_fetch_array($db_conn,$results)){ //Know name of all groups
	$groups[$group['ID_G']] =  $group['name'];
}


$results = dbw_query($db_conn,"SELECT * FROM CHECKS ORDER BY ID_G"); //All checks

while ($onechk = dbw_fetch_array($db_conn,$results)){
	$idchk = $onechk['ID_C'];
	$chks[$idchk] = $onechk; //First array data
	$chks[$idchk]['nameGroup'] = $groups[$onechk['ID_G']]; //Know group of this check

	switch ($chks[$idchk]['ID_TC']){
		// Case 1 and 2 are normal status, case 3 (Visits) is numeric, not status
		case 1: //Ping 
		case 2: //HttpCode
		case 4: //MySQL 

			if (dbw_query_fetch_array($db_conn, "SELECT COUNT(`ID_CHist`) FROM CHKHIST WHERE ID_C = '$idchk'")[0] != 0){
				

				$consul = dbw_query_fetch_array($db_conn, "SELECT `code`,`timestamp` FROM CHKHIST WHERE ID_C = '$idchk' AND (select min(`timestamp`) FROM CHKHIST)");
				$chks[$idchk]['dateLastChk'] = date('d/m H:i',$consul['timestamp']);

				$chks[$idchk]['failedLastChk'] = $consul['code'];

				$lastErr = dbw_query_fetch_array($db_conn, "SELECT MIN(`timestamp`) FROM CHKHIST WHERE ID_C = '$idchk' AND code != 0")[0]; //Record last error, for showing if there are any.
				if ($lastErr != false){
					$chks[$idchk]['dateLastErr'] = date('d/m H:i',$lastErr);
				}

				$ID_TC = $chks[$idchk]['ID_TC'];
				$chks[$idchk]['nameCheck'] = textTypeChk($T_,$ID_TC);

				if ($onechk['manStatus'] != ''){
					$chks[$idchk]['uptime'] = 255;//255 = Maintenance I suppose, not implemented
				}else{
					$chks[$idchk]['uptime'] = checkUptime($db_conn,$idchk);
				}

			}

		break;

		case 3: //Visits
			$visitsSite = dbw_query($db_conn, "SELECT * FROM (SELECT * FROM VISITS WHERE ID_C = '$idchk' ORDER BY `date` DESC LIMIT 0,5) sub ORDER BY `Date` ASC");
			//Create array from last 5 days
			while ($visitsDay = dbw_fetch_array($db_conn, $visitsSite)){
				$dateArray[] = $visitsDay['date'];
				$visitsArray[] = $visitsDay['visits'];
				$uniqueVisitsArray[] = $visitsDay['uniqueVisits'];
			}
			$chks[$idchk]['dateArray'] = implode(',',$dateArray);
			$chks[$idchk]['visitsArray'] = implode(',',$visitsArray);
		break;
	}

}

//Here load "News"

$incidents = dbw_query($db_conn,"SELECT * FROM NEWS ORDER BY ID_N DESC LIMIT 20");
while ($incident = dbw_fetch_array($db_conn,$incidents)){
	$ID_N = $incident['ID_N'];
	$incs[$ID_N]['day'] = date('j/n/Y', $incident['timestamp']);
	require_once "lib/parsedown.php"; //Import parsedown (once)
		
	$incs[$ID_N]['sentBy'] = $incident['sentBy'];
	if ($incident['sentBy'] != NULL && substr($incident['sentBy'],0,1) == "S"){ // Sent by a page 
		$pageinc = substr($incident['sentBy'],1);
		$pageinc = dbw_query_fetch_array($db_conn,"SELECT name FROM CHECKS WHERE ID_C = '$pageinc'")[0];
		$incs[$ID_N]['sentBy'] = $pageinc;
	}
	$incs[$ID_N]['text'] = Parsedown::instance()
			->setMarkupEscaped(true) # escapes markup (HTML)
			->text($incident['text']);
	$incs[$ID_N]['ID_N'] = $incident['ID_N'];
}

echo $twig->render('indexpage.twig', array('T_' => $T_, 'you' => $you, 'checks' => $chks, 'news' => $incs)); //Render
?>
