<?php 
$messageid=$_POST["messageid"];
$rolenumber=$_SESSION["rolenumber"];

$date=date("Y-m-d");
$update_message=$dbh->query("update message set dateread='$date' where messageid='$messageid' and sender!='$rolenumber'");

$result_message=$dbh->query("select * from message where messageid='$messageid' order by autoid asc");
$row_message=$result_message->fetchObject();

$result_users=$dbh->query("select * from users where rolenumber='$row_message->sender'");
$row_users=$result_users->fetchObject();

echo "
<div class='col-lg-12 alert alert-warning'><span style='cursor:pointer;' onclick='message_back()'>< ".$row_users->firstname." ".$row_users->lastname."</span> <span style='float:right;cursor:pointer;' onclick='reply_text()' >Reply ></span></div>
<div class='col-lg-12'>
<b><i>Message thread</i></b><br>
<div class='panel panel-green'>
<div style='overflow-y:scroll;height:250px;' id='message_out' class='panel-body'>

<table width='100%' border='0'>"; 

do{
if($rolenumber==$row_message->sender){

echo "
<tr>
<td width='40%'></td>
<td width='20%'></td>
<td width='40%' align='right'>
<div style='color:#3c763d;border-radius:10px;padding:20px;background-color:#dff0d8;font-size:15px;margin-bottom:2px;word-wrap:break-word;'>
".$row_message->message."

<span style='float:left;font-size:12px;'>".$row_message->dater."</span>
</div>
</td>

</tr>
";	
}else{
echo "
<tr>
<td width='40%' align='left'>
<div style='color:#333;border-radius:10px;padding:20px;background-color:#d9edf7;font-size:15px;margin-bottom:2px;word-wrap:break-word;'>
".$row_message->message."
<img src='img/tick.png' alt='tick'>
<span style='float:right;font-size:12px;'>".$row_message->dater."</span>
</div>
</td>
<td width='20%'></td>
<td width='40%'></td>
</tr>"; 
}
}while($row_message=$result_message->fetchObject());

$rolenumber=$_SESSION["rolenumber"];
$result_message=$dbh->query("select * from message where messageid='$messageid' order by autoid desc");
$row_message=$result_message->fetchObject();

if($row_message->sender==$rolenumber){
	$replier=$row_message->recepient;
}elseif($row_message->sender!=$rolenumber){
	$replier=$row_message->sender;
}
echo "
</table></div>
</div>
<div style='opacity:0;width:5%;' id='reply_box'>
<div class='col-lg-11'>
<input type='hidden' id='message_id' value='".$messageid."'>  
<input type='hidden' id='reply_user' value='".$replier."'>  
<textarea id='dareply' class='form-control' placeholder='Reply'></textarea>
</div>
<div class='col-lg-1'>
<input type='submit' onclick='insert_reply()' class='btn btn-sm btn-primary' value='Send'>
</div>
</div>
</div>";
?>