<?php 

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

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

if(isset($_POST['cancelot'])){
  $otid = $_POST['otid'];
  $cancelot = "UPDATE ot SET status='5' WHERE id='$otid'";
  if(mysqli_query($conn, $cancelot)){

    $alert='<div class="alert alert-success" role="alert"> OT successfully cancelled </div>';
  
    header('location: ot.php?alert='.$alert);
  }

}


?>
<!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>Employee | Over Time</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";
       $ot = "active";
      include('sidebar.php');
      ?>
      <div id="content-wrapper">

        <div class="container-fluid">

          <!-- Breadcrumbs-->
          <ol class="breadcrumb">

            <li class="breadcrumb-item active">Over Time</li>
          </ol>
          <div class="row p-3">
            <div class="col-lg-12">
                <h4>Over Time</h4>
                <?php  echo $_GET['alert']; ?>
            </div>

            <div class="col-lg-12">
                <a href="fileot.php" class="btn btn-success"> <i class="fa fa-check"> </i> File OT </a>
            </div>
            <div class="col-lg-12 p-3 table-responsive">

              <table class="table" id="otlist">
                <thead class="thead-dark">
                  <tr>
                    <th>Status</th>
                    <th>Type</th>
                    <th>Date</th>
                    <th> Duration </th>
                    <th>Team Head </th>
                    <th> HR </th>
                    <th> Manager </th>
                    <th> Action </th>
                  </tr>
                </thead>
                <tbody>
                  <?php 
                    

                    $otsql = "SELECT * FROM ot WHERE empid='$empid' ORDER BY datefiled DESC";
                    $otres = mysqli_query($conn, $otsql);


                    if(mysqli_num_rows($otres) > 0){

                      while($otrow = mysqli_fetch_assoc($otres)){

                        $id = $otrow['id'];
                        $schedcode = $otrow['schedule'];
                        $log = $otrow['attendance'];
  
                        $schedulesql ="SELECT * FROM schedule WHERE schedcode='$schedcode'";
                        $schedres = mysqli_query($conn, $schedulesql);
                        $schedrow = mysqli_fetch_assoc($schedres);
  
  
                        $logsql ="SELECT * FROM tbl_login WHERE id='$log'";
                        $logres = mysqli_query($conn, $logsql);
                        $logrow = mysqli_fetch_assoc($logres);

                        if($schedrow['starttime'] == 'OFF' || $schedrow['starttime'] == 'HOLIDAY OFF'){

                          $schedpst = $schedrow['starttime'];



                      }else{

                          $schedMNLPST = new DateTime($schedrow['starttime'], new DateTimeZone('Asia/Manila'));

                          $schedMNLPST->setTimezone(new DateTimeZone('America/Los_Angeles'));


                          
                          $schedMNLPST1 = new DateTime($schedrow['endtime'], new DateTimeZone('Asia/Manila'));

                          $schedMNLPST1->setTimezone(new DateTimeZone('America/Los_Angeles'));

                          
  
                          $schedpst = $schedMNLPST->format('h:i A');

                          $schedpst1 = $schedMNLPST1->format('h:i A');
     



                      }
  
                
  
                        echo '<tr>';
  
                        ////STATUS COLUMN
  
                        if($otrow['status'] == '0' || $otrow['status'] == '1' || $otrow['status'] == '2' ){
  
                          echo '<td> Pending Approval </td>';
  
                        }else if($otrow['status'] == '3'){
  
                          echo '<td> Approved </td>';
  
  
                        }else if($otrow['status'] == '4.1' || $otrow['status'] == '4.2' || $otrow['status'] == '4.3'){
  
                          echo '<td> Declined </td>';
                        }else{
  
                          echo '<td> Cancelled </td>';
  
                        }
                        //////
  
                        echo'<td>'.$otrow['type'].'</td>';
                        echo'<td>'.date('m/d/Y', strtotime($otrow['date'])).'</td>';
                        echo'<td>'.$otrow['hrs'].'</td>';
  
                        ///TEAM HEAD, HR, MANAGER, 
  
                        
                        if($otrow['status'] == '0'){
  
                          echo '<td> Pending </td>';
                          echo '<td> Pending </td>';
                          echo '<td> Pending </td>';
  
                        }
                        
                        else if($otrow['status'] == '1'){
  
                          echo '<td> Approved <br> '.$otrow['tl'].' </td>';
                          echo '<td> Pending </td>';
                          echo '<td> Pending </td>';
  
  
  
                        } else if ($otrow['status'] == '2' ){
  
                          echo '<td> Approved <br> '.$otrow['tl'].' </td>';
                          echo '<td> Approved <br> '.$otrow['hr'].' </td>';
                          echo '<td> Pending </td>';
  
                        }else if($otrow['status'] == '3'){
  
                          echo '<td> Approved <br> '.$otrow['tl'].' </td>';
                          echo '<td> Approved <br> '.$otrow['hr'].' </td>';
                          echo '<td> Approved <br> '.$otrow['mng'].' </td>';
  
                        }else if($otrow['status'] == '4.1'){
  
                          echo '<td> Declined <br> '.$otrow['tl'].' </td>';
                          echo '<td> Declined </td>';
                          echo '<td> Declined </td>';
  
  
  
                        } else if ($otrow['status'] == '4.2' ){
  
                          echo '<td> Declined <br> '.$otrow['tl'].' </td>';
                          echo '<td> Declined <br> '.$otrow['hr'].' </td>';
                          echo '<td> Declined </td>';
  
                        }else if($otrow['status'] == '4.3'){
  
                          echo '<td> Declined <br> '.$otrow['tl'].' </td>';
                          echo '<td> Declined <br> '.$otrow['hr'].' </td>';
                          echo '<td> Declined <br> '.$otrow['mng'].' </td>';
  
                        }else{
  
                          echo '<td> Cancelled <br> '.$otrow['tl'].' </td>';
                          echo '<td> Cancelled <br> '.$otrow['hr'].' </td>';
                          echo '<td> Cancelled <br> '.$otrow['mng'].' </td>';
  
  
                        }
  
  
                        ////ACTION 
  
                        echo '<td><a href="#view'.$id.'" class="btn btn-primary text-white btn-sm" data-toggle="modal"> <i class="far fa-eye"></i> View </a> | ';
  
                        if($otrow['status'] == '5'){
  
                          echo'
                          <button type="button" class="btn btn-danger btn-sm" disabled> <i class="fa fa-close"></i>  Cancel </button> ';
                     
                        } else{
  
                          echo'
                          <a href="#cancel'.$id.'" class="btn btn-danger text-white btn-sm" data-toggle="modal"> <i class="fa fa-close"></i> Cancel </a></td>';
                        } ?>
  
                          <div id="view<?php echo $id; ?>" class="modal fade" role="dialog">
                              <form method="post" class="form-horizontal" role="form">
                                  <div class="modal-dialog modal-md modal-dialog-centered">
                                      <!-- Modal content-->
                                      <div class="modal-content">
                                          <div class="modal-header">
                                              <h4 class="modal-title"> View Leave</h4>
                                              <button type="button" class="close" data-dismiss="modal">&times;</button>
  
                                          </div>
  
                                          <div class="modal-body">
                                            <div class="row">
                                              <div class="col-lg-6">
                                                <label for=""> Date Filed</label> <br>
                                                <input type= "date" class="form-control" value="<?php  echo $otrow['datefiled']?>" disabled>
                                              </div>
                                              <div class="col-lg-6">
                                                <label for=""> Shift Date</label> <br>
                                                <input type= "date" class="form-control" value="<?php  echo $otrow['date']?>" disabled> 
                                              </div>
                                              <div class="col-lg-6">
                                                <label for=""> Schedule PST </label> <br>
                                                <input type= "text" class="form-control" value="<?php  echo $schedpst.' - ' .$schedpst1 ?> " disabled> 
                                              </div>
                                              <div class="col-lg-6">
                                                <label for=""> Attendance </label> <br>
                                                <input type= "text" class="form-control" value="<?php  echo $logrow['timein'].' - ' .$logrow['timeout']?> " disabled> 
                                              </div>
                                              <div class="col-lg-6">
                                                <label for=""> Type </label> <br>
                                                <input type= "text" class="form-control" value="<?php  echo  $otrow['type']?> " disabled> 
                                              </div>
  
                                              <div class="col-lg-6">
                                                <label for=""> No. of OT Hours </label> <br>
                                                <input type= "text" class="form-control" value="<?php  echo  $otrow['hrs']?> " disabled> 
                                              </div>
                                            
                                            </div>
                                           
                                              
  
                                          </div>
  
                                          <div class="modal-footer">
  
                                            <a href="viewutil.php?date=<?php echo date('m/d/Y', strtotime($otrow['date'])); ?>" class="btn btn-primary"> <i class="far fa-eye"> </i>  View Utilization</a>
  
                                              
  
                                              <button type="button" class="btn btn-danger" data-dismiss="modal"><i class="fas fa-close"> </i> Close</button>
                                          </div>
                                      </div>
                                  </div>
                                  
                              </form>
                              
  
                          </div>
                          <div id="cancel<?php echo $id; ?>" class="modal fade" role="dialog">
                            <form method="post" class="form-horizontal" role="form">
                                <div class="modal-dialog modal-md modal-dialog-centered">
                                    <!-- Modal content-->
                                    <div class="modal-content">
                                        <div class="modal-header">
                                        <h4 class="modal-title">Cancel Leave</h4>
                                            <button type="button" class="close" data-dismiss="modal">&times;</button>
                                
                                        </div>
                                        <div class="modal-body">
                                            <input type="hidden" name="otid" value="<?php echo $id; ?>">
               
  
                                            Are you sure you want to cancel your OT ?
                                          
  
                                        </div>
  
                                        <div class="modal-footer">
                                            <button type="submit" class="btn btn-success" name="cancelot"><i class="fas fa-check"> </i> Yes</button>
                                            <button type="button" class="btn btn-danger" data-dismiss="modal"><i class="fas fa-close"> </i> Cancel</button>
                                        </div>
                                    </div>
                                </div>
                            </form>
                          </div>
  
  
  
  
  
  
  
  
  
  
  
                        <?php 
  
                        
  
  
  
                        echo '</tr>';
  
  
  
                      }

                    }else{

                      echo' <tr> <td colspan= 8> No data found</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

            $(document).ready(function() {
              $('#otlist').DataTable( {

                responsive: true

              } );
            } );




        </script>
</body>

</html>
