<?php 

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

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

 

    $filterteam = $_POST['team'];
    $co = $conn->query("SELECT * FROM pto_cut_off WHERE id='".$_POST['year_team']."' ");
    $co_row = $co->fetch_assoc();

    if($filterteam == 'All'){

        $utilpto = "SELECT *, `leave`.status AS 'stat' FROM `leave` JOIN empdetails ON `leave`.empid = empdetails.detailid JOIN empinfo ON empdetails.detailid = empinfo.empid WHERE `leave`.status IN('3','4.1','4.2','4.3') AND datefiled >= '".$co_row['d_from']."' AND datefiled <= '".$co_row['d_to']."' ORDER BY empinfo.empid";
    }else{

        $utilpto = "SELECT *, `leave`.status AS 'stat' FROM `leave` JOIN empdetails ON `leave`.empid = empdetails.detailid JOIN empinfo ON empdetails.detailid = empinfo.empid WHERE empdetails.team='$filterteam' AND `leave`.status IN('3','4.1','4.2','4.3') AND datefiled >= '".$co_row['d_from']."' AND datefiled <= '".$co_row['d_to']."' ORDER BY empinfo.empid";

    }

}else{

    $latest_cutoff = $conn->query("SELECT * FROM pto_cut_off ORDER BY id DESC LIMIT 1 ");
    $latest_cut_off = $latest_cutoff->fetch_assoc();

    $utilpto = "SELECT *, `leave`.status AS 'stat' FROM `leave` JOIN empdetails ON `leave`.empid = empdetails.detailid JOIN empinfo ON empdetails.detailid = empinfo.empid WHERE `leave`.status IN('3','4.1','4.2','4.3') AND datefiled >= '".$latest_cut_off['d_from']."' AND datefiled <= '".$latest_cut_off['d_to']."'  ORDER BY empinfo.empid";



}

if(isset($_POST['filterindividual'])){
    $co = $conn->query("SELECT * FROM pto_cut_off WHERE id='".$_POST['year_indi']."' ");
    $co_row = $co->fetch_assoc();

    $filterindi = $_POST['emp'];

    if($filterindi == 'All'){

        $utilptoindi = "SELECT *, `leave`.status AS 'stat' FROM `leave` JOIN empdetails ON `leave`.empid = empdetails.detailid JOIN empinfo ON empdetails.detailid = empinfo.empid WHERE `leave`.status IN('3','4.1','4.2','4.3') AND datefiled >= '".$co_row['d_from']."' AND datefiled <= '".$co_row['d_to']."' ORDER BY empinfo.empid";
    }else{

       $utilptoindi = "SELECT *, `leave`.status AS 'stat' FROM `leave` JOIN empdetails ON `leave`.empid = empdetails.detailid JOIN empinfo ON empdetails.detailid = empinfo.empid WHERE empdetails.empid='$filterindi' AND `leave`.status IN('3','4.1','4.2','4.3') AND datefiled >= '".$co_row['d_from']."' AND datefiled <= '".$co_row['d_to']."' ORDER BY empinfo.empid";

    }

}else{   
    $latest_cutoff = $conn->query("SELECT * FROM pto_cut_off ORDER BY id DESC LIMIT 1 ");
    $latest_cut_off = $latest_cutoff->fetch_assoc();

    $utilptoindi = "SELECT *, `leave`.status AS 'stat' FROM `leave` JOIN empdetails ON `leave`.empid = empdetails.detailid JOIN empinfo ON empdetails.detailid = empinfo.empid WHERE `leave`.status IN('3','4.1','4.2','4.3') AND datefiled >= '".$latest_cut_off['d_from']."' AND datefiled <= '".$latest_cut_off['d_to']."'  ORDER BY empinfo.empid";



}
                     
