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

	 if(isset($_SESSION['loan_crm_username']) && isset($_SESSION['loan_crm_password']))
	{
		
		
		
	   	$from_date = $_REQUEST['from_date'];
		$to_date = $_REQUEST['to_date'];
		
		
		
		
				$begin = new DateTime($from_date);
				$end = new DateTime(date('Y-m-d',strtotime($to_date." +1 day")));

				$interval = DateInterval::createFromDateString('1 day');
				$period = new DatePeriod($begin, $interval, $end);
				
				
	


	function secToHour($seconds)
	{
	  return sprintf('%02d:%02d:%02d', ($seconds/ 3600),($seconds/ 60 % 60), $seconds% 60);	

	}
	
	
	
	function getPercentage($total_time,$used_time)
	{
		
			$count1 = $used_time / $total_time;
		   $count2 = $count1 * 100;
		   $count = number_format($count2, 0);
		   
		   return $count.'%';  
		
	}
	
	
					
				
		
		
		 ?>
		 
		 
		 
		 <div class="table-responsive card-datatable text-nowrap">
		 
		 <table  id="myTable"   class="table table-bordered " style="zoom:90%">
      <thead class="table-dark">
        <tr>
          <th>Advisor</th>
          <th>ID</th>
          <th>Total Login Time</th>
		  
		  <?php
		  foreach ($period as $dt) 
						{
							?>
							
							<th>
							<?php echo  $status_date = $dt->format("d-M-y");  ?>
							</th>
							
							
							<?php
						}
						
		  ?>
          
		   
		  
        </tr>
      </thead>
      <tbody >
	  
	  <?php $advisorQ = mysqli_query($conn,"select *from `advisors` ");
	  
	  
	  
	   if($_SESSION['login_as'] == 'advisor')
		 {
			 $login_name = $_SESSION['login_name'];
			 $advisorQ = mysqli_query($conn,"select *from `advisors` where name='$login_name' "); 
		 }
		 
	  
	    while($advisorData = mysqli_fetch_assoc($advisorQ))
		{
			
			$advisor_id = $advisorData['id'];
			$username = $advisorData['username'];
			
		   $login_time_in_sec = 0;
			 
			 
			  
			  
			  
			   //  get Login Time
			   
			   
			      $loginTimeDateArray =  array();
			  
						foreach ($period as $dt) 
						{
							//echo "AAAAAAAAAAAAAAAAAAAA";
							//echo $dt->format("l Y-m-d H:i:s\n");
							
							  $status_date = $dt->format("Y-m-d");
								  
			   $loginStatusQ = mysqli_query($conn,"select *from `advisor_login_status` where advisor_id =$advisor_id and  login_status = 'logged_in' and status_date = '$status_date'   order by id asc limit 1  ");
			    if($firstLoginData = mysqli_fetch_assoc($loginStatusQ))
				 {
					 
					  $first_login_time = $firstLoginData['status_time'];
					 
			    
			  
			  
				//  $loginStatusQ = mysqli_query($conn,"select *from `advisor_login_status` where advisor_id =$advisor_id and  login_status = 'logged_out' and   status_date = '$status_date'  and  time(status_time) >  time('$first_login_time')     order by id desc limit 1  ");
				
				
				  $loginStatusQ = mysqli_query($conn,"select *from `advisor_login_status` where advisor_id =$advisor_id and  login_status = 'logged_out' and   status_date = '$status_date'    order by id desc limit 1  ");
				  
				  
				  
				  if($lastLogoutData = mysqli_fetch_assoc($loginStatusQ))
				  {
					  
					  $last_lagout_time = $lastLogoutData['status_time'];
					  
				  }
				  else
				  {
					  $last_lagout_time = $advisorData['end_time'];
				  }
				   
				 
				   
				   // diff in time
				   
				   $diff_in_sec = strtotime($last_lagout_time) -  strtotime($first_login_time);
				   
				   $login_time_in_sec = $login_time_in_sec + $diff_in_sec;
				   
				   
				  // $loginTimeDateArray[$status_date] = $login_time_in_sec;
				   $loginTimeDateArray[$status_date] = $diff_in_sec;
			  
			  
					}
					
					 
					 
					 
					 
					 
					 
					 
					 
				 }
				 
				 
				 
				 /******************  Get Talk time *********************/
				 
				 
				 
				 
				 
				 
				  
			  
			
			

	  ?>
	  
	  <tr>
	  
	  <td><?php echo $advisorData['name']; ?></td>
	  <td><?php echo $advisorData['id']; ?></td>
	  <td><?php if($login_time_in_sec > 0) {  echo secToHour($login_time_in_sec); }  ?></td>
	  
	  <?php  foreach ($period as $dt) 
						{ 
						
						
							  $status_date = $dt->format("Y-m-d");
							  
							
							  
								  
						
						?>
						
	 <td><?php  if(isset($loginTimeDateArray[$status_date])) { if($loginTimeDateArray[$status_date] > 0) {  echo secToHour($loginTimeDateArray[$status_date]); } else { echo "L"; } } else { echo "L"; } ?></td>
	 
	 <?php } ?>
	  
	  </tr>
       
		<?php } ?>
        
      </tbody>
    </table>
		 
	</div>	 
		 
		 
		 
		 
		 <?php
		
		
		
		
	}
	
	
	?>
	
	 