<?php 
include ('pdf_header_reverse.php');
//include ('includes/config/config.php');
//include ('includes/functions.php');

?>

<link href="includes/css-js/admin.css" rel="stylesheet" type="text/css" />
<body>
	<table width="100%">
		<tr>
			<div style="font-size: 12px">
				<td width="25%" align="left"> Run Date : <?php  echo date("d-M-Y"); ?></td>
				<td width="50%" align="center" style="font-size: 40px"><strong>Hygiene Report</strong></td>
				<td width="25%" align="left"> User : <?php  echo $username ?></td>
				<?php  if(isset($from_date) && isset($to_date)){?>
           <td width="25%" align="right"> Range : <?php  echo $from_date; ?> to <?php  echo $to_date ;?></td>
<?php  }?>
		</tr>
	</table>
	<table width="87%" border="1">
		<tr>
			<th width="2%" align="center">Sr</th>
			<th align="center" width="10%">Employee </th>
			<th align="center" width="30%">Hygiene Parameter</th>
			<th align="center" width="10%">Hygiene Current Value</th>
			<th align="center" width="10%">Hygiene Reference Range</th>
			<th align="center" width="10%">Checkup Date</th>
	
		</tr>
<?php 


$from_date = $_POST['startDate'];
$from_date = date('Y-m-d ', strtotime($from_date));  
//echo $from_date;
$hygiene_abnormal=$_POST['hygiene_abnormal'];
//echo $hygiene_abnormal;
if($hygiene_abnormal == "all_record")
{
	$hygiene_abnormal1 = "N";
	
}
else
{
	$hygiene_abnormal1 = "Y";
}
//echo  $hygiene_abnormal1;
$to_date = $_POST['endDate'];
$to_date = date('Y-m-d ', strtotime($to_date));  
//echo $to_date;
$emp_id=$_POST['empName'];
$hygiene_parameter = $_POST['hygiene_parameter'];
error_reporting(E_ERROR | E_PARSE);
$hygiene_parameter_ids = implode(',',$hygiene_parameter);
//echo $hygiene_parameter_ids;
//echo $hygiene_parameter_ids[2];
//echo $emp_id;
if($emp_id=="All"){
$sql_abnormal_hygiene = "select * from emp_hyg_check_master emplevel group by emp_id order by emplevel.emp_id DESC ";
//echo $sql_abnormal_hygiene;
}
else{
$sql_abnormal_hygiene = "select * from emp_hyg_check_master emplevel  where emplevel.emp_id='$emp_id' group by emp_id order by emplevel.emp_id DESC ";
//echo $sql_abnormal_hygiene;
}
$qtype = '';
$query = '';

$searchSql = ($qtype != '' && $query != '') ? "where upper($qtype) like upper('%".trim($query)."%')" : '';
// Get total count of records

 if($qtype == 'from_date') {
        
        $searchSql = " and date_format(date(from_date),'%Y-%m-%d')=str_to_date('$query','%d-%m-%Y') ";
    }
    elseif($qtype == 'to_date') {
        
        $searchSql = " and date_format(date(to_date),'%Y-%m-%d')=str_to_date('$query','%d-%m-%Y') ";
    }

    else if($qtype=='checkup_date_between'){
        $dates=explode("-",$query);
        
        //$qtype=" due_date between ";
        $searchSql=" and checkup_date between str_to_date('".trim($dates[0])."','%d/%m/%Y') and  str_to_date('".trim($dates[1])."','%d/%m/%Y') ";
    }
//	qwwecho	echo $sql_abnormal_hygiene;

$results_abnormal_hygiene = mysqli_query($conn,$sql_abnormal_hygiene); 
$count = 0;


