<?php

require '../../lib/glob.php';
require '../../lib/muoribene.php';
require '../../lib/sessionstart.php';
require '../../lib/myconn.php';
require '../../lib/getadmacc.php';
if ($account['Level']=='guest')
	muoribene('Sorry, you are not authorized.',true);
require '../../lib/n2es.php';
require '../../lib/menu.php';
$menu['menu']['selected']=true;
$menu['menu']['submenu']['platforms']['href']=null;
$menu['menu']['submenu']['platforms']['selected']=true;
buildmenu($menu);

$dbg='';
$dbg.='$account: <pre>'.print_r($account,1).'</pre>'.N.'$POST: <pre>'.print_r($_POST,1).'</pre>'.N;

use function mysqli_real_escape_string as myesc;

// praticamente una macro
function hspech($str) {
	return(htmlspecialchars($str,ENT_QUOTES|ENT_HTML5,'UTF-8'));
}

foreach ($_POST as $key=>$val) {
	if (preg_match('#^cons(\d+)$#',$key,$matches)===1) {
		if ($val==='0')
			$val='1';
		elseif ($val==='1')
			$val='0';
		elseif ($val==='2')
			$val='NULL';
		else
			$val=false;
		if ($val!==false) {
			$que="UPDATE Platforms SET Consider={$val} WHERE ID={$matches[1]}".N;
			$dbg.=$que.N;
			mysqli_query($link,$que)
				or muoribene(__LINE__.': '.$dbg.'<br>'.N.mysqli_error($link),true);
		}
	}
}

$mastplats=[];
$res=mysqli_query($link,'SELECT * FROM Platforms WHERE Consider=1')
	or muoribene(__LINE__.': '.$dbg.'<br>'.N.mysqli_error($link),true);
while ($row=mysqli_fetch_assoc($res))
	$mastplats[]=$row;
$nonmastplats=[];
$res=mysqli_query($link,'SELECT * FROM Platforms WHERE Consider!=1 OR Consider IS NULL')
	or muoribene(__LINE__.': '.$dbg.'<br>'.N.mysqli_error($link),true);
while ($row=mysqli_fetch_assoc($res))
	$nonmastplats[]=$row;

$dbg.='$mastplats: <pre>'.print_r($mastplats,1).'</pre>'.N;
$dbg.='$nonmastplats: <pre>'.print_r($nonmastplats,1).'</pre>'.N;

$mastplatscount=count($mastplats);
$nonmastplatscount=count($nonmastplats);

if ($mastplatscount>0 || $nonmastplatscount>0) {
	$out='<form method="post">'.N.'<table class="plattable">'.N;
	if ($mastplatscount>0)
		$out.=showrecs($mastplats,'Piattaforme Mastodon');
	if ($nonmastplatscount>0)
		$out.=showrecs($nonmastplats,'Piattaforme non-Mastodon');
	$out.='<tr><th colspan="4"><button>Salva le modifiche</button></th></tr>'.N.'</table>'.N.'</form>'.N;
} else {
	$out='<p>La tabella delle piattaforme è vuota!</p>'.N;
}

mysqli_close($link);

function showrecs($platarr,$title) {
	global $link;
	$selopts=['Si','No','Non so'];
	$out='<tr><th colspan="4" class="title">'.$title.'</th></tr>'.N.'<tr><th>Piattaforma</th><th>Istanze di esempio</th><th>È Mastodon?</th><th>Commento</th></tr>'.N;;
	foreach ($platarr as $plat) {
		$res=mysqli_query($link,'SELECT URI FROM Instances WHERE Software="'.myesc($link,$plat['Name']).'" ORDER BY ID DESC LIMIT 0,5')
			or muoribene(__LINE__.': '.$dbg.'<br>'.N.mysqli_error($link),true);
		$exinsts=[];
		while ($row=mysqli_fetch_assoc($res))
			$exinsts[]="<a href=\"https://{$row['URI']}\" target=\"blank\">{$row['URI']}</a>";
		if (is_null($plat['Consider']))
			$seli=2;
		elseif ($plat['Consider']==1)
			$seli=0;
		else
			$seli=1;
		$sel="<select name=\"cons{$plat['ID']}\">\n";
		for ($i=0; $i<3; $i++) {
			($i==$seli) ? $selected=' selected' : $selected='';
			$sel.="<option value=\"{$i}\"{$selected}>{$selopts[$i]}</option>\n";
		}
		$sel.="</select>\n";
		$out.="<tr><td>{$plat['Name']}</td><td>".implode(' ',$exinsts)."</td><td>{$sel}</td><td>...</td></tr>\n";
	}
	return $out;
}

?>
<!DOCTYPE HTML>
<html lang="it">
<head>
<title>Mustard - Piattaforme</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="description" content="Admin pages for Mastodon Help">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link rel="icon" type="image/png" href="imgs/icona-32.png" sizes="32x32">
<link rel="icon" type="image/png" href="imgs/icona-192.png" sizes="192x192">
<link rel="icon" type="image/png" href="imgs/icona-512.png" sizes="512x512">
<link rel="apple-touch-icon-precomposed" href="imgs/icona-180.png">
<link rel="stylesheet" type="text/css" href="css/theme.css?v=<?php echo($cjrand); ?>">
<link rel="stylesheet" type="text/css" href="css/platforms.css?v=<?php echo($cjrand); ?>">
<script language="JavaScript" src="js/menu.js?v=<?php echo($cjrand); ?>"></script>
<script language="JavaScript" src="js/confirma.js?v=<?php echo($cjrand); ?>"></script>
<script language="JavaScript" src="js/alerta.js?v=<?php echo($cjrand); ?>"></script>
<script language="JavaScript">
</script>
</head>
<body>

<nav>
<div id="hmenu">
<ul>
<?php echo($menuout); ?>
</ul>
<div class="mtit">Piattaforme</div>
<div id="rightdiv">
	<img src="imgs/esci.svg" class="rlinks" title="Logout" onclick="document.location.href='logout.php';">
</div>
</div>
</nav>

<div id="popup">
<div id="inpopup">
<div id="popupcont">
...
</div>
</div>
</div>

<div id="debug">
<?php echo($dbg); ?>
</div>

<div id="fullscreen">
<div id="middlerow">

<?php
echo $out;
?>

</div>
</div>

</body>
</html>
