<?php 
session_start();
include '../db.php';
error_reporting(0);
include("../all_query.php");

 if(isset($_SESSION['loan_crm_username']) && isset($_SESSION['loan_crm_password']))
{
	
	
 	
	 $advisor_id = $_SESSION['loan_crm_userid'];
	$login_name = $_SESSION['login_name'];
	
	 
	
	$search_contact = $_REQUEST['search_contact'];
	
	  
	
	   $today_date = date('Y-m-d');
	


?>
                    <ul class="mailbox list-style-none" style="list-style: none;
  padding-left: 0;">
                        <li>
                            <div class="message-center chat-scroll">
                               
							   
							   <?php 
							   
							   $listArray = array();
							   
							   $listQ = mysqli_query($conn,"select *  from `advisors` where id<>$advisor_id   ");
							   
							   while($advData = mysqli_fetch_assoc($listQ))
							   {
								   
								   $sender_id = $advData['id'];
								   
								   
								   // get unread count 
		    $newChatQ = mysqli_query($conn,"select *from `messages` where  ( (sender_id = $sender_id and receiver_id = $advisor_id))  and  is_read = 0 ");
			$newChatCount = mysqli_num_rows($newChatQ);
			
			// get Last Message
			 $lastMessageQ = mysqli_query($conn,"select *from `messages` where ( (sender_id = $sender_id and receiver_id = $advisor_id) or (sender_id = $advisor_id and receiver_id = $sender_id) ) and delete_for_everyone = 0  order by id desc limit 1  ");
			 $lastMessageData = mysqli_fetch_assoc($lastMessageQ);
			 
			 $advData['newChatCount'] =$newChatCount;
			 $advData['last_date_time'] =$lastMessageData['date_time'];
			 
						$listArray[] = $advData;
						
			 
							   }
							   
							   
							   
							   
							   
							   function date_compare($a, $b)
								{
									$t1 = strtotime($a['last_date_time']);
									$t2 = strtotime($b['last_date_time']);
									return $t1 - $t2;
								}    
								
								ksort($listArray, 'date_compare');
															   
							   
							   
							   foreach($listArray as $listData)
							   {
								   
								   
								     $sender_id = $listData['id'];
								   
								   
								   // get unread count 
		    $newChatQ = mysqli_query($conn,"select *from `messages` where  ( (sender_id = $sender_id and receiver_id = $advisor_id))  and  is_read = 0 ");
			$newChatCount = mysqli_num_rows($newChatQ);
			
			// get Last Message
			 $lastMessageQ = mysqli_query($conn,"select *from `messages` where ( (sender_id = $sender_id and receiver_id = $advisor_id) or (sender_id = $advisor_id and receiver_id = $sender_id) ) and delete_for_everyone = 0  order by id desc limit 1  ");
			 $lastMessageData = mysqli_fetch_assoc($lastMessageQ);
								   
								   


								   ?>
							   
                                <!-- Message -->
                           <label href="#" style="cursor: pointer;" onclick="openChatWindow('<?php echo $sender_id; ?>','<?php echo $listData['name']; ?>')"  class="message-item" id='chat_user_<?php echo $sender_id; ?>' data-user-id='2'>
                                   
							
									
									
								   <span class="user-img"> 
									<img style="width:30px;" src="<?php if($listData['image']!='') { ?>../images/<?php echo $listData['image']; ?><?php } else { ?>https://icon-library.com/images/generic-person-icon/generic-person-icon-24.jpg<?php } ?>"  alt="user" class="rounded-circle"></span>
									
									
                                    <div class="mail-contnet">
									
							 <?php if($newChatCount > 0) { ?><span style="float:right;right:0"  class="badge bg-danger badge-notifications"><?php echo $newChatCount; ?></span><?php }  ?>
											
                                        <h5  style="font-size:14px !important;" class="message-title"><?php echo $listData['name']; ?></h5>
										 
										<?php if(isset($lastMessageData)) {
											
											
											if($lastMessageData['delete_for_everyone'] == '1')
											{
											echo "<br/><label style='color:grey;font-size:12px;'>Message Deleted</label>";
								
											} else {
								


											?>
										
										
										
										<?php if($lastMessageData['message']!=''){ ?><span class="mail-desc"><?php echo substr($lastMessageData['message'],0,50); if(strlen($lastMessageData['message']) > 50) { echo ".."; } ?></span><?php } ?>
										
										<?php if($lastMessageData['document']!='') { ?><br/><span class="mail-desc"><i class="fa fa-paperclip"></i> Attachment</label><?php } ?>
										
											<?php } ?>
										
										<span class="time"><?php  if(date('Y-m-d',strtotime($lastMessageData['date_time'])) == $today_date) {  echo date('h:i A',strtotime($lastMessageData['date_time'])); } else { echo date('d M y',strtotime($lastMessageData['date_time'])); } ?> </span>
										
										
										
										<?php } ?>
									 
									
										
                                         </div>
										
										
								
                                </label>
                                <!-- Message -->
								
							   <?php } ?>
                               
                            </div>
                        </li>
                    </ul>
     <?php } ?>           