<?php
require_once("includes/SQLConnect.php");
require_once("includes/user_data.php");

if($_POST['action']=="delete"){
	$message_ID = (int)$_POST['message_ID'];
	$user_ID = (int)$user_data['ID'];
	
	if(mysql_query("DELETE FROM User_Messages WHERE ID=".$message_ID." AND to_user_ID = ".$user_ID." LIMIT 1"))
		$success = "Message deleted.";
	else
		$error = "The message was not deleted, probably because it was deleted previously.";
}
include_once("includes/header.php");


?>


<?php
/*
	
	I think there should be a feature that forwards received mail to the person's real email address.
*/
?>

<form action="message_compose.php" method="POST" class='action'>
	<input type=submit class="submit" value="Compose Message">
</form>

<h2>
	Inbox
</h2>

<table cellspacing="0" id="inbox">
	<thead>
		<tr>
			<th style='text-align:left' width="150">Sender</th>
			<th>Subject</th>
			<th width="180" style="text-align:left">Date and Time</th>
			<th width="120">Reply/Delete</th>
		</tr>
	</thead>
	<tbody>
<?php
$sql = "SELECT User_Messages.ID,User_Messages.from_user_ID,User_Messages.open_flag,User_Messages.to_user_ID,UNIX_TIMESTAMP(User_Messages.TS) as 'timestamp',User_Messages.subject,LEFT(User_Messages.body,80) as 'excerpt',CONCAT(Users.first_name,' ',Users.last_name) as 'name_concat' FROM User_Messages,Users WHERE Users.ID = User_Messages.from_user_ID AND User_Messages.to_user_ID = ".$user_data['ID'];


$result = mysql_query($sql);
while($message = mysql_fetch_assoc($result)){
	?>
	
	<tr <?php
		if(!$message['open_flag'])
			print "class='unread'";
		?>>
		
		<td class="from">
			<?php 
			$query = sprintf("SELECT photo_URL FROM `Users` WHERE `ID`='%s'",
			mysql_real_escape_string($message['from_user_ID']));
			$extension = mysql_result(mysql_query($query),0);
			$thumb_image_location = 'images/profiles/thumbnail_'.$message['from_user_ID'].'.'.$extension;
			if(file_exists($thumb_image_location))

				print '<img style="float: left;" src="'.$thumb_image_location.'" alt="Thumbnail Image"/>';
			?>
			<p><a href="profile_user.php?ID=<?=$message['from_user_ID'];?>"><?=$message['name_concat'];?></a></p>
		</td>
		<td class='message'>
			<a href="message_view.php?ID=<?=$message['ID'];?>">
				<?=$message['subject'];?>
			</a>
			
			<p class='excerpt'>
				<?=$message['excerpt'];?>...
			</p>
		</td>
		<td style="text-align:left">
			<?=date("F jS",$message['timestamp']);?>
			<b>
				<?=date("g:i a",$message['timestamp']);?>
			</b>
		</td>
		<td style="text-align:center">
			<form action="message_compose.php" method="POST" id="action-reply" class='tiny'>
				<input type="hidden" class="hidden" name="message_ID" value="<?=$message['ID'];?>">
				<input type="hidden" class="hidden" name="action" value="reply">
				<input type="submit" class="submit" value="Reply">
			</form>

			<form action="message_inbox.php" method="POST" id="action-delete" class='tiny'>
				<input type="hidden" class="hidden" name="action" value="delete">
				<input type="hidden" class="hidden" name="message_ID" value="<?=$message['ID'];?>">
				<input type="submit" class="submit" value="Delete">
			</form>
		</td>
	
	</tr>
	
	<?php
}

?>
</table>

<script type="text/javascript">
	$('table#inbox tr:odd').css({'background-color' : '#f2f8ff'});
	$('table#inbox tr.unread td').css({'border-top' : '3px solid #FFFF66','border-bottom' : '3px solid #FFFF66','background-color' : '#ffffa9'});
</script>

<?php
include_once("includes/footer.php");
?>
