<?php 

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

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


function mnltopst($date, $time){

    $convert = new DateTime($date.' '.$time, new DateTimeZone('Asia/Manila'));
    
    
    $convert->setTimezone(new DateTimeZone('America/Los_Angeles'));
    
    $datetime = $convert->format('m/d/Y h:i A');
    
    
    return ($datetime);
    
    
    
    
    }
    
    function psttomnl($date, $time){
    
      $convert = new DateTime($date.' '.$time, new DateTimeZone('America/Los_Angeles'));
      
      
      $convert->setTimezone(new DateTimeZone('Asia/Manila'));
      
      $datetime = $convert->format('m/d/Y h:i A');
      
      
      return ($datetime);
      
      
      
      
      }

      if(isset($_POST['team']) && isset($_POST['date'])){

        $team = phps($_POST['team']);
         $date = phps($_POST['date']);
          $date = date('m/d/Y', strtotime($date));
          $dateval = date('Y-m-d', strtotime($date));
   
   
       if($team == 'All'){
   
   
   
           $emplist = "SELECT * FROM empinfo JOIN empdetails ON empinfo.empid = empdetails.detailid  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' 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' ORDER BY empinfo.empid ";
          $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 = '$team'  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='$team' 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 ='$team' ORDER BY empinfo.empid ";
          $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='$team' GROUP BY tbl_utilization.empid ";
   
                 
       }
   
   
   
    }else{
   
       $emplist = "SELECT * FROM empinfo JOIN empdetails ON empinfo.empid = empdetails.detailid  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' 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' ORDER BY empinfo.empid";
     $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 ";    
   
   
    }


?>


                <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);
                    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 A');
                                $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>';

                        }
                        




                    }
                    mysqli_close($conn);
                    ?>
                    </tbody>
                </table>
                        
