<?php


$checkRoom = $db->query("SELECT * FROM notice_rooms WHERE owner_id = '".$_SESSION['user_id']."' and room_type = 'help'");
if(count($checkRoom) == 0){				
	$createRoom  = $db->query("INSERT INTO notice_rooms(chat_name,owner_id,room_type) VALUES ('Help', '".$_SESSION['user_id']."', 'help')");
	$room_id = $db->lastInsertId();
}else{
	$room_id = $checkRoom[0]["room_id"];
} 
$data = $db->query("SELECT notice_messages.message_id as message_id, notice_messages.message as message, notice_messages.from_id as from_id, notice_messages.created_on as created_on, notice_messages.chat_type as chat_type, notice_messages.mime as mime FROM `notice_messages` join notice_room_message on notice_messages.message_id = notice_room_message.message_id WHERE notice_room_message.room_id = '".$room_id."' && notice_messages.message_id > '".$id."'");

$counts = 0;
$html = "";
if(count($data) > 0){
	foreach ($data as $key => $value) {
		
		if(date("Y-m-d", strtotime($request->get('last_date'))) <> date("Y-m-d", strtotime($value['created_on'])))	{
			if($counts == 0){
				$counts = 1;
				$prev_date = date("Y-m-d", strtotime($value['created_on']));
				$html .= '<br/><li class="date"><center><span class="label label-sm label-info date" data-id="'.date("Y-m-d", strtotime($value['created_on'])).'"  style="border-radius: 6px !important;">' . date("F j, Y", strtotime($value['created_on'])) . '</span></center></li>';
			}
		}
		if(date("Y-m-d", strtotime($request->get('last_date'))) <> date("Y-m-d", strtotime($value['created_on'])))	{
			if (date("Y-m-d", strtotime($prev_date)) <> date("Y-m-d", strtotime($value['created_on']))){
				$prev_date = date("Y-m-d", strtotime($value['created_on']));
				$html .= '<li class="date"><center><span class="label label-sm label-info date" data-id="'.date("Y-m-d", strtotime($value['created_on'])).'" style="border-radius: 6px !important;">' . date("F j, Y", strtotime($value['created_on'])) . '</span></center></li>';
			}
		}
		
		if($value["from_id"] == $_SESSION['user_id'] ){
			$class="out";
		}else{
			$class="in";
			// $data = $db->query("UPDATE notice_members SET status = 1 WHERE id = '". $value['id'] . "'");
		}
		$html .= '<li class="'.$class.'" id="cc" data-id="'.$value['message_id'].'">';
		$html .= '<br>';
		$html .= '<img class="avatar" alt="" src="'. showImage($value["from_id"]).'">';
		$html .= '<div class="message">';
		$html .= '<div class="desc">'.$value['message'];
		$html .= '<br/><span class="datetime"><a href="javascript:;">'.($value['from_id'] == 1 ? "Bot" : getProfileData($value["from_id"], "first_name"))."</a> at ".date("H:i", strtotime($value['created_on'])).'</span>'.'</div>';
		$html .= '</div>';
		$html .= '</li>';
	}
	
echo $html;
}

?>