<?php 

include('../dbconnect.php');
include('../phps.php');

ob_start();
session_start();
include('uppernav.php');
if( !isset($_SESSION['user']) ) {
 header("Location: ../index.php");
 exit;
}
 $date = date('m/d/Y');
$dateval = date('Y-m-d', strtotime($date));


 if(isset($_POST['utilfilter'])){

     $teams= phps($_POST['team']);
      $date = phps($_POST['date']);
       $date = date('m/d/Y', strtotime($date));
       $dateval = date('Y-m-d', strtotime($date));


    if($teams== 'All'){



        $emplist = "SELECT * FROM empinfo JOIN empdetails ON empinfo.empid = empdetails.detailid  WHERE status='Active'";
        $sql = "SELECT * FROM `tbl_utilization` JOIN empinfo ON tbl_utilization.empid = empinfo.empid  JOIN empdetails ON tbl_utilization.empid = empdetails.empid WHERE tbl_utilization.date = '$date' GROUP BY tbl_utilization.empid ";
       $emplist1 = "SELECT *,tbl_login.date AS 'utildate' FROM tbl_login JOIN empdetails ON tbl_login.empid = empdetails.detailid JOIN empinfo ON  tbl_login.empid = empinfo.empid WHERE tbl_login.date ='$date' ";
       $sql1 = "SELECT * FROM `tbl_utilization` JOIN empinfo ON tbl_utilization.empid = empinfo.empid  JOIN empdetails ON tbl_utilization.empid = empdetails.empid WHERE tbl_utilization.date = '$date' GROUP BY tbl_utilization.empid ";



    }
    else{

        $emplist = "SELECT * FROM empinfo JOIN empdetails ON empinfo.empid = empdetails.detailid WHERE empdetails.team = '$teams'  AND status='Active'";
        $sql = "SELECT * FROM `tbl_utilization` JOIN empinfo ON tbl_utilization.empid = empinfo.empid  JOIN empdetails ON tbl_utilization.empid = empdetails.empid WHERE tbl_utilization.date = '$date' AND team='$teams' GROUP BY tbl_utilization.empid ";

       $emplist1 = "SELECT *,tbl_login.date AS 'utildate' FROM tbl_login JOIN empdetails ON tbl_login.empid = empdetails.detailid JOIN empinfo ON  tbl_login.empid = empinfo.empid WHERE tbl_login.date ='$date' AND team ='$teams'  ";
       $sql1 = "SELECT * FROM `tbl_utilization` JOIN empinfo ON tbl_utilization.empid = empinfo.empid  JOIN empdetails ON tbl_utilization.empid = empdetails.empid WHERE tbl_utilization.date = '$date' AND team='$teams' GROUP BY tbl_utilization.empid ";

              
    }



 }else{

    $emplist = "SELECT * FROM empinfo JOIN empdetails ON empinfo.empid = empdetails.detailid WHERE supervisor='$accountname'  AND status='Active'";
    $sql = "SELECT * FROM `tbl_utilization` JOIN empinfo ON tbl_utilization.empid = empinfo.empid  JOIN empdetails ON tbl_utilization.empid = empdetails.empid WHERE tbl_utilization.date = '$date' AND supervisor='$accountname'  GROUP BY tbl_utilization.empid ";
 
    $emplist1 = "SELECT *,tbl_login.date AS 'utildate' FROM tbl_login JOIN empdetails ON tbl_login.empid = empdetails.detailid JOIN empinfo ON  tbl_login.empid = empinfo.empid WHERE tbl_login.date ='$date' AND supervisor='$accountname'  ";
  $sql1 = "SELECT * FROM `tbl_utilization` JOIN empinfo ON tbl_utilization.empid = empinfo.empid  JOIN empdetails ON tbl_utilization.empid = empdetails.empid WHERE tbl_utilization.date = '$date' AND supervisor='$accountname' GROUP BY tbl_utilization.empid ";    


 }