?>
<!DOCTYPE html>
<html lang="en">

  <head><meta http-equiv="Content-Type" content="text/html; 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 | Dashboard</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 
       $report = "active";
       $ptoutil = "active";
      include('sidebar.php');
      ?>
      <div id="content-wrapper">

        <div class="container-fluid">

          <!-- Breadcrumbs-->
          <ol class="breadcrumb">
    
            <li class="breadcrumb-item active">Paid Time Off Utilization</li>
          </ol>
        <ul class="nav nav-tabs" id="myTab" role="tablist">
            <li class="nav-item">
                <a class="nav-link active" id="home-tab" data-toggle="tab" href="#home" role="tab" aria-controls="home" aria-selected="true">Team</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" id="profile-tab" data-toggle="tab" href="#profile" role="tab" aria-controls="profile" aria-selected="false">Individual</a>
            </li>

        </ul>
            <div class="tab-content" id="myTabContent">
                <div class="tab-pane fade show active p-2" id="home" role="tabpanel" aria-labelledby="home-tab">
                    <div class="row">
                        <div class="col-lg-12 form-inline p-2">
                            <form method="POST">
                                <button type='submit' class="btn btn-primary" name="filterteam"><i class="fas fa-filter"></i> Filter</button>

                                <select name="team" class="form-control" id="">
                                    <option value="team" selected disabled> Team </option>
                                    <option value="All"> All </option>

                                    <?php 
                                        $sqlteam ="SELECT team FROM empdetails GROUP BY team";
                                        $sqlres = mysqli_query($conn, $sqlteam);
                                        while($sqlrow = mysqli_fetch_assoc($sqlres)){

                                            if($filterteam == $sqlrow['team']){
                                                echo '<option selected>'.$sqlrow['team'].'</option>';


                                            }else{
                                                echo '<option>'.$sqlrow['team'].'</option>';
                                            }

                                            
                                        }

                                        
                                    
                                    ?>
                                </select>

                                <select name="year_team" id="year_team" class="form-control">
                                <!-- <option value="" selected disabled> Select Year </option> -->
                                    <?php 
                                    
                                        $pto_year = $conn->query('SELECT * FROM pto_cut_off ORDER BY id DESC');
                                        echo $conn->error;
                                        while($y_row = $pto_year->fetch_assoc()){
                                            echo '<option value="'.$y_row['id'].'" 
                                            
                                            '.(isset($_POST['year_team']) && $_POST['year_team'] == $y_row['id']? "selected" : "" ).'
                                            
                                            
                                            >'.$y_row['year'].'</option>';
                                        }
                                    
                                    ?>
                                </select>
                            
                            
                            </form>

                        </div>
                        <div class="col-lg-12 table-responsive tableFixHead">
                            <table class="table table-bordered">
                                <thead class="thead-dark">
                                    <th>Employee ID</th>
                                    <th>Name</th>
                                    <th>Team</th>
                                    
                                    <th>Type</th>
                                    <th>Date Filed</th>
                                    <th>Date</th>
                                    <th>Duration</th>
                                    <th>Status</th>

                                </thead>
                                <tbody>
                                    <?php
                                    
                                    $utilres = mysqli_query($conn, $utilpto);
                                  
                                    while ($utilptorow = mysqli_fetch_assoc($utilres)){
                                        echo '<tr>';

                                        echo '<td>'.$utilptorow['empid'].'</td>';
                                        echo '<td>'.$utilptorow['firstname'].' '.$utilptorow['surname'].'</td>';
                                        echo '<td>'.$utilptorow['team'].'</td>';
                                        echo '<td>'.$utilptorow['name'].'</td>';
                                        echo '<td>'.date('m/d/Y', strtotime($utilptorow['datefiled'])).'</td>';
                                        echo '<td>'.date('m/d/Y', strtotime($utilptorow['datefrom'])).' - '.date('m/d/Y', strtotime($utilptorow['dateto'])).'</td>';
                                        echo '<td>'.$utilptorow['duration'].'</td>';


                                        if($utilptorow['stat'] == '3'){

                                            echo '<td> Approved </td>';


                                        }else if($utilptorow['stat'] == '5'){
                                            echo '<td> Cancelled </td>';
                                            
                                        }else{

                                            echo '<td> Declined </td>';

                                        }
                                        

                                        echo '</tr>';
                                        
                                        


                                    }
                                    
                                    ?>

                                        
                                </tbody>
                            
                            </table>

                        
                        </div>
                    
                    </div>

                    <div name="printArea" id="printArea" hidden>
                        <div style="margin:0 auto; width:50%"> 

                            <img src="../images/logo1.jpg"style="float:left; padding:10px 20px;" >
                            &nbsp &nbsp &nbsp &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
                            <h1 style="font-size: 40px"> ATV International ROHQ </h1>
                        
                            
                            <br>

                        </div>
                        <div class="col-12">
                
                
                            <hr class="new1" style="height:2px; border:none; color:#000; background-color:#000;">
                        </div>
                        <h5> Paid Time Off Utilization <br> <?php if($filterteam == 'All') echo ''; else echo $filterteam; ?></h5>

                            <table class="table table-bordered" id="team_pto_tbl">
                                <thead class="thead-dark">
                                    <th>Employee ID</th>
                                    <th>Name</th>
                                    <th>Team</th>
                                    
                                    <th>Type</th>
                                    <th>Date Filed</th>
                                    <th>Date</th>
                                    <th>Duration</th>
                                    <th>Status</th>

                                </thead>
                                <tbody>
                                    <?php
                                    
                                    $utilres = mysqli_query($conn, $utilpto);
                                  
                                    while ($utilptorow = mysqli_fetch_assoc($utilres)){
                                        echo '<tr>';

                                        echo '<td>'.$utilptorow['empid'].'</td>';
                                        echo '<td>'.$utilptorow['firstname'].' '.$utilptorow['surname'].'</td>';
                                        echo '<td>'.$utilptorow['team'].'</td>';
                                        echo '<td>'.$utilptorow['name'].'</td>';
                                        echo '<td>'.date('m/d/Y', strtotime($utilptorow['datefiled'])).'</td>';
                                        echo '<td>'.date('m/d/Y', strtotime($utilptorow['datefrom'])).' - '.date('m/d/Y', strtotime($utilptorow['dateto'])).'</td>';
                                        echo '<td>'.$utilptorow['duration'].'</td>';


                                        if($utilptorow['stat'] == '3'){

                                            echo '<td> Approved </td>';


                                        }else if($utilptorow['stat'] == '5'){
                                            echo '<td> Cancelled </td>';
                                            
                                        }else{

                                            echo '<td> Declined </td>';

                                        }
                                        

                                        echo '</tr>';
                                        
                                        


                                    }
                                    
                                    ?>

                                        
                                </tbody>
                            
                            </table>
            
                      


                    </div>

                





                    <span class="pull-right">
                    <button type="button" class="btn btn-danger" onclick="printContent('printArea')"> <i class="fas fa-file-pdf"></i> PDF</button>
                    <button onclick="exportTableToExcel('team_pto_tbl', '<?php echo $filterteam.'pto'?>')" class="btn btn-success"> <i class="fa fa-table"></i> Excel</button>
                    </span>

                  

                    

                </div>
                <div class="tab-pane fade p-2" id="profile" role="tabpanel" aria-labelledby="profile-tab">
                    <div class="row">
                            <div class="col-lg-12 form-inline p-2">
                                <form method="POST">
                                    <button type='submit' class="btn btn-primary" name="filterindividual"><i class="fas fa-filter"></i> Filter</button>

                                    <select name="emp" class="form-control" id="">
                                        <option selected disabled> Employee </option>
                                        <option value="All"> All </option>

                                        <?php 
                                            $indisqlteam = "SELECT * FROM empdetails JOIN empinfo ON empinfo.empid = empdetails.empid";
                                            $indisqlres = mysqli_query($conn, $indisqlteam);
                                            while($indisqlrow = mysqli_fetch_assoc($indisqlres)){

                                                if($filterindi == $indisqlrow['empid']){
                                                    echo '<option value="'.$indisqlrow['empid'].'" selected>'.$indisqlrow['empid'].' '.$indisqlrow['firstname'].' '.$indisqlrow['surname'].'</option>';


                                                }else{
                                                    echo '<option value="'.$indisqlrow['empid'].'">'.$indisqlrow['empid'].' '.$indisqlrow['firstname'].' '.$indisqlrow['surname'].'</option>';
                                                }

                                                
                                            }

                                            
                                        
                                        ?>
                                    </select>
                                    <select name="year_indi" id="year_indi" class="form-control">
                                        <!-- <option value="" selected disabled> Select Year </option> -->
                                        <?php 
                                        
                                            $pto_year = $conn->query('SELECT * FROM pto_cut_off ORDER BY id DESC');
                                            echo $conn->error;
                                            while($y_row = $pto_year->fetch_assoc()){
                                                echo '<option value="'.$y_row['id'].'" 
                                                
                                                '.(isset($_POST['year_indi']) && $_POST['year_indi'] == $y_row['id']? "selected" : "" ).'
                                                
                                                
                                                >'.$y_row['year'].'</option>';
                                            }
                                        
                                        ?>
                                    </select>
                                
                                
                                </form>

                            </div>
                            <div class="col-lg-12 table-responsive tableFixHead">
                                <table class="table table-bordered" id="indiptotable">
                                    <thead class="thead-dark">
                                        <th>Employee ID</th>
                                        <th>Name</th>
                                        <th>Team</th>
                                        
                                        <th>Type</th>
                                        <th>Date Filed</th>
                                        <th>Date</th>
                                        <th>Duration</th>
                                        <th>Status</th>

                                    </thead>
                                    <tbody>
                                        <?php
                                        
                                        $indiutilres = mysqli_query($conn, $utilptoindi);
                                    
                                        while ($indiutilptorow = mysqli_fetch_assoc($indiutilres)){
                                            echo '<tr>';

                                            echo '<td>'.$indiutilptorow['empid'].'</td>';
                                            echo '<td>'.$indiutilptorow['firstname'].' '.$indiutilptorow['surname'].'</td>';
                                            echo '<td>'.$indiutilptorow['team'].'</td>';
                                            echo '<td>'.$indiutilptorow['name'].'</td>';
                                            echo '<td>'.date('m/d/Y', strtotime($indiutilptorow['datefiled'])).'</td>';
                                            echo '<td>'.date('m/d/Y', strtotime($indiutilptorow['datefrom'])).' - '.date('m/d/Y', strtotime($indiutilptorow['dateto'])).'</td>';
                                            echo '<td>'.$indiutilptorow['duration'].'</td>';


                                            if($indiutilptorow['stat'] == '3'){

                                                echo '<td> Approved </td>';


                                            }else if($indiutilptorow['stat'] == '5'){
                                                echo '<td> Cancelled </td>';
                                            
                                            
                                                
                                            }else{

                                                echo '<td> Declined </td>';

                                            }
                                            

                                            echo '</tr>';
                                            
                                            


                                        }
                                        
                                        ?>

                                            
                                    </tbody>
                                
                                </table>

                            
                            </div>
                        
                        </div>

                        <div name="printAreaindi" id="printAreaindi" hidden>
                            <div style="margin:0 auto; width:50%"> 

                                <img src="../images/logo1.jpg"style="float:left; padding:10px 20px;" >
                                &nbsp &nbsp &nbsp &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
                                <h1 style="font-size: 40px"> ATV International ROHQ </h1>
                            
                                
                                <br>

                            </div>
                            <div class="col-12">
                    
                    
                                <hr class="new1" style="height:2px; border:none; color:#000; background-color:#000;">
                            </div>
                            <?php 
                                $empinfosql ="SELECT * FROM empdetails JOIN empinfo ON empinfo.empid = empdetails.empid WHERE empdetails.empid='$filterindi'";
                                $empinfores = mysqli_query($conn, $empinfosql);
                                $empinforow = mysqli_fetch_assoc($empinfores);
                            ?>
                            <h5> Paid Time Off Utilization <br> <?php if($filterindi == 'All') echo ''; else echo $filterindi.' | '.$empinforow['firstname'].' '.$empinforow['surname']; ?></h5>

                            <table class="table table-bordered" id="mytable">
                                    <thead class="thead-dark">
                                        <th>Employee ID</th>
                                        <th>Name</th>
                                        <th>Team</th>
                                        <th>Type</th>
                                        <th>Date Filed</th>
                                        <th>Date</th>
                                        <th>Duration</th>
                                        <th>Status</th>

                                    </thead>
                                    <tbody>
                                        <?php
                                        
                                        $indiutilres = mysqli_query($conn, $utilptoindi);
                                    
                                        while ($indiutilptorow = mysqli_fetch_assoc($indiutilres)){
                                            echo '<tr>';

                                            echo '<td>'.$indiutilptorow['empid'].'</td>';
                                            echo '<td>'.$indiutilptorow['firstname'].' '.$indiutilptorow['surname'].'</td>';
                                            echo '<td>'.$indiutilptorow['team'].'</td>';
                                            echo '<td>'.$indiutilptorow['name'].'</td>';
                                            echo '<td>'.date('m/d/Y', strtotime($indiutilptorow['datefiled'])).'</td>';
                                            echo '<td>'.date('m/d/Y', strtotime($indiutilptorow['datefrom'])).' - '.date('m/d/Y', strtotime($indiutilptorow['dateto'])).'</td>';
                                            echo '<td>'.$indiutilptorow['duration'].'</td>';


                                            if($indiutilptorow['stat'] == '3'){

                                                echo '<td> Approved </td>';


                                            }else if($indiutilptorow['stat'] == '5'){

                                                echo '<td> Cancelled </td>';


                                            }else{

                                                echo '<td> Declined </td>';

                                            }
                                            

                                            echo '</tr>';
                                            
                                            


                                        }
                                        mysqli_close($conn);
                                        ?>

                                            
                                    </tbody>
                                
                                </table>
                
                        


                        </div>

                    

            

                    

                    <span class="pull-right">
            
                
                        <button type="button" class="btn btn-danger" onclick="printContent('printAreaindi')"> <i class="fas fa-file-pdf"></i> PDF</button>
                        <button onclick="exportTableToExcel('indiptotable', 'individualutil')" class="btn btn-success"> <i class="fa fa-table"></i> Excel</button>
                    </span>
                    
                    </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



        </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>

            function printContent(el){

            var css = '@page { size: auto}',
              head = document.head || document.getElementsByTagName('head')[0],
              style = document.createElement('style');

            style.type = 'text/css';
            style.media = 'print';

            if (style.styleSheet){
            style.styleSheet.cssText = css;
            } else {
            style.appendChild(document.createTextNode(css));
            }

            head.appendChild(style);



            var restorepage = document.body.innerHTML;
            var printcontent = document.getElementById(el).innerHTML;
            document.body.innerHTML = printcontent;
            window.print();
            document.body.innerHTML = restorepage;
            }

            function doCsv(){
            var table = document.getElementById("mytable").innerHTML;
            var data = table.replace(/<thead>/g, '')
            .replace(/<\/thead>/g, '')
            .replace(/<tbody>/g, '')
            .replace(/<\/tbody>/g, '')
            .replace(/<tr>/g, '')
            .replace(/<\/tr>/g, '\r\n')
            .replace(/<th>/g, '')
            .replace(/<\/th>/g, ';')
            .replace(/<td>/g, '')
            .replace(/<\/td>/g, '')
            .replace(/\t/g, '')
            .replace(/\n/g, '');

            var mylink = document.createElement('a');
            mylink.download = "csvname.csv";
            mylink.href = "data:application/csv," + escape(data);
            mylink.click();


            }

            function exportTableToExcel(tableID, filename = ''){
              var downloadLink;
              var dataType = 'application/vnd.ms-excel';
              var tableSelect = document.getElementById(tableID);
              var tableHTML = tableSelect.outerHTML.replace(/ /g, '%20');
              
              // Specify file name
              filename = filename?filename+'.xls':'excel_data.xls';
              
              // Create download link element
              downloadLink = document.createElement("a");
              
              document.body.appendChild(downloadLink);
              
              if(navigator.msSaveOrOpenBlob){
                  var blob = new Blob(['\ufeff', tableHTML], {
                      type: dataType
                  });
                  navigator.msSaveOrOpenBlob( blob, filename);
              }else{
                  // Create a link to the file
                  downloadLink.href = 'data:' + dataType + ', ' + tableHTML;
              
                  // Setting the file name
                  downloadLink.download = filename;
                  
                  //triggering the function
                  downloadLink.click();
              }
            }

            </script>
</body>

</html>
