<?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'];
	
	 
	 
	 
	 
     $today_date = date('Y-m-d');
    
	
	   $sender_id = $_REQUEST['advisor_id'];
	   
	     $advisorQ = mysqli_query($conn,"select *  from `advisors` where id=$sender_id  ");
							   
		 $advisorData = mysqli_fetch_assoc($advisorQ);
							 

      
	 $messageQ  = 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 asc ");
	
	
?>

<!--chat Row -->
                                <ul class="chat-list" id="chat_list"  style="height:calc(100vh - 280px); overflow-y:scroll; ">
								
								 
								
								<?php 
								
								while($messageData = mysqli_fetch_assoc($messageQ))
									{
										
										
 
 
									?>
                                    <!--chat Row -->
                                    <li class="<?php if($messageData['sender_id'] == $advisor_id) { echo "odd"; }  ?> chat-item">
									  
									  <div class="chat-content  ">
										 
										<?php if($messageData['sender_id'] == $sender_id) { ?>
                                            <h6 class="font-medium"><?php echo $advisorData['name']; ?></h6>
										<?php } ?>	
											
                                            <div  data-bs-toggle="dropdown" aria-expanded="false" style="<?php if($messageData['delete_for_everyone'] == '1') {   ?>background: #d6d6d6 !important; <?php } ?>" class="box <?php if($messageData['sender_id'] ==  $sender_id) { echo "bg-light-info"; } else { echo "bg-light-inverse"; } ?>  dropdown-toggle ">
											
											 
											<!----------- Delete Dropdown --------------->
											 
											<ul class="dropdown-menu">
											<li><a onclick="deleteMessage('<?php echo $messageData['id']; ?>','delete_for_me');" class="dropdown-item">Delete For Me</a></li>
											<?php  if($messageData['sender_id'] == $advisor_id) {?>
											<li><a  onclick="deleteMessage('<?php echo $messageData['id']; ?>','delete_for_everyone');" class="dropdown-item">Delete For Everyone</a></li>
											<?php } ?>
											
											 <?php if($messageData['document']!='') {
					  
												$document = $messageData['document']; ?>

											<li><a   class="dropdown-item"><a target="_BLANK" href="../images/<?php echo $document; ?>">Download</a></a></li>
											 <?php } ?>
										
											
											<li><a onclick="copyContent('message_content_<?php echo $messageData['id']; ?>');" class="dropdown-item">Copy</a></li>
											 </ul>
											  
										<!-------------------------------------------------->

											
											
										<?php  if($messageData['delete_for_everyone'] == '1') { echo "Message Deleted"; } else { ?>	
											
											
											
												
	 <?php if($messageData['document']!='') {
					  
	    $document = $messageData['document'];

					  ?>
    
	 <?php if(strpos($document,'.png') != false || strpos($document,'.jpg') != false || strpos($document,'.jpeg') != false || strpos($document,'.gif') != false) { ?>
	 
	<a target="_BLANK" href="../images/<?php echo $document; ?>"><img style="width:200px;"  src="../images/<?php echo $document; ?>" alt="photo" class="imaged w160"></a>
	
	  <?php }
 
    else if(strpos($document,'.mp3') != false || strpos($document,'.wav') != false || strpos($document,'.mpeg') != false || strpos($document,'.aac') != false) {
		
		?>
	<audio style="width: 250px;" controls>
   <source src="../images/<?php echo $document; ?>" type="audio/mpeg">
   </audio>	
		 
	  <?php
		
	}

   else if(strpos($document,'.mp4') != false || strpos($document,'.3gp') != false || strpos($document,'.mov') != false || strpos($document,'.avi') != false || strpos($document,'.mkv') != false || strpos($document,'.flv') != false) {
		
		?>
	<video  height="200" controls>
	 <source src="../images/<?php echo $document; ?>" type="video/mp4">
	 </video>
	  <?php
		
	}


	else { ?><br/>
	
	 <a target="_BLANK" href="../images/<?php echo $document; ?>"> <span   src="../images/<?php echo $document; ?>"  style="background:white;color:grey;padding:10px;border-radius:20px;"><i class="fa fa-download"></i> Document</span></a>
	 <Br/>
    	 <?php } ?>
		
   <br/>		
											
	 <?php } ?>										
											
											
											
											
											
											
											
											<span id="message_content_<?php echo $messageData['id']; ?>"><?php echo $messageData['message']; ?></span>
										

										<?php } ?>										
											</div>
                                        </div>
                                        <div class="chat-time"><?php if(date('Y-m-d',strtotime($messageData['date_time'])) == $today_date) { echo date('h:i A',strtotime($messageData['date_time'])); } else { echo date('d M h:i A',strtotime($messageData['date_time'])); } ?></div>
                                    </li>
                                   
								   
								   <?php } ?>
									
									
									 
									
                                   
                                </ul>
      
 <?php } ?>	  