<?php 
session_start();

include("include/settings.php");

$db = new SQLite3('ask.db');

$p_user = $db->query("SELECT * FROM users WHERE username = '" . $_GET["q"] . "';")->fetchArray(SQLITE3_ASSOC);
if(!$p_user || !$p_user["id"]){
	include("404.php");
	die();
}

if(isset($_SESSION["uid"])){
	if($_SESSION["uid"] == $p_user["id"]){
		$is_current_user = true;
	}
}
?>

<html>
<head>
	<?php include("themes/$theme_name/user.php"); ?>
	<title><?= $p_user["username"] ?> | LibreCat</title>
</head>
<body> 
	<?php // print_profile($p_user); ?>
	<?php include("include/header.php"); ?>
<div id="parent-container">
	<div id="user-container">
		<p id="user-name"><?= $p_user["username"] ?></p>
		<p class="user-bio"><?php
//			if($p_user["bio"]) echo($p_user["bio"]);
//			else echo("This is a sample bio, please change me, this is just to test the layout.");
		?></p>
	<?php
		if($is_current_user){
			echo("<br/>");
			if($fancy_urls)
				echo("<a href='/config/profile'>config</a>");
			else
				echo("<a href='/config.php?q=profile'>config</a>");
		}
	?>
	</div>

	<div class="columns-container">
	<div class="column form-column">
	<form name="input" action="/action/publish-question.php" method="post">
		<textarea placeholder="Ask me anything!" id="post-text" name="post-text"></textarea>
		<br/>
		<input type="hidden" name="uid" value="<?= $p_user["id"] ?>"/>
		<p><input type="checkbox" <?php if(!isset($_SESSION["uid"])) echo("checked disabled"); ?> name="anon"/> Post anonymously</p>
		<?php if(isset($errorMsg)) echo "<p>$errorMsg</p>\n"; ?>
		<input type="submit" value="Ask" name="post-submit"/>
	</form>

	</div>
	<div class="question-container column">
	<?php
		$u_prep = $db->prepare("SELECT * FROM users WHERE id = :id");
		$qs = $db->query("SELECT * FROM questions WHERE user = '" . $p_user["id"] . "' ORDER BY id DESC LIMIT 0, 10;");

		$time = new DateTime("@0");
		while($current = $qs->fetchArray(SQLITE3_ASSOC)){
			// Execute prepared statement
			$u_prep->bindValue(":id", $current["by"], SQLITE3_INTEGER);
			$q_user = $u_prep->execute()->fetchArray(SQLITE3_ASSOC);

			echo("<div class='question'>");
			
			if($q_user["id"] == 0) echo("<p class='question-username'>" . $q_user["name"] ."</p>");
			else if($fancy_urls) echo("<p class='question-username'><a href='/user/" . $q_user["name"] . "'>" . $q_user["name"] ."</a></p>\n");
			else echo("<p class='question-username'><a href='/user.php?q=" . $q_user["name"] . "'>" . $q_user["name"] ."</a></p>\n");
			echo("\t<p class='question-text'>". $current["question"] ."</p>\n");
			
			if($is_current_user){
				echo("\t<a href='/action/reply.php?q=".$current["id"]."'>reply</a> ");
				echo("\t<a href='/action/delete-question.php?q=".$current["id"]."'>delete</a> ");
				echo("fav ignore ");
			}
			
			// Time
			$time->settimestamp($current["q_date"]);
			echo("<p class='question-date'>" . $time->format("y-m-d h:i:s") . "</p>");
	
			if($current["answer"]){
				echo("<div class='answer'>");
				echo("<p class='answer-text'>" . $current["answer"] . "</p>");
				$time->settimestamp($current["a_date"]);
				echo("<p class='answer-date'>" . $time->format("y-m-d h:i:s") . "</p>");
				echo("</div>");
			}
			echo("\n\n");

			echo("</div>");
		}
	?>
	</div>
</div>
</div>
	<div id="footer">
		<p>
			Powered by Librecat, under the GPL3 license.
			Source code: <a href="https://git.posweg.es/posweg/librecat">https://git.posweg.es/posweg/librecat</a>
		</p>
	</div>
</body>
</html>