while ($row_abnormal_hygiene = mysqli_fetch_array($results_abnormal_hygiene)) {
    $hygieneParameters = null;
    $hygiene_currentValue=null;
    $checkup_date = null;
    $hygieneParameterEnd="";
    $temp=1;
    $sql_emp_hyg_id = "select hyg_check_id,checkup_date from emp_hyg_check_master where emp_id='".$row_abnormal_hygiene['emp_id']."' AND checkup_date between '$from_date' AND  '$to_date'";
    $results_emp_hyg_id = mysqli_query($conn,$sql_emp_hyg_id);
// echo $sql_emp_hyg_id;
    $noOfRows= mysqli_num_rows($results_emp_hyg_id);
    while ($row_emp_hyg_id = mysqli_fetch_array($results_emp_hyg_id)) {
        $hyg_check_id = $row_emp_hyg_id['hyg_check_id'];
        if($noOfRows==$temp)
           $hygieneParameterEnd.="'$hyg_check_id'";
        else 
        	$hygieneParameterEnd.="'$hyg_check_id',";
        
          $temp++;
         //
		// $emp_min_range =$row_emp_hyg_id['emp_min_range'];
		// 	$emp_max_range =$row_emp_hyg_id['emp_max_range'];
	//$range=$emp_min_range."-".$emp_max_range;
	//echo $range;
     }
	

     if($hygiene_parameter_ids!="" && $hygiene_parameter_ids!=null)
     $hygieneParameter = "select * from emp_hyg_check_items where hyg_check_id in (".$hygieneParameterEnd.") and hyg_para_id in (".$hygiene_parameter_ids.")  and is_abnormal='$hygiene_abnormal1'";
    else 
     $hygieneParameter = "select * from emp_hyg_check_items where hyg_check_id in (".$hygieneParameterEnd.") and is_abnormal='$hygiene_abnormal1'";
     
    // echo $hygieneParameter;
	// echo  $row_abnormal_hygiene['emp_id']." ";
     $resultHygieneParameter = mysqli_query($conn,$hygieneParameter);
	// $emp_quy="select * from employee where id='".$row_abnormal_hygiene['emp_id']."'";
	// echo 	 $emp_quy;
	// $results_quy = mysqli_query($conn,$emp_quy);
	// $row_emp = mysqli_fetch_array($results_quy);
     	//echo $row_emp['fname'];
	 //	echo $row_emp['lname'];
   $employee_name = getTableFieldValue('employee', "CONCAT(fname, ' ', lname)", 'id', $row_abnormal_hygiene['emp_id']);
	// getTableFieldValue('hygiene','hygiene_parameter','hygiene_id',$row_work_area_hyg_parameter['hyg_parameter_id']);
//echo $employee_name1;
      $counter=1;
     $noOfRowsHygieneParamter= mysqli_num_rows($resultHygieneParameter);
     if($noOfRowsHygieneParamter>0){
     	?>
     	     <tr>
     	     <td rowspan="<?php  echo $noOfRowsHygieneParamter?>" align="center" ><?php  echo ++$count?></td>
     	      <td rowspan="<?php  echo $noOfRowsHygieneParamter?>"  align="center"><?php  echo $employee_name?></td><?php 
     	      
     	      
     	      
     	
     }
     
    
     while($rowOfhygieneParameter = mysqli_fetch_array($resultHygieneParameter)){
     	$checkup_date=getTableFieldValue('emp_hyg_check_master ','checkup_date','hyg_check_id',$rowOfhygieneParameter['hyg_check_id']);
     	$hygiene_id = $rowOfhygieneParameter['hyg_para_id'];
     	$sqlParameterName = "select hygiene_parameter,human_min_range,human_max_range from hygiene where hygiene_id ='$hygiene_id' ";
     	$resultParameterName = mysqli_query($conn,$sqlParameterName);
     	$rowParameterName = mysqli_fetch_array($resultParameterName);
     	// $hygieneParameters = $hygieneParameters.$rowParameterName['hygiene_parameter'].'<br>';
     	//  $hygiene_currentValue = $hygiene_currentValue.$rowOfhygieneParameter['hyg_param_val'].'<br>';
     	  $human_min_range =$rowParameterName['human_min_range'];
	      $human_max_range =$rowParameterName['human_max_range'];
           $range=$human_min_range."-".$human_max_range;
     	  if($counter>1){
     	  	?><tr>
     	  	  
     	 <?php  }
     	?>	
     	        			<td align="center"><?php  echo $rowParameterName['hygiene_parameter']?></td>
     	        			<td align="center"><?php  echo $rowOfhygieneParameter['hyg_param_val']?></td>
							<td align="center"><?php  echo $range?></td>
     	        			<td align="center"><?php  echo  date_format(date_create($checkup_date),"d-M-Y h:i:sa");?></td>
					
     	        		
     	        		</tr><?php 
     	        		$counter++;
     	        		
     }
	
 }

?>

</table>
<?php 
include('pdf_footer.php');
?>