<?php $root=$_SERVER['DOCUMENT_ROOT'];$wroot=dirname($_SERVER['SCRIPT_NAME']);while((strlen($wroot)>1)&&(!file_exists($root.$wroot.'/main')))$wroot=dirname($wroot);if($wroot=='/')$wroot='';require_once($root.$wroot.'/main/app/App.php');
	if (!$app->privileged('staff')) $app->nopage('This page can only be accessed by staff.');
	else{
		if ($app->getPost('c')=='du'){
			$u=$app->findUser($app->getPost('user'));
			if ($u) $app->delUser($u['uuid']);
		}
		else if ($app->getPost('c')=='utype'){
			$u=$app->findUser($app->getPost('user'));
			if ($u) $app->editUser($u['uuid'],['type'=>$_POST['type']]);
		}
		else if ($app->getPost('c')=='set'){
			$u=$app->findUser($app->getPost('user'));
			if ($u) $app->editUser($u['uuid'],['cred'=>$_POST['cred']]);
		}
		$app->heads('Users');
		$app->header();
?>			
<section>
	<article>
		<h1 class='article'>Users</h1><hr/>
		<table style='width:100%;overflow:scroll;'>
			<tr class='article' style='background:var(--article-fg);color:var(--article-bg);'>
				<th>Avatar</th><th>Username</th><th>Permissions</th><th>Credit</th><th>Delete</th>
			</tr>
<?php
	$users=$app->getUsers();
	foreach($users as $user){
		echo "<tr class='article'>";
		$icon="/main/users/{$user['user']}/avatar.png";
		if (!file_exists($icon)) $icon="/main/assets/icons/anonymous.png";
		echo "<td><img class='icon' src='{$app->wroot}$icon' /></td>";
		echo "<td>{$user['user']}</td>";
		if ($user['uuid']==1) echo "<td>{$user['type']}</td>";
		else if ($app->privileged('admin')){
			echo "<td><form method='post'><select name='type' onchange='form.submit();'>";
			foreach ($app->usertypes as $type=>$ord){
				if ($user['type']!==$type) echo "<option>$type</option>";
				else echo "<option selected='1'>$type</option>";
			}
			echo "</select><input type='hidden' name='c' value='utype' /><input type='hidden' name='user' value='{$user['user']}' /></form></td>";
		}
		else echo "<td>{$user['type']}</td>";
		if ($app->privileged('admin')){
			echo "<td><form style='display:inline;' method='post'><input type='hidden' name='user' value='{$user['user']}' /><input type='number' name='cred' value='{$user['cred']}' style='width:5rem;display:inline;' /><input type='submit' value='set' name='c' style='width:5rem;display:inline;' /></form></td>";
			if (($user['uuid']==1)) echo "<td></td>";
			elseif ($user['type']==='social') echo "<td><input class='blue round' onclick=\"alert('Can\'t delete a social login from here.');\" style='width:2rem;height:2rem;' title='Cant delete a social login from here' type='submit' value='?' /></td>";
			else echo "<td><form method='post'><input type='hidden' name='c' value='du' /><input type='hidden' name='user' value='{$user['user']}' /><input class='red round' style='width:2rem;height:2rem;' type='submit' value='X' /></form></td>";
		}
		else{
			echo "<td>{$user['cred']}</td>";
			echo "<td></td>";
		}
		echo "</tr>";
	}
?>
		</table>
	</article>
</section>
<?php
		$app->footer();
		$app->tails();
	}
?>
