<?php
session_start();
require_once 'MessageDAO.class.php';
require_once 'MemberDAO.class.php';


require_once 'auth.php';

$fbids = MessageDAO::getCounterpartFBIDByFBID($fbid);
$HEADMESSAGE_LEN = 20;
$messages = array();
foreach ($fbids as $withId) {
	$withMember = MemberDAO::getByFBId($withId);
	$tmp = MessageDAO::getMessage($fbid, $withId);
	$message = $tmp[count($tmp) - 1];
	$messages[] = array(
			"withId" => $withId,
			"name" => $withMember->first_name,
			"age" => $withMember->getAge(),
			"picture" => $withMember->getPictureUrl(),
			"time" => $message->date,
			"is_read" => ($message->from_fbid == $withId && $message->read == 0) ? 0 : 1,
			"head_message" => mb_substr($message->text, 0, (strlen($message->text) > $HEADMESSAGE_LEN ? $HEADMESSAGE_LEN : NULL), "UTF-8")
	);
}


?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>メッセージ一覧</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script>

</script>
</head>
<body>
<div data-role="page" id="message_list">
<div data-role="header" style="overflow:hidden;" data-tap-toggle="false">
<h1>メッセージ一覧</h1>
</div>
<div role="main" class="ui-content">
<!-- 
<ul data-role="listview" data-inset="true">
<li data-role="list-divider">保留中のリクエスト <span class="ui-li-count">2</span></li>
	<li data-icon="info"><a href="index.html">
	<h2>Stephen Weber</h2>
	<p><strong>You've been invited to a meeting at Filament Group in Boston, MA</strong></p>
	<p>Hey Stephen, if you're available at 10am tomorrow, we've got a meeting with the jQuery team.</p>
		<p class="ui-li-aside"><strong>6:24</strong>PM</p>
	</a></li>
	<li data-icon="info"><a href="index.html">
	<h2>Stephen Weber</h2>
	<p><strong>You've been invited to a meeting at Filament Group in Boston, MA</strong></p>
	<p>Hey Stephen, if you're available at 10am tomorrow, we've got a meeting with the jQuery team.</p>
		<p class="ui-li-aside"><strong>6:24</strong>PM</p>
	</a></li>
</ul>
-->

<ul data-role="listview" data-inset="true">
	<li data-role="list-divider">会話一覧 <span class="ui-li-count"><?php echo count($messages);?></span></li>
<?php
foreach ($messages as $message) {
	?>
	<li><a href="conversation.php?with=<?php echo $message["withId"];?>" data-transition="slidefade">
	<img src="<?php echo $message["picture"];?>">
	<h2><?php echo $message["name"];?></h2>
	<p><?php
	if ($message["is_read"] == 0) {
		echo "<b>" . $message["head_message"] . "</b>";
	} else {
		echo $message["head_message"];
	}
	?>
</p>
	<p class="ui-li-aside"><?php echo $message["time"];?></p>
	<?php
	if ($message["is_read"] == 0) {
		echo '<span class="ui-li-count">1</span>';
	} else {

	}
	?>
	</a></li>
	
<?php 
}
?>

</ul>
</div>
<div data-role="footer" style="overflow:hidden;" data-position="fixed" data-tap-toggle="false">
	<div data-role="navbar">
	<ul>
		<li><a href="/" data-icon="grid" data-transition="fade" class="ui-btn ui-corner-all ui-shadow ui-btn-inline" data-direction="reverse">周辺のゆる募</a></li>
		<li><a href="#" data-icon="star" class="ui-btn-active ui-state-persist">会話</a></li>
		<li><a href="my_profile.php" data-icon="gear"  data-transition="fade" class="ui-btn ui-corner-all ui-shadow ui-btn-inline">自分のゆる募</a></li>
	</ul>
	</div>
</div>

</div>

</body></html>