<?php
 session_start();
 error_reporting(E_ERROR | E_PARSE);
 error_reporting(0);
//  mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Emp_Prod_Benchmark</title>
    <link rel="shortcut icon" href="images/Files_Download.png" type="image/x-icon"/>
</head>
<style>
    .split.bor{
            margin-left:5%;
            transition: 1s; 
             /* margin-right:;  */
            /* margin-top:-15px; */ 
        }
    #nav-toggle:checked ~ .split.bor
    {
        margin-left:20%;
        transition: 1s;
        /* margin-right:15%; */
        
    }
        /* table{
    position:absolute; */
    
 

</style>
<body>
    <?php
    include "Nav.php";
    include "Connection.php"; 
    ?>

<?php 
    date_default_timezone_set('Asia/Kolkata');
    $date =  date('Y-m-d h:i:s');
?>
<br><br><br>
<div class="container split bor">
    <div class="container">
        <div class="row">
            <div class="col-md-12"><br>
                <form  action="" method = "POST" enctype="multipart/form-data">
                    
                        <div class="form-control">
                            <label for="Project" class=""><b>Select Project :</b></label>
                            <select id="Project" name="Project" class="" placeholder="Select-Project" value="<?php ?>">
                            <option value="" ></option>
                            <?php
                            $_SESSION['pro'] = $proj;
                            $q = mysqli_query($con,"SELECT distinct Project FROM epb_tbl");
                            while($q_row= mysqli_fetch_array($q)) {
                            ?>
                            <option>
                            <?php echo $project = $q_row['Project'];?></option>
                            <?php
                            
                            }
                            ?>
                            </select>
                                <label class=" control-label"><b>From:</b></label>
                                <input type="date" name="dateFrom" value="<?php echo date('Y-m-d');?>" required >
                                <label class="control-label"><b>To:</b></label>
                                <input type="date" name="dateTo" value="<?php echo date('Y-m-d');?>" required>
                                <label class="control-label"><b>Emp Name :</b></label>
                                <input type="text" name="EmpName" value="">
                                <button class="btn btn-success btn-sm" type="submit" name="search" value="Search" style="border-radius:15px;">Search</button>
                     </div>
                </form>

                
                <!-- <form method='POST' action='Export_report.php'>      
<input type='submit' value='Export File ' name='Export' class="btn btn-success">
</form>  -->
               
            </div>
        </div>
    </div>
     
    <?php
  $proj = $_SESSION['proj']; 
  if(isset($_POST['search'])){
    $From_date     = $_POST['dateFrom'];
    $To_date       = $_POST['dateTo'];
    $EmpName       = $_POST['EmpName'];
    $Project       = $_POST['Project'];
    $up = "select * from epb_tbl where Project='$Project'" ;
    $Query = mysqli_query($con, $up);
    if(!$Query){
             echo("Failed");
      }else{
        while($r = mysqli_fetch_array($Query)){ 
            $D_B = $r['Database'];
            $S_I = $r['Server_ip'];
            $U_N = $r['Username'];
            $P_W = $r['Password'];
          //echo("Success");       
        } 
    }
  }
?>
<br><br>
<table class="table table-bordered table-sm ">
    <thead >
        <tr>
            <th>Emp Name</th>  
            <th>Imagename</th>
            <th>Process</th>
            <th>StartTime</th>
            <th>EndTime</th>
            <th>BenchMark</th>
            <th>TimeTaken</th>
            <th>TimeExceed</th>
            <th>Status</th>            
        </tr>
    </thead>
<tbody>
<?php
$server= $S_I;
$user= $U_N;
$password= $P_W;
$db= $D_B ;
$connect=mysqli_connect($server,$user,$password,$db);

if($connect->connect_error){
die("Connection Failed");
}
else{
// echo $EmpName;
}

            ?><?php
            if($EmpName == "")
            {
            $up ="SELECT L.EMPNO,EM.EMPNAME,B.IMAGENAME as IMAGENAME,L.PROCESS as PROCESS ,L.PASS as PASS, L.startTime as startTime, L.EndTime 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) between '$From_date' and '$To_date' ORDER BY L.startTime ,L.EndTime";
            // $up = "SELECT L.EMPNO,EM.EMPNAME,B.IMAGENAME as IMAGENAME,L.PROCESS as PROCESS ,L.PASS as PASS, L.startTime as startTime, L.EndTime EndTime, TIMESTAMPDIFF(SECOND, L.startTime, L.EndTime) as TDif,BM.BTD as BTD , IF(BM.BTD < TIMESTAMPDIFF(SECOND, L.startTime, L.EndTime), 'NO', 'YES') AS FinalStatus 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) between '$From_date' and '$To_date' ORDER BY L.startTime ,L.EndTime";    
            } 
            else{

            $up ="SELECT L.EMPNO,EM.EMPNAME,B.IMAGENAME as IMAGENAME,L.PROCESS as PROCESS ,L.PASS as PASS, L.startTime as startTime, L.EndTime 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) between '$From_date' and '$To_date' AND L.EMPNO = '$EmpName' ORDER BY L.startTime ,L.EndTime";
            // $up = "SELECT L.EMPNO,EM.EMPNAME,B.IMAGENAME as IMAGENAME,L.PROCESS as PROCESS ,L.PASS as PASS, L.startTime as startTime, L.EndTime EndTime, TIMESTAMPDIFF(SECOND, L.startTime, L.EndTime) as TDif,BM.BTD as BTD , IF(BM.BTD < TIMESTAMPDIFF(SECOND, L.startTime, L.EndTime), 'NO', 'YES') AS FinalStatus 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) between '$From_date' and '$To_date' AND L.EMPNO = '$EmpName' ORDER BY L.startTime ,L.EndTime";
            }
            $Query = mysqli_query($connect, $up);
            while ($r = mysqli_fetch_array($Query)){
                $F_S =  $r['FinalStatus'];
            ?>
            <tr>
            <td> <?php echo $r['EMPNAME'] ;?> </td>  
            <td> <?php echo $r['IMAGENAME'] ;?> </td> 
            <td> <?php echo $r['PROCESS'] ;?> </td> 
            <td> <?php echo $r['startTime'] ;?> </td> 
            <td> <?php echo $r['EndTime'] ;?> </td> 
            <td> <?php echo $r['BTD'];?> </td> 
            <td> <?php echo $r['TDif'];?> </td> 
            <td> <?php echo $r['TIMEEXCEED'];?> </td> 
            <?php if($F_S == "YES") 
            {
            ?>
            <td class="table-success"><?php echo $F_S?></td>

            <?php }else
            {
            ?>
            <td class="table-danger"><?php echo $F_S;?></td>
            <?php }?>
            </tr>
            <?php
            }
          
            // echo "Succeed";
            ?>

</tbody>
</table> 
</div>
</body>
</html>