?>
<!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, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>Super Admin | Utilization</title>

    <!-- Bootstrap core CSS-->
    <link href="../vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom fonts for this template-->
    <link href="../vendor/fontawesome-free/css/all.min.css" rel="stylesheet" type="text/css">

    <!-- Page level plugin CSS-->
    <link href="../vendor/datatables/dataTables.bootstrap4.css" rel="stylesheet">

    <!-- Custom styles for this template-->
        <link href="../css/sb-admin.css" rel="stylesheet">
    <link href="../css/style.css" rel="stylesheet">
    <link rel="icon" href="../images/logo.jpg" type="image/gif" sizes="16x16">

  </head>

  <body id="page-top">
	<div id="preloader">
   		<img src="../images/loaderS.gif" alt="processing..." height="150px" width="200px"/>
	</div>

    <?php ?>



      <!-- Sidebar -->
      
      <?php 
       $admintools = "active";
       $util = "active";
      include('sidebar.php');
      ?>
      <div id="content-wrapper">

        <div class="container-fluid">

          <!-- Breadcrumbs-->
          <ol class="breadcrumb">
   
            <li class="breadcrumb-item active">Utilization</li>
          </ol>


        <nav>


        <div class="col-lg-12 p-2 form-inline" >
            <form method="POST">
                <button type="submit" class="btn btn-primary" name="utilfilter"><i class="fa fa-filter"></i> Filter</button> &nbsp
                <select name="team" id="team" class="form-control" >
            
                <option value="All" selected>All</option>
                    <?php 
                        $teamsql = "SELECT team FROM empdetails JOIN tbl_utilization ON tbl_utilization.empid =  empdetails.detailid  GROUP BY team";
                        $teamre = mysqli_query($conn, $teamsql);
                        while($teamrow = mysqli_fetch_assoc($teamre)){
                            if($teamrow['team'] == $teams){

                                echo '<option selected>'.$teamrow['team'].'</option>';

                            }else{

                                echo '<option>'.$teamrow['team'].'</option>';

                            }
                            
                        }
                    ?>
                </select> &nbsp

                <input type="date" class="form-control" name="date" value="<?php echo $dateval?>">
            </form>

      
         
   
        

        </div>
        <div class="nav nav-tabs" id="nav-tab" role="tablist">
            <a class="nav-item nav-link active" id="nav-home-tab" data-toggle="tab" href="#nav-home" role="tab" aria-controls="nav-home" aria-selected="true">Attendance</a>
            <a class="nav-item nav-link" id="nav-profile-tab" data-toggle="tab" href="#nav-profile" role="tab" aria-controls="nav-profile" aria-selected="false">Utilization</a>

        </div>
        </nav>
        <div class="tab-content" id="nav-tabContent">
            <div class="tab-pane fade show active" id="nav-home" role="tabpanel" aria-labelledby="nav-home-tab">
                <div class="row p-3">
                    <div class="col-lg-12"><h5>Employee Attendance</h5></div>
                    <div class="col-lg-5 my-custom-scrollbar my-custom-scrollbar-primary tableFixHead " id="test_scroll" >
                                    
                                <?php 
                               
                                $empres = mysqli_query($conn, $emplist);

                                ?>
                                <table class="table table-sm" >
                                    <thead class="thead-dark">
                                        <th>Employee ID</th>
                                        <th>Name</th>
                                        <th>Schedule </th>
                                        <th>Login </th>
                                        <th>Late </th>
                                        
                                        <!--                             <th>Current </th>
                                        <th> Elapsed Time </th> -->
                                    </thead>
                                    <tbody>
                                        <?php 
                                    $date4sched =date('Y-m-d', strtotime($date));
                                    $empres = mysqli_query($conn, $emplist);
                                    if(mysqli_num_rows($empres) > 0) {


                                        while($emprow = mysqli_fetch_assoc($empres)){

                                            $employee = $emprow['empid'];


                                            echo'<tr><td>'.$emprow['empid'].'</td>';
                                            echo'<td>'.$emprow['firstname'].' '.$emprow['surname'].'</td>';


                                            $schedsql= "SELECT * FROM schedule WHERE empid='$employee' AND date= '$date4sched'";

                                            $schedres = mysqli_query($conn, $schedsql);
                                            $schedrow = mysqli_fetch_assoc($schedres);

                                            $schedtime = $schedrow['starttime'];

                                            if($schedtime == 'OFF' || $schedtime == 'HOLIDAY OFF'){

                                              if($schedtime == 'OFF'){
                                                echo'<td>OFF</td>';
                                                echo'<td>---</td>';
                                                echo'<td>0</td>';
                                              }else{
                                                echo'<td>HOLIDAY OFF</td>';
                                                echo'<td>---</td>';
                                                echo'<td>0</td>';
                                              }


                                            }else if ($schedrow['idl'] != ''){



                                               $idl = $schedrow['idl'];
                                               $today = date('Y-m-d', strtotime($date));

                                               


                                                    
                                                    $leavequery = "SELECT * FROM `schedule` JOIN `leave` ON schedule.idl = `leave`.idl  WHERE schedule.idl='$idl' AND `leave`.status='3' AND schedule.date='$today' ";
                                                    $leaveres = mysqli_query($conn, $leavequery);
                                                    $leaverow = mysqli_fetch_assoc($leaveres);
                                                    $duration = $leaverow['duration'];
      
                                                    $dur = substr($duration, -2);
      
                                                    if (mysqli_num_rows($leaveres) > 0)
                                                    {
                                                          if ($leaverow['leaveduration'] == '0.5')
                                                          {
      
                              
                                      
                                                              echo '<td class="text-danger">  '.$leaverow['name'] .' Halfday </td>';
                                                              echo '<td> --- </td>';
                                                              echo '<td> 0 </td>';
                                                              
                                                          }else{
      

                         
                                                              echo '<td class="text-danger">'. $leaverow['name'] .'</td>';
                                                              echo '<td> --- </td>';
                                                              echo '<td> 0 </td>';
                                                               
                                                          }
      
      
                                                    }





                                            } else{


                                            ///SCHED MANILA TO PST CONVERSION
                                            $pstschedtime = new DateTime($date.' '.$schedtime, new DateTimeZone('Asia/Manila'));

                                            $pstschedtime->setTimezone(new DateTimeZone('America/Los_Angeles'));
                                            $pstschedST= $pstschedtime->format('h:i A');



                                   
                               

                                              $loginsql= "SELECT * FROM tbl_login WHERE empid='$employee' AND date= '$date'";
                                            $loginres = mysqli_query($conn, $loginsql);
                                            $loginrow = mysqli_fetch_assoc($loginres);
                                            $logtime = $loginrow['timein'];

                                            $loginTIME = $loginrow['timein'];
                                            $loginTIME = new DateTime($loginTIME);
                                            $scheduledTIME = $pstschedST;
                                            $scheduledTIME = new DateTime($scheduledTIME);



                                            echo'<td>'.$pstschedST.'</td>';
 
                                            if(mysqli_num_rows($loginres) > 0){
 
                                             


                                             if($loginTIME > $scheduledTIME){

                                             $sched = $pstschedtime->format('H:i');
                                                $attend = date('H:i', strtotime($logtime));

                                                 
                                                $schedule = new DateTime($sched);
                                                $attendance = new DateTime($attend);
                                                $elapselate = $schedule->diff($attendance);








                                                echo'<td  class="text-danger">'.$loginrow['timein'].'</td>';
                                                echo'<td  class="text-danger">'.$elapselate->format('%H:%I').' </td>';
                                             }else{

                                                echo'<td>'.$loginrow['timein'].'</td>';
                                                echo'<td> 0 </td>';
                                             }
                                        
 
 
 
                                            }else{
                                                $today = date('Y-m-d');
                                                $tday = new DateTime($today);
                                                $getdate =  new DateTime($date);
                                              

                                                if($tday < $getdate){
                                                    echo'<td class="text-danger"> ABSENT </td>';
                                                    echo'<td> 0 </td>';


                                                }else{

                                                    echo'<td> --- </td>';
                                                    echo'<td> --- </td>';

                                                }


 
                                            
                                                 
                                             }
 
 
 
                                            }



                                            }




                                    }else{

                                        echo'<td> No data found</td>';
                                    }


                                            



                                      
                                            



                                           












                                            
                                        
                                        ?>
                                    </tbody>
                                </table>
                    </div>
                    <div class="col-lg-7 my-custom-scrollbar my-custom-scrollbar-primary" >
                        
                            
                        <?php 

                                





                    
                            //$date = '03/04/2019';\


                          






                            $result = $conn->query($sql);

                            if ($result->num_rows > 0) {

                                while($row = $result->fetch_assoc()) { 
                                    $emp = $row['empid'];

                                    $login = "SELECT * FROM `tbl_login` WHERE empid= '$emp' AND date = '$date'";
                                    $loginres = $conn->query($login);
                                    $loginrow = $loginres->fetch_assoc();

                                    
                                    $logoff = "SELECT * FROM `tbl_login` WHERE empid= '$emp' AND date = '$date'";
                                    $logoffres = $conn->query($logoff);
                                    $logoffrow = $logoffres->fetch_assoc();




                                    $curact= "SELECT * FROM `tbl_utilization` WHERE empid= '$emp' AND date = '$date' ORDER BY id DESC  LIMIT 1";
                                    $actres = $conn->query($curact);
                                    $currentact = $actres->fetch_assoc();



                                    $prodsql = "SELECT SUM(elapsedtime) AS 'prod'  FROM `tbl_utilization` WHERE empid= '$emp' AND date = '$date' AND type='Prod'";
                                    $prodresult = $conn->query($prodsql);
                                    $prodrow = $prodresult->fetch_assoc();

                                
                                    

                                    $adminsql = "SELECT SUM(elapsedtime) AS 'admin'  FROM `tbl_utilization` WHERE empid= '$emp' AND date = '$date' AND type='Admin'";
                                    $adminresult = $conn->query($adminsql);
                                    $adminrow = $adminresult->fetch_assoc();

                                    $nonprod = "SELECT SUM(elapsedtime) AS 'nonprod'  FROM `tbl_utilization` WHERE empid= '$emp' AND date = '$date' AND type='Non-Prod'";
                                    $nonprodresult = $conn->query($nonprod);
                                    $nonprodrow = $nonprodresult->fetch_assoc();
                                    

                                    $break = "SELECT SUM(elapsedtime) AS 'break'  FROM `tbl_utilization` WHERE empid= '$emp' AND date = '$date' AND type='Break'";
                                    $breakres = $conn->query($break);
                                    $breakrow = $breakres->fetch_assoc();

                                    $break1 = "SELECT SUM(elapsedtime) AS 'break1'  FROM `tbl_utilization` WHERE empid= '$emp' AND date = '$date' AND activity='1st Break'";
                                    $break1res = $conn->query($break1);
                                    $break1row = $break1res->fetch_assoc();

                                    $break2 = "SELECT SUM(elapsedtime) AS 'break2'  FROM `tbl_utilization` WHERE empid= '$emp' AND date = '$date' AND activity='2nd Break'";
                                    $break2res = $conn->query($break2);
                                    $break2row = $break2res->fetch_assoc();

                                    
                                    $lunchbreak = "SELECT SUM(elapsedtime) AS 'lunch'  FROM `tbl_utilization` WHERE empid= '$emp' AND date = '$date' AND activity='Lunch Break'";
                                    $lunchbreakres = $conn->query($lunchbreak);
                                    $lunchbreakrow = $lunchbreakres->fetch_assoc();


                                    $toilet = "SELECT SUM(elapsedtime) AS 'toilet'  FROM `tbl_utilization` WHERE empid= '$emp' AND date = '$date' AND activity='Toilet Break'";
                                    $toiletres = $conn->query($toilet);
                                    $toiletrow = $toiletres->fetch_assoc();

                                    $total = "SELECT SUM(elapsedtime) AS 'totaltime'  FROM `tbl_utilization` WHERE empid= '$emp' AND date = '$date' ";
                                    $totalres = $conn->query($total);
                                    $totalrow = $totalres->fetch_assoc();

                                    if($prodrow['prod'] == '' || $prodrow['prod'] == '0'){

                                        $prodpercent = '0';
                                    

                                    }else{

                                        $prodpercent = ($prodrow['prod'] / $totalrow['totaltime']) * 100;

                                    }

                                    if($adminrow['admin']  == '' || $adminrow['admin']  == '0'){

                                        $adminpercent = '0';

                                    }else{

                                        $adminpercent = ($adminrow['admin'] / $totalrow['totaltime']) * 100;

                                    }

                                    if($nonprodrow['nonprod'] == '' || $nonprodrow['nonprod'] == '0'){

                                        $nonprodpercent = '0';

                                    }else{

                                        $nonprodpercent = ($nonprodrow['nonprod'] / $totalrow['totaltime']) * 100;

                                    }
                                    if($breakrow['break'] == '' || $breakrow['break'] == '0'){
                                        $breakpercent = '0';
                                        
                                    

                                    }
                                    else{
                                        $breakpercent = ($breakrow['break'] / $totalrow['totaltime']) * 100;
                                    }



                            

                                    
                                        
                                        



                                        if($break1row['break1'] > 15){

                                            $break1percent =  $break1row['break1'] - 15;
                                        
                                        }else{
                                            $break1percent = 0;
                                        }

                                        if($break2row['break2'] > 15){

                                            $break2percent =  $break2row['break2'] - 15;
                                            
                                        }else{
                                            $break2percent = 0;
                                        }

                                        if($lunchbreakrow['lunch']  > 60 ){
                                            $lunchpercent = $lunchbreakrow['lunch'] - 60;
                                            

                                        }else{
                                            $lunchpercent = 0;
                                        }
                                        if($toiletrow['toilet'] > 10){
                                            $toiletpercent = $toiletrow['toilet'] - 10;
                                        }else{
                                            $toiletpercent = 0;
                                        }



                                    


                                    






                                    

                                    ?>

                                
                                    <div class="col-lg-12 col-sm-12 col-md-12" style="font-size:13px">
                                        <div class="row">
                                        <div class="col-sm-12" >
                                            <div class="card p-1">
                                                <div class="card-body p-1">
                                                    <a href="viewutil.php?empid=<?php echo $emp?>&date=<?php echo $date?>" class="btn btn-info btn-sm float-right">View</a>
                                                    <h5 class="card-title"><?php echo $row['firstname'].' '.$row['surname'] ?></h5> 
                                                    <h6 class="card-title"><?php echo $row['team']?> | <?php echo $row['empid'] ?></h6>
                                            

                                                    <div class="row">

                                                    <div class="col-lg-4"> <h6>Details</h6></div>
                                                    <div class="col-lg-4"> <h6>Summary</h6></div>
                                                    <div class="col-lg-4"> <h6>Breaks</h6></div>

                                                    <div class="col-lg-4">

                                                        DATE: <?php echo $date?> <br>
                                                        LOGIN : <?php echo $loginrow['timein']?> <br>
                                                        LOGOUT: <?php echo $logoffrow['timeout']?> <br>
                                        
                                                        TOTAL TIME: <?php echo $totalrow['totaltime']?><br>
                                                        CURRENT : <?php echo $currentact['activity']?> 

                                                    
                                                        
                                                    </div>
                                                    <div class="col-lg-4">
                                                        
                                            
                                                
                                                        <table class= "table table-sm table-borderless" style="width:100%; font-size:13px;">
                                                        <tr>
                                                            <th> </th>
                                                            <th>Mins</th> 
                                                            <th>Percent</th>
                                                        </tr>
                                                        <tr>
                                                            <td>PROD : </td>
                                                            <td><?php 
                                                            if($prodrow['prod'] == ''){
                                                                echo '0';
                                                            }
                                                            else{

                                                                echo $prodrow['prod'];

                                                            }
                                                            ?> </td> 
                                                            <td><?php echo round($prodpercent, 0)?> %  </td>
                                                        </tr>
                                                        <tr>
                                                            <td>ADMIN:</td>
                                                            <td><?php 
                                                            if($adminrow['admin'] == ''){
                                                                echo '0'; 
                                                        

                                                            }else{
                                                                echo $adminrow['admin'];
                                                            }
                                                        
                                                            
                                                            ?></td> 
                                                            <td><?php echo round($adminpercent, 0)?> %  </td>
                                                        </tr>

                                                        <tr>
                                                            <td>NON-PROD:</td>
                                                            <td><?php 
                                                            if($nonprodrow['nonprod'] == ''){
                                                                echo '0'; 
                                                        

                                                            }else{
                                                                echo $nonprodrow['nonprod'];
                                                            }
                                                            
                                                            
                                                            ?> </td> 
                                                            <td><?php echo round($nonprod, 0)?> %  </td>
                                                        </tr>

                                                        <tr>
                                                            <td>BREAK:</td>
                                                            <td><?php 
                                                            if($breakrow['break'] == ''){
                                                                echo '0'; 
                                                        

                                                            }else{
                                                                echo $breakrow['break'];
                                                            }
                                                            
                                                            
                                                            ?> </td>
                                                            <td><?php echo round($breakpercent, 0)?> %  </td>
                                                        </tr>

                                                        
                                                        </table>

                                        

                                                        
                                            
                                            
                                                    </div>

                                                    <div class="col-lg-4" >

                                                    <table class= "table table-sm table-borderless" style="width:100%; font-size:13px;">
                                                        <tr>
                                                            <th> </th>
                                                            <th>Mins</th> 
                                                            <th>OB</th>
                                                        </tr>
                                                        <tr>
                                                            <td>1ST RBEAK: </td>
                                                            <td><?php 
                                                            if($break1row['break1'] == ''){
                                                                echo '0'; 
                                                        

                                                            }else{
                                                                echo $break1row['break1'];
                                                            }
                                                            
                                                            
                                                            ?> </td>
                                                            <td><?php echo round($break1percent, 0)?>   </td>
                                                        </tr>
                                                        <tr>
                                                            <td>2ND BREAK: </td>
                                                            <td><?php 
                                                            if($break2row['break2'] == ''){
                                                                echo '0'; 
                                                        

                                                            }else{
                                                                echo $break2row['break2'];
                                                            }
                                                            
                                                            
                                                            ?> </td>
                                                            <td><?php echo round($break2percent, 0)?>  </td>
                                                        </tr>

                                                        <tr>
                                                            <td>LUNCH BREAK:</td>
                                                            <td><?php 
                                                            if($lunchbreakrow['lunch'] == ''){
                                                                echo '0'; 
                                                        

                                                            }else{
                                                                echo $lunchbreakrow['lunch'];
                                                            }
                                                            
                                                            
                                                            ?> </td>
                                                            <td><?php echo round($lunchpercent, 0)?>  </td>
                                                        </tr>

                                                        <tr>
                                                            <td>TOILET BREAK:</td>
                                                            <td><?php 
                                                            if($toiletrow['toilet'] == ''){
                                                                echo '0'; 
                                                        

                                                            }else{
                                                                echo $toiletrow['toilet'];
                                                            }
                                                            
                                                            
                                                            ?> </td>
                                                            <td><?php echo round($toiletpercent, 0)?>  </td>
                                                        </tr>
                                                    </table>

                                                        



                                                        

                                                        
                                                    </div>

                                        
                                                    </div>
                                                    
                                            
                                                </div>
                                            </div>
                                            </div>



                                            
                                        </div>







                                    </div>


                                <?php
                            
                                    }
                                } else {
                                    echo "No data found";
                                }




                                ?>

                                        
                            
                            
                    </div>
                </div>
        
                
            </div>
           
            <div class="tab-pane fade" id="nav-profile" role="tabpanel" aria-labelledby="nav-profile-tab">
                <div class="row p-3">
                    <div class="col-lg-12"><h4>Utilization</h4></div>
                   
                    <div class="col-lg-5 my-custom-scrollbar my-custom-scrollbar-primary tableFixHead " id="test_scroll" >
                                    

                                <table class="table table-sm" >
                                    <thead class="thead-dark">
                                        <th>Employee ID</th>
                                        <th>Name</th>
                                        <th>Current Status </th>
                                        <th> Elapsed Time </th>
                                    </thead>
                                    <tbody>
                                     <?php 
                                    $emp1res = mysqli_query($conn, $emplist1);


                                    if(mysqli_num_rows($emp1res) > 0){
                                        while($emprow1 = mysqli_fetch_assoc($emp1res)){
                                            $employee1 = $emprow1['empid'];
    
                                            $utisql ="SELECT * FROM tbl_utilization WHERE empid='$employee1' AND date='$date' ORDER BY id DESC LIMIT 1";
                                            $utilsqlres = mysqli_query($conn, $utisql);
                                            $utilrow = mysqli_fetch_assoc($utilsqlres);
    
    
                                            $utilrow['starttime'];
                                            $curactST = new DateTime($utilrow['starttime']);
                                             $cur = date('h:i');
                                                  $curtime = new DateTime($cur);
                                                  $curelapse = $curactST->diff($curtime);
          
                                                  $curmin = $curelapse->format('%H:%I');
                                                  $getcurentmin = ($curelapse->format('%H') * 3600) + ($curelapse->format('%I') * 60) + $curelapse->format('%S');
                                            
                                                  
    
                                        
    
                                            
                                            echo '<tr><td>'.$emprow1['empid'].'</td>';
                                            echo'<td>'.$emprow1['firstname'].' '.$emprow1['surname'].'</td>';
                                            echo '<td>'.$utilrow['activity'].'</td>';
                               
    
                                            
                                            if($utilrow['date'] != date('m/d/Y')) {
                                                echo '<td> 0 </td>';
                                            }else{
                                                echo '<td>'.$curelapse->format('%H:%I').'</td>';
    
                                            }
                                            
    
    
    
    
                                        }

                                    }else{
                                        echo '<td> No data found </td>';

                                    }


                                    ?>
                                    </tbody>
                                </table>
                                     
                    </div>
                    <div class="col-lg-7 my-custom-scrollbar my-custom-scrollbar-primary" >
                        
                            
                        <?php 

                                





                    
                            //$date = '03/04/2019';\






                            $result = $conn->query($sql1);

                            if ($result->num_rows > 0) {

                                while($row = $result->fetch_assoc()) { 
                                    $emp = $row['empid'];


                                    $login = "SELECT * FROM `tbl_login` WHERE empid= '$emp' AND date = '$date'";
                                    $loginres = $conn->query($login);
                                    $loginrow = $loginres->fetch_assoc();

                                    
                                    $logoff = "SELECT * FROM `tbl_login` WHERE empid= '$emp' AND date = '$date'";
                                    $logoffres = $conn->query($logoff);
                                    $logoffrow = $logoffres->fetch_assoc();





                                    $curact= "SELECT * FROM `tbl_utilization` WHERE empid= '$emp' AND date = '$date' ORDER BY id DESC  LIMIT 1";
                                    $actres = $conn->query($curact);
                                    $currentact = $actres->fetch_assoc();



                                    $prodsql = "SELECT SUM(elapsedtime) AS 'prod'  FROM `tbl_utilization` WHERE empid= '$emp' AND date = '$date' AND type='Prod'";
                                    $prodresult = $conn->query($prodsql);
                                    $prodrow = $prodresult->fetch_assoc();

                                
                                    

                                    $adminsql = "SELECT SUM(elapsedtime) AS 'admin'  FROM `tbl_utilization` WHERE empid= '$emp' AND date = '$date' AND type='Admin'";
                                    $adminresult = $conn->query($adminsql);
                                    $adminrow = $adminresult->fetch_assoc();

                                    $nonprod = "SELECT SUM(elapsedtime) AS 'nonprod'  FROM `tbl_utilization` WHERE empid= '$emp' AND date = '$date' AND type='Non-Prod'";
                                    $nonprodresult = $conn->query($nonprod);
                                    $nonprodrow = $nonprodresult->fetch_assoc();
                                    

                                    $break = "SELECT SUM(elapsedtime) AS 'break'  FROM `tbl_utilization` WHERE empid= '$emp' AND date = '$date' AND type='Break'";
                                    $breakres = $conn->query($break);
                                    $breakrow = $breakres->fetch_assoc();

                                    $break1 = "SELECT SUM(elapsedtime) AS 'break1'  FROM `tbl_utilization` WHERE empid= '$emp' AND date = '$date' AND activity='1st Break'";
                                    $break1res = $conn->query($break1);
                                    $break1row = $break1res->fetch_assoc();

                                    $break2 = "SELECT SUM(elapsedtime) AS 'break2'  FROM `tbl_utilization` WHERE empid= '$emp' AND date = '$date' AND activity='2nd Break'";
                                    $break2res = $conn->query($break2);
                                    $break2row = $break2res->fetch_assoc();

                                    
                                    $lunchbreak = "SELECT SUM(elapsedtime) AS 'lunch'  FROM `tbl_utilization` WHERE empid= '$emp' AND date = '$date' AND activity='Lunch Break'";
                                    $lunchbreakres = $conn->query($lunchbreak);
                                    $lunchbreakrow = $lunchbreakres->fetch_assoc();


                                    $toilet = "SELECT SUM(elapsedtime) AS 'toilet'  FROM `tbl_utilization` WHERE empid= '$emp' AND date = '$date' AND activity='Toilet Break'";
                                    $toiletres = $conn->query($toilet);
                                    $toiletrow = $toiletres->fetch_assoc();

                                    $total = "SELECT SUM(elapsedtime) AS 'totaltime'  FROM `tbl_utilization` WHERE empid= '$emp' AND date = '$date' ";
                                    $totalres = $conn->query($total);
                                    $totalrow = $totalres->fetch_assoc();

                                    if($prodrow['prod'] == '' || $prodrow['prod'] == '0'){

                                        $prodpercent = '0';
                                    

                                    }else{

                                        $prodpercent = ($prodrow['prod'] / $totalrow['totaltime']) * 100;

                                    }

                                    if($adminrow['admin']  == '' || $adminrow['admin']  == '0'){

                                        $adminpercent = '0';

                                    }else{

                                        $adminpercent = ($adminrow['admin'] / $totalrow['totaltime']) * 100;

                                    }

                                    if($nonprodrow['nonprod'] == '' || $nonprodrow['nonprod'] == '0'){

                                        $nonprodpercent = '0';

                                    }else{

                                        $nonprodpercent = ($nonprodrow['nonprod'] / $totalrow['totaltime']) * 100;

                                    }
                                    if($breakrow['break'] == '' || $breakrow['break'] == '0'){
                                        $breakpercent = '0';
                                        
                                    

                                    }
                                    else{
                                        $breakpercent = ($breakrow['break'] / $totalrow['totaltime']) * 100;
                                    }



                            

                                    
                                        
                                        



                                        if($break1row['break1'] > 15){

                                            $break1percent =  $break1row['break1'] - 15;
                                        
                                        }else{
                                            $break1percent = 0;
                                        }

                                        if($break2row['break2'] > 15){

                                            $break2percent =  $break2row['break2'] - 15;
                                            
                                        }else{
                                            $break2percent = 0;
                                        }

                                        if($lunchbreakrow['lunch']  > 60 ){
                                            $lunchpercent = $lunchbreakrow['lunch'] - 60;
                                            

                                        }else{
                                            $lunchpercent = 0;
                                        }
                                        if($toiletrow['toilet'] > 10){
                                            $toiletpercent = $toiletrow['toilet'] - 10;
                                        }else{
                                            $toiletpercent = 0;
                                        }



                                    


                                    






                                    

                                    ?>

                                
                                    <div class="col-lg-12 col-sm-12 col-md-12" style="font-size: 13px;">
                                        <div class="row">
                                        <div class="col-sm-12" >
                                            <div class="card p-1">
                                                <div class="card-body p-1">
                                                    <a href="viewutil.php?empid=<?php echo $emp?>&date=<?php echo $date?>" class="btn btn-info btn-sm float-right">View</a>
                                                    <h5 class="card-title"><?php echo $row['firstname'].' '.$row['surname'] ?></h5> 
                                                    <h6 class="card-title"><?php echo $row['team']?> | <?php echo $row['empid'] ?></h6>
                                            

                                                    <div class="row">

                                                    <div class="col-lg-4"> <h6>Details</h6></div>
                                                    <div class="col-lg-4"> <h6>Summary</h6></div>
                                                    <div class="col-lg-4"> <h6>Breaks</h6></div>

                                                    <div class="col-lg-4">

                                                        DATE: <?php echo $date?> <br>
                                                        LOGIN : <?php echo $loginrow['timein']?> <br>
                                                        LOGOUT: <?php echo $loginrow['timeout']?> <br>
                                        
                                                        TOTAL TIME: <?php echo $totalrow['totaltime']?><br>
                                                        CURRENT : <?php echo $currentact['activity']?> 

                                                    
                                                        
                                                    </div>
                                                    <div class="col-lg-4">
                                                        
                                            
                                                
                                                        <table class= "table table-sm table-borderless" style="width:100%; font-size:13px;"">
                                                        <tr>
                                                            <th> </th>
                                                            <th>Mins</th> 
                                                            <th>Percent</th>
                                                        </tr>
                                                        <tr>
                                                            <td>PROD : </td>
                                                            <td><?php 
                                                            if($prodrow['prod'] == ''){
                                                                echo '0';
                                                            }
                                                            else{

                                                                echo $prodrow['prod'];

                                                            }
                                                            ?> </td> 
                                                            <td><?php echo round($prodpercent, 0)?> %  </td>
                                                        </tr>
                                                        <tr>
                                                            <td>ADMIN:</td>
                                                            <td><?php 
                                                            if($adminrow['admin'] == ''){
                                                                echo '0'; 
                                                        

                                                            }else{
                                                                echo $adminrow['admin'];
                                                            }
                                                        
                                                            
                                                            ?></td> 
                                                            <td><?php echo round($adminpercent, 0)?> %  </td>
                                                        </tr>

                                                        <tr>
                                                            <td>NON-PROD:</td>
                                                            <td><?php 
                                                            if($nonprodrow['nonprod'] == ''){
                                                                echo '0'; 
                                                        

                                                            }else{
                                                                echo $nonprodrow['nonprod'];
                                                            }
                                                            
                                                            
                                                            ?> </td> 
                                                            <td><?php echo round($nonprod, 0)?> %  </td>
                                                        </tr>

                                                        <tr>
                                                            <td>BREAK:</td>
                                                            <td><?php 
                                                            if($breakrow['break'] == ''){
                                                                echo '0'; 
                                                        

                                                            }else{
                                                                echo $breakrow['break'];
                                                            }
                                                            
                                                            
                                                            ?> </td>
                                                            <td><?php echo round($breakpercent, 0)?> %  </td>
                                                        </tr>

                                                        
                                                        </table>

                                        

                                                        
                                            
                                            
                                                    </div>

                                                    <div class="col-lg-4" >

                                                    <table class= "table table-sm table-borderless" style="width:100%; font-size:13px;">
                                                        <tr>
                                                            <th> </th>
                                                            <th>Mins</th> 
                                                            <th>OB</th>
                                                        </tr>
                                                        <tr>
                                                            <td>1ST RBEAK: </td>
                                                            <td><?php 
                                                            if($break1row['break1'] == ''){
                                                                echo '0'; 
                                                        

                                                            }else{
                                                                echo $break1row['break1'];
                                                            }
                                                            
                                                            
                                                            ?> </td>
                                                            <td><?php echo round($break1percent, 0)?>   </td>
                                                        </tr>
                                                        <tr>
                                                            <td>2ND BREAK: </td>
                                                            <td><?php 
                                                            if($break2row['break2'] == ''){
                                                                echo '0'; 
                                                        

                                                            }else{
                                                                echo $break2row['break2'];
                                                            }
                                                            
                                                            
                                                            ?> </td>
                                                            <td><?php echo round($break2percent, 0)?>  </td>
                                                        </tr>

                                                        <tr>
                                                            <td>LUNCH BREAK:</td>
                                                            <td><?php 
                                                            if($lunchbreakrow['lunch'] == ''){
                                                                echo '0'; 
                                                        

                                                            }else{
                                                                echo $lunchbreakrow['lunch'];
                                                            }
                                                            
                                                            
                                                            ?> </td>
                                                            <td><?php echo round($lunchpercent, 0)?>  </td>
                                                        </tr>

                                                        <tr>
                                                            <td>TOILET BREAK:</td>
                                                            <td><?php 
                                                            if($toiletrow['toilet'] == ''){
                                                                echo '0'; 
                                                        

                                                            }else{
                                                                echo $toiletrow['toilet'];
                                                            }
                                                            
                                                            
                                                            ?> </td>
                                                            <td><?php echo round($toiletpercent, 0)?>  </td>
                                                        </tr>
                                                    </table>

                                                        



                                                        

                                                        
                                                    </div>

                                        
                                                    </div>
                                                    
                                            
                                                </div>
                                            </div>
                                            </div>



                                            
                                        </div>







                                    </div>


                                <?php
                            
                                    }
                                } else {
                                    echo "No data found";
                                }




                                ?>

                                        
                            
                            
                    </div>
                    
                </div>
            </div>

        </div>

          

          <!-- Icon Cards-->
      
        <!-- /.container-fluid -->

        <!-- Sticky Footer -->

      </div>
      <!-- /.content-wrapper -->

    </div>
    <!-- /#wrapper -->

    <!-- Scroll to Top Button-->
    <a class="scroll-to-top rounded" href="#page-top">
      <i class="fas fa-angle-up"></i>
    </a>

    <!-- Logout Modal-->
    <div class="modal fade" id="logoutModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title" id="exampleModalLabel">Ready to Leave?</h5>
            <button class="close" type="button" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">×</span>
            </button>
          </div>
          <div class="modal-body">Select "Logout" below if you are ready to end your current session.</div>
          <div class="modal-footer">
            <button class="btn btn-secondary" type="button" data-dismiss="modal">Cancel</button>
            <a class="btn btn-primary" href="login.html">Logout</a>
          </div>
        </div>
      </div>
    </div>

    <!-- Bootstrap core JavaScript-->
    <script src="../vendor/jquery/jquery.min.js"></script>
    <script src="../vendor/bootstrap/js/bootstrap.bundle.min.js"></script>

    <!-- Core plugin JavaScript-->
    <script src="../vendor/jquery-easing/jquery.easing.min.js"></script>

    <!-- Page level plugin JavaScript-->
    <script src="../vendor/chart.js/Chart.min.js"></script>
    <script src="../vendor/datatables/jquery.dataTables.js"></script>
    <script src="../vendor/datatables/dataTables.bootstrap4.js"></script>

    <!-- Custom scripts for all pages-->
    <script src="../js/sb-admin.min.js"></script>

    <!-- Demo scripts for this page-->
    <script src="../js/demo/datatables-demo.js"></script>
    <script src="../js/demo/chart-area-demo.js"></script>

          <script language="javascript" type="text/javascript">
            $(document).ready(function(){
            $("form").on("submit", function(){
                $("#pageloader").fadeIn();
            });//submit

             $('#preloader').fadeOut();
            });//document ready


            var myCustomScrollbar = document.querySelector('.my-custom-scrollbar');
