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



 if(isset($_SESSION['loan_crm_username']) && isset($_SESSION['loan_crm_password']))
{
	
	
	 $account_holder_id = $_REQUEST['account_holder_id'];
	 $type = $_REQUEST['type'];
	 $page_start = $_REQUEST['page_start'];
	 
	 
	 
	 
	 //  and  (date_time >= now()-interval 1 month) 
	 
    $nonContactTQ = mysqli_query($conn,"select *from `updates` where account_holder_id = '$account_holder_id'  and  attempt_status = 'No Contact'   order by id desc   ");
	 
	 $total_non_contact = mysqli_num_rows($nonContactTQ);
				

     $updatesQ = mysqli_query($conn,"select *from `updates` where account_holder_id = '$account_holder_id'    order by id desc   ");
	 $total_update = mysqli_num_rows($updatesQ);
	 
     if($type == 'all')
	 {		 
	  $updatesQ = mysqli_query($conn,"select *from `updates` where account_holder_id = '$account_holder_id'    order by id desc  limit $page_start, 10  ");
	 }

	if($type == 'contact')
	{
		$updatesQ = mysqli_query($conn,"select *from `updates` where account_holder_id = '$account_holder_id' and  ( attempt_status = 'Contact P'  or attempt_status ='Contact N'   )   order by id desc limit  $page_start, 10  ");
	}
	
	if($type == 'non_contact')
	{
		$updatesQ = mysqli_query($conn,"select *from `updates` where account_holder_id = '$account_holder_id' and  attempt_status = 'No Contact'   order by id desc    limit $page_start, 10  ");
	}
	  
	  
	  
	  
	  $total_contact = $total_update -  $total_non_contact;
	 
	 
	 
	 ?>
	 
	 
	 <?php if(!isset($_GET['hidetotal'])) { ?>
	 <label>Total Updates:<?php echo $total_update; ?> | Contact: <a href="#" onclick="showAllUpdates(this,'<?php echo $account_holder_id; ?>','contact','1');" ><?php echo $total_contact; ?></a> |  Non Contact:   <a href="#" onclick="showAllUpdates(this,'<?php echo $account_holder_id; ?>','non_contact','1');" ><?php echo $total_non_contact; ?></a> </label>
	 <?php } ?>
	 
				<ul class="timeline timeline-dashed " style="height:600px;overflow: auto;    padding: 10px;">
		
		
					<?php
					
					
					$timeline_background = array('primary','success','info','warning');


				 	
					while($updatesData = mysqli_fetch_assoc($updatesQ))
					{
						
						$random_keys=rand(0,3);
						$bakground = $timeline_background[$random_keys];

					?>
					
					
          <li class="timeline-item timeline-item-<?php echo $bakground; ?> mb-4">
            <span class="timeline-indicator timeline-indicator-<?php echo $bakground; ?>" >
              <i class="bx bx-phone"></i>
            </span>
            <div class="timeline-event">
              <div class="timeline-header border-bottom mb-3">
                <h6 class="mb-0"><?php echo $updatesData['attempt_type']; ?> - <?php echo $updatesData['attempt_status']; ?></h6>
                <small class="text-muted"><?php echo  date('d-M',strtotime($updatesData['update_date'])); echo " ".$updatesData['update_time']; ?></small>
              </div>
              <div class="d-flex justify-content-between flex-wrap mb-2">
			   <div><h6 class="badge bg-label-primary"><?php echo $updatesData['mobile']; ?></h6><br>
               
                  <span class="badge bg-label-success"><?php echo $updatesData['disposition']; ?> </span> - <span><?php echo $updatesData['sub_disposition']; ?></span><br/>
				  
				   <?php if($updatesData['reason_for_default']!='') { ?>
                  <span>RFD: <?php echo $updatesData['reason_for_default']; ?></span><br/>
				   <?php } ?>
				  
				  <?php if($updatesData['amount']!='') { ?><span class="fw-semibold">Amount:</span> <span><?php echo   $updatesData['amount']; ?> - <?php echo   $updatesData['payment_mode']; ?></span><?php } ?>
                  
				   <?php if($updatesData['remark']!='') { ?>
					<br/>
				 <span>Remark: <?php echo $updatesData['remark']; ?></span><br/>
				   <?php } ?>
				  <span>Update by: <?php echo $updatesData['update_by']; ?></span><br/>
                </div>
                <div>
                  <?php if($updatesData['next_update_date']!='') { ?><span class="fw-semibold">Followup : <?php echo  date('d-M-y',strtotime($updatesData['next_update_date'])); ?> <?php echo date('h:i A',strtotime($updatesData['next_update_time'])); ?></span>
				  <?php } ?>
				  
				  
				   <?php if($updatesData['uniqueid']!='') {
					   
					   $uniqueid = $updatesData['uniqueid'];
					   
					    $cdrQ = mysqli_query($conn,"select *from `call_reponse` where uniqueid = '$uniqueid' ");
						if(mysqli_num_rows($cdrQ) > 0)
						{
							
							$cdrData = mysqli_fetch_assoc($cdrQ);


					   ?>
					<br/>
				 <span>Recording: <a href="<?php echo $cdrData['resource_url']; ?>" target="_blank">Play</a></span><br/>
						<?php } } ?>
				   
				   
                </div>
				
				 
				 
              </div>
               
            </div>
          </li>
		  
					<?php } ?>
		  
          </ul>
		  
		  
		  <a href="#" onclick="loadMoreUpdates(this,'<?php echo $account_holder_id; ?>','<?php echo $type; ?>','<?php echo $page_start + 10; ?>');" >Load More</a>
		  
	 
	 
	 <?php
	 
	
	
	
	
	
 }
	
	
	?>