<?php 

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

if( !isset($_SESSION['user']) ) {
 header("Location: ../index.php");
 exit;
}

$empid = $_SESSION['user'];
$date = date('m/d/Y');
$date= $_GET['date'];

$sqlinfo = "SELECT * FROM empinfo WHERE empid='$empid'";
$sqlinfores = $conn->query($sqlinfo);
$inforow = $sqlinfores->fetch_assoc();

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

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






}


?>
<!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>Team Head | View 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" onload=display_ct();>

    <?php include('uppernav.php')?>



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

        <div class="container-fluid">

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

          </ol>

          



            

          
            <div class="row p-3" >

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

                <?php 

                                





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


$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='$team' AND tbl_utilization.empid = '$empid' GROUP BY tbl_utilization.empid ";







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



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

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

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




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





        $curact= "SELECT * FROM `tbl_utilization` WHERE empid= '$empid' 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= '$empid' 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= '$empid' 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= '$empid' 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= '$empid' 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= '$empid' 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= '$empid' 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= '$empid' 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= '$empid' 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= '$empid' 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:15px">
        <h5>View Utilization</h5>
                <div class="col-lg-12 p-1 form-inline" >
                    <form method="POST">
                        <button type="submit" class="btn btn-primary" name="utilfilter"><i class="fa fa-filter"></i> Filter</button> &nbsp


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

            
            
    
            

                </div>
            <div class="row">
                <div class="col-sm-12" >
                    <div class="card p-1">
                        <div class="card-body p-1">
            
                            <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 date('h:i A', strtotime($loginrow['timein']))?> <br>
                                LOGOUT: <?php echo $logoffrow['timeout']==''? ' ' : date('h:i A', strtotime($logoffrow['timeout'])) ?> <br>
                
                                TOTAL TIME: <?php echo $totalrow['totaltime']?><br>
                                CURRENT : <?php echo $currentact['activity']?> 

                            
                                
                            </div>
                            <div class="col-lg-4">
                                
                    
                        
                                <table style="width:100%" class="table table-borderless table-sm">
                                <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 style="width:100%" class="table table-borderless table-sm">
                                <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>



        <div class="col-lg-12 my-custom-scrollbar my-custom-scrollbar-primary tableFixHead" >
                


                <table class="table table-bordered table-sm">
                <thead class="thead-dark">
                <th>Activity</th>
                <th>Type</th>
                <th>Start Time</th>
                <th>End time</th>
                <th>Date</th>
                <th>Elapsed Time</th>
                <th>Remarks</th>
            
                
                </thead>
                

                <tbody>

                <?php 

                
                            
                
                                
            
            
                                        
                                
                            
                    // $date = '03/04/2019';
                        $sql = "SELECT * FROM `tbl_utilization` JOIN empinfo ON tbl_utilization.empid = empinfo.empid WHERE tbl_utilization.empid ='$empid' AND date = '$date' ";
                        $result = $conn->query($sql);


                        
                    if ($result->num_rows > 0) {
                
                        while($row = $result->fetch_assoc()) { 

                            echo '
                            
                            <tr><td>'.$row['activity'].'</td>
                            <td>'.$row['type'].'</td>
                            <td>'.($row['starttime']== '' ? '00:00' : date('h:i A', strtotime($row['starttime']))).'</td>
                            <td>'.($row['endtime']== '' ? '00:00' : date('h:i A', strtotime($row['endtime']))).'</td>
                            <td>'.$row['date'].'</td>
                            <td>'.$row['elapsedtime'].'</td>
                            <td>'.$row['remarks'].'</td> </tr>
                            ';



                        }

                    }


                mysqli_close($conn);
                ?>
                
                </tbody>
                </table>
        </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



        </script>
</body>

</html>