Ps.initialize(myCustomScrollbar);

var scrollbarY = myCustomScrollbar.querySelector('.ps.ps--active-y>.ps__scrollbar-y-rail');

myCustomScrollbar.onscroll = function() {
  scrollbarY.style.cssText = `top: ${this.scrollTop}px!important; height: 400px; right: ${-this.scrollLeft}px`;
}


        </script>
         <script>
    $('a[data-toggle="tab"]').click(function (e) {
    e.preventDefault();
    $(this).tab('show');
    });

    $('a[data-toggle="tab"]').on("shown.bs.tab", function (e) {
        var id = $(e.target).attr("href");
        localStorage.setItem('selectedTab', id)
    });

    var selectedTab = localStorage.getItem('selectedTab');
    if (selectedTab != null) {
        $('a[data-toggle="tab"][href="' + selectedTab + '"]').tab('show');
    }



    </script>
    <script>
     var time = new Date().getTime();
     $(document.body).bind("mousemove keypress", function(e) {
         
         time = new Date().getTime();

     });

     function refresh() {
        /// 30000 30 secs 


         if(new Date().getTime() - time >= 5000) 
             window.location.reload(true);
           
         else 
             setTimeout(refresh, 1000);


     }

     setTimeout(refresh, 1000);


     $(window).scroll(function() {
  sessionStorage.scrollTop = $(this).scrollTop();
});

$(document).ready(function() {
  if (sessionStorage.scrollTop != "undefined") {
    $(window).scrollTop(sessionStorage.scrollTop);
  }
});


</script>
</body>

</html>
