<?php
// ini_set('max_execution_time','3');
session_start();

include('config.php');
 $return = '';
 $name = $_SESSION['name'];
if(isset($_POST["query"]))
{
   $search = mysqli_real_escape_string($conn2, $_POST["query"]);
   $query="SELECT L.EMPNO,EM.EMPNAME,B.IMAGENAME as IMAGENAME,L.PROCESS as PROCESS ,L.PASS as PASS, RIGHT(L.startTime,8) as startTime, RIGHT(L.EndTime,8)AS EndTime, 
   LEFT(SEC_TO_TIME(TIMESTAMPDIFF(SECOND, L.startTime, L.EndTime)),8) as TDif,LEFT(SEC_TO_TIME(BM.BTD),8) as BTD , IF(BM.BTD < TIMESTAMPDIFF(SECOND, L.startTime, L.EndTime), 'NO', 'YES') AS FinalStatus ,IF(BM.BTD < TIMESTAMPDIFF(SECOND,L.startTime, L.EndTime), LEFT(SEC_TO_TIME((TIMESTAMPDIFF(SECOND, L.startTime, L.EndTime)-BM.BTD)),8), LEFT(SEC_TO_TIME('0'),8)) AS TIMEEXCEED from LOGIN_FINAL  L left JOIN BATCHES B ON B.BATCHNO = L.BATCHNO left JOIN MASTER_BENCHMARK BM ON BM.PROCESS = CONCAT(L.PROCESS,L.PASS) left JOIN empmaster EM ON EM.EMPNO = L.EMPNO Where left(starttime,10)=CURDATE() AND L.EMPNO = '$name' AND  L.PROCESS  LIKE '%".$search."%' ORDER BY L.startTime ASC";
}
else
{
$query="SELECT L.EMPNO,EM.EMPNAME,B.IMAGENAME as IMAGENAME,L.PROCESS as PROCESS ,L.PASS as PASS, RIGHT(L.startTime,8) as startTime, RIGHT(L.EndTime,8)AS EndTime, 
LEFT(SEC_TO_TIME(TIMESTAMPDIFF(SECOND, L.startTime, L.EndTime)),8) as TDif,LEFT(SEC_TO_TIME(BM.BTD),8) as BTD , IF(BM.BTD < TIMESTAMPDIFF(SECOND, L.startTime, L.EndTime), 'NO', 'YES') AS FinalStatus ,IF(BM.BTD < TIMESTAMPDIFF(SECOND,L.startTime, L.EndTime), LEFT(SEC_TO_TIME((TIMESTAMPDIFF(SECOND, L.startTime, L.EndTime)-BM.BTD)),8), LEFT(SEC_TO_TIME('0'),8)) AS TIMEEXCEED from LOGIN_FINAL  L left JOIN BATCHES B ON B.BATCHNO = L.BATCHNO left JOIN MASTER_BENCHMARK BM ON BM.PROCESS = CONCAT(L.PROCESS,L.PASS) left JOIN empmaster EM ON EM.EMPNO = L.EMPNO Where left(starttime,10)=CURDATE() AND L.EMPNO = '$name' ORDER BY L.startTime desc";
}
$result = mysqli_query($conn2, $query)  or die(mysqli_error($conn2));
$RES=mysqli_num_rows($result);
// echo $RES;
if($RES  >0)
{ 
	
  	$return .='
	<table  style="">
	<tr style="">
	<th style="padding:8px">Emp Name</th>
	<th style="padding:8px">Imagename</th>
	<th style="padding:8px">Process</th>
	<th style="padding:8px">StartTime</th>
	<th style="padding:8px">EndTime</th>
	<th style="padding:8px">Benchmark</th>
	<th style="padding:8px">TimeTaken</th>
	<th style="padding:8px">TimeExceed</th>
	</tr>';
	while($row1 = mysqli_fetch_array($result))
	{
		$F_S =  $row1['FinalStatus'];
		if($F_S == "YES")
		{
		$return .= '
		<tr">
		<td  style="">'.$row1["EMPNAME"].' </td> 
		<td> '.$row1["IMAGENAME"].'</td> 
		<td> '.$row1["PROCESS"].'</td> 
		<td> '.$row1["startTime"].'</td> 
		<td> '.$row1["EndTime"].' </td> 
		<td> '.$row1["BTD"].' </td> 
		<td> '.$row1["TDif"].'</td> 
		<td style="background-color:#c3e6cb ">'.$row1['TIMEEXCEED']. '</td>';
 
		}
		else{ 
	    $return.= '
		<td  style="">'.$row1["EMPNAME"].' </td> 
		<td style="font-weight: bolder;color:#db1709"> '.$row1["IMAGENAME"].'</td> 
		<td style="font-weight: bolder;color:#db1709"> '.$row1["PROCESS"].'</td> 
		<td> '.$row1["startTime"].' </td> 
		<td>'.$row1["EndTime"].' </td> 
		<td> '.$row1["BTD"].' </td> 
		<td  >'.$row1["TDif"].' </td>
        <td style="background-color:#f5c6cb;font-weight:bold;color:#8f1b1b;";>'.$row1['TIMEEXCEED']. '</td>';
	}
		$return.= '</tr> </table>' ;

	}
	 echo $return;
	}
else
{
	// echo 'No results containing all your search terms were found....';
	echo 'No data found....';
}
?>

