<?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($con, $_POST["query"]);
  $query ="SELECT E.EMP,E.PROJECT as PROJECT,E.CATEGORY as CATEGORY,E.PROCESS as PROCESS,RIGHT(E.START_TIME,8) AS START_TIME,RIGHT(E.END_TIME,8) AS END_TIME,LEFT(SEC_TO_TIME(TIMESTAMPDIFF(SECOND,E.START_TIME,E.END_TIME)),8)AS TD,LEFT(SEC_TO_TIME(MP.BENCHMARK),8) AS BENCHMARK ,IF(MP.BENCHMARK < TIMESTAMPDIFF(SECOND, E.START_TIME, E.END_TIME), 'NO', 'YES') AS FinalStatus,
    IF(MP.BENCHMARK < TIMESTAMPDIFF(SECOND, E.START_TIME, E.END_TIME), LEFT(SEC_TO_TIME((TIMESTAMPDIFF(SECOND, E.START_TIME, E.END_TIME)-MP.BENCHMARK)),8), LEFT(SEC_TO_TIME((TIMESTAMPDIFF(SECOND, E.START_TIME, E.END_TIME)-MP.BENCHMARK)),9)) AS TIMEEXCEED FROM EMPDATA_TBL E left JOIN master_process_copy MP ON E.PROJECT= MP.PROJECT AND E.CATEGORY= MP.CATEGORY  AND E.PROCESS= MP.PROCESS  WHERE EMP='$name'  AND left(START_TIME,10) = CURDATE() AND E.PROCESS  LIKE '%".$search."%' ORDER BY E.START_TIME ASC";
}
else
{
	$query = "SELECT E.EMP,E.PROJECT as PROJECT,E.CATEGORY as CATEGORY,E.PROCESS as PROCESS,RIGHT(E.START_TIME,8) AS START_TIME,RIGHT(E.END_TIME,8) AS END_TIME,LEFT(SEC_TO_TIME(TIMESTAMPDIFF(SECOND,E.START_TIME,E.END_TIME)),8)AS TD,LEFT(SEC_TO_TIME(MP.BENCHMARK),8) AS BENCHMARK ,IF(MP.BENCHMARK < TIMESTAMPDIFF(SECOND, E.START_TIME, E.END_TIME), 'NO', 'YES') AS FinalStatus,
    IF(MP.BENCHMARK < TIMESTAMPDIFF(SECOND, E.START_TIME, E.END_TIME), LEFT(SEC_TO_TIME((TIMESTAMPDIFF(SECOND, E.START_TIME, E.END_TIME)-MP.BENCHMARK)),8), LEFT(SEC_TO_TIME((TIMESTAMPDIFF(SECOND, E.START_TIME, E.END_TIME)-MP.BENCHMARK)),9)) AS TIMEEXCEED FROM EMPDATA_TBL E left JOIN master_process_copy MP ON E.PROJECT= MP.PROJECT AND E.CATEGORY= MP.CATEGORY  AND E.PROCESS= MP.PROCESS  WHERE EMP='$name'  AND left(START_TIME,10)=CURDATE() ORDER BY E.START_TIME DESC";
}
$result = mysqli_query($con, $query)  or die(mysqli_error($con));
$RES=mysqli_num_rows($result);
// echo $RES;
if($RES  >0)
{ 
  	$return .='
	<table  style="">
	<tr style="">
	<th style="padding:8px">Project</th>
	<th style="padding:8px">Category</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["PROJECT"].' </td> 
		<td> '.$row1["CATEGORY"].'</td> 
		<td> '.$row1["PROCESS"].'</td> 
		<td> '.$row1["START_TIME"].' </td> 
		<td>'.$row1["END_TIME"].' </td> 
		<td>'.$row1["BENCHMARK"].' </td>
		<td  >'.$row1["TD"].' </td> 
		<td style="background-color:#c3e6cb ">'.$row1['TIMEEXCEED']. '</td>';
 		}
		else{ $return.= '
			<td  style="">'.$row1["PROJECT"].' </td> 
			<td> '.$row1["CATEGORY"].'</td> 
			<td style="font-weight: bolder;color:#db1709"> '.$row1["PROCESS"].'</td> 
			<td> '.$row1["START_TIME"].' </td> 
		    <td>'.$row1["END_TIME"].' </td> 
			<td>'.$row1["BENCHMARK"].' </td>
			<td>'.$row1["TD"].' </td>
		    <td style="background-color:#f5c6cb;font-weight:bold;color:#8f1b1b;";>'.$row1['TIMEEXCEED']. '</td>';}
		$return.= '</tr> </table>' ;
	}
	 echo $return;
	}
else
{
 	
	echo 'No data found....';
}
?>