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

	 if(isset($_SESSION['loan_crm_username']) && isset($_SESSION['loan_crm_password']))
	{
		
		
		
	   	 	$month = $_REQUEST['month'];
	   	 	  $sub_type = $_REQUEST['sub_type'];
		 
		 
		   $from_date =  $month.'-01';
		   $to_date = date("Y-m-t", strtotime($from_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);
				
				$total_day = 0;
				
				foreach ($period as $dt) 
				 {
					 
					 $total_day++;
							
				 }
				
				
				
	


	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>
           <?php
		  foreach ($period as $dt) 
		   {
			   if(strtotime($dt->format("Y-m-d")) < strtotime(date('Y-m-d')))
			   {
			   
			 ?>
							
			 <th>
			 <?php echo  $status_date = $dt->format("d M");  ?>
			 </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'];
			
			$start_time = $advisorData['start_time'];
			$end_time = $advisorData['end_time'];
			
		      
	  ?>
	  
	  <tr>
	  
	  <td><?php echo $advisorData['name']; ?></td>
	  <?php  foreach ($period as $dt) 
	      { 
		  if(strtotime($dt->format("Y-m-d")) < strtotime(date('Y-m-d')))
			   {
						
						
		 $status_date = $dt->format("Y-m-d");
		 $att_status = '';
		 
		    $login_time_in_sec = 0;
			$first_login_time = '';
			$last_lagout_time = '';
	   					  
			   $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'        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;
			  
			  
					}


					
							
							
			if($login_time_in_sec == 0 || $login_time_in_sec == '')
			{
				$att_status = 'AB';
			}
			else
			{
				
				 $att_status = 'PR';
				
				if(strtotime($first_login_time)  >= ( strtotime($start_time) + (15 * 60)) )
				{
					$att_status = 'LT';
				}
				
				if(strtotime($first_login_time)  >= ( strtotime($start_time) + (120 * 60)) )
				{
					$att_status = 'HF';
				}
				
				if(strtotime($first_login_time)  >= ( strtotime($start_time) + (120 * 60)) )
				{
					$att_status = 'HF';
				}
				
				if( (strtotime($end_time) -  strtotime($last_lagout_time) )  >= (120 * 60) )
				{
					$att_status = 'HF';
				}
				
				
				
			}
			
			
			// check holiday
			
			$holidayQ = mysqli_query($conn,"select *from `holidays` where holiday_date = '$status_date' ");
			if(mysqli_num_rows($holidayQ) > 0)
			{
				$att_status = 'HL';
			}
			
			// check if sunday
			if(date('D',strtotime($status_date)) == 'Sun')
			{
				$att_status = 'HL';
			}
			
							
							  
								  
						
		 ?>
						
	 <td><?php if($sub_type == 'login') { echo $first_login_time; } else  if($sub_type == 'logout') { echo $last_lagout_time; } else { echo $att_status; } ?></td>
	 
		  <?php } }  ?>
	  
	  </tr>
       
		<?php } ?>
        
      </tbody>
    </table>
		 
	</div>	 
		 
		 
		 
		 
		 <?php
		
		
		
		
	}
	
	
	?>
	
	 