<?php 

include('../dbconnect.php');
include('../phps.php');
ob_start();
session_start();
$user = $_SESSION['user'];
$sqla = "SELECT * FROM account WHERE userid = '$user'";
$resulta = mysqli_query($conn, $sqla);
$rowa  = mysqli_fetch_assoc($resulta);


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


$search = phps($_POST['search']);


$search = mysqli_real_escape_string($conn, $search);
/*SELECt * FROM compensation JOIN salarypackage ON compensation.salarycode = salarypackage.salarycode 
JOIN monetary ON compensation.monetaryid = monetary.monetaryid
WHERE compensation.salarycode = salarypackage.salarycode AND compensation.monetaryid = monetary.monetaryid
*/


$w = date('W');

$week = ltrim($w, '0');
$year = date('Y');




?>
<!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 | Salary Package</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">

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>

    <link href = "../css/style.css" rel = "stylesheet">
    <script>
    function opentm(){
        document.getElementById('teamtab').style.display = 'block';
        document.getElementById('inditab').style.display = 'none';
        document.getElementById('tm').style.backgroundColor = '#dddddd';
        document.getElementById('indi').style.backgroundColor = '#f1f1f1';

    }
    function openindi(){
        document.getElementById('inditab').style.display = 'block';
        document.getElementById('teamtab').style.display = 'none';
        document.getElementById('indi').style.backgroundColor = '#dddddd';
        document.getElementById('tm').style.backgroundColor = '#f1f1f1';
    }
    </script>
 
  

  </head>

   <body id="page-top">


  <div id="pageloader">
   <img src="../images/loaderS.gif" alt="processing..." height="150px" width="200px"/>
</div>

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

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


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

        <div class="container-fluid">

          <!-- Breadcrumbs-->
          <ol class="breadcrumb">
            <li class="breadcrumb-item">
              <a href="schedule.php?page=1">Employee Schedule</a>
            </li>
           
          </ol>
          <div class="row p-3">
          <div class="col-sm-12">
            <h4>Employee Schedule</h4>
          </div>
          <div class="col-sm-12 form-inline" >
              <a href="addsched.php?show=style='display: none;'" class="btn btn-success" role="button"><i class="fa fa-plus"></i> Add  </a> <br> &nbsp
              <a href="updatesched.php?teamtab=style='display:block;'" class="btn btn-warning" role="button" style="color: white;"><i class="fa fa-edit"></i> Update  </a> <br> &nbsp
              <a href="viewsched.php" class="btn btn-primary" role="button"><i class="fa fa-eye"></i> View  </a> <br> &nbsp
          </div>
          <!--
          <div class="col-sm-12 form-inline" >
          <form method="POST">
          <button class="btn btn-primary" type="submit"><i class="fas fa-search" name="seachb"> </i> Search </button> &nbsp
          <input type="text" class="form-control"  placeholder="Search here...">
          </form>
          </div> -->
          <div class="col-sm-12 form-group" >

          </div>
      
                <table class="table table-bordered" id="myTable">
                  <thead>
                  <tr class="thead-dark">
                    <th style="vertical-align:middle;">Employee ID</th>
                    <th style="vertical-align:middle;">Employee Name</th>
                    <?php 
                    $getemp = "SELECT * FROM schedule WHERE week='$week'  AND year='$year' AND indi='0'  LIMIT 1";
                    $getempresult = mysqli_query($conn,$getemp);
  
                    $getemprow = mysqli_fetch_assoc($getempresult);
  
                    $firstemp = $getemprow['empid'];
  
                   
                    $query = "SELECT * FROM schedule WHERE week='$week'   AND year='$year' AND empid='$firstemp'  ORDER BY FIELD(day, 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday') LIMIT 7";
                    $result = mysqli_query($conn,$query);

                    if (mysqli_num_rows($result) > 0) {
                          
    
                     
                      while($row = mysqli_fetch_assoc($result)) {
                       
                            echo '<th style="text-align: Center">'.$row['day'].'<br/>'.date('m/d/Y', strtotime($row['date'])).' </th> ';
                           
                          

                      }
                
    
                    }
                    else{
                      echo '<tr><td><h6> No schedule added for the current week </h6> <td> </tr>';
                    }
  
                    
                    
                    ?>
                  </tr>
                  </thead>
                  <tbody>
                  <tr>
                    <?php 
                    $getemp = "SELECT * FROM schedule WHERE week='$week'  AND year='$year' GROUP BY empid"; //GET EMPLOYEE 
                    $getempresult = mysqli_query($conn,$getemp);
    
                    $getemprow = mysqli_num_rows($getempresult);
    
    
                    $empid = array();
    
                                    
                    while($r = mysqli_fetch_assoc($getempresult)) {
                        
                        $empid[] = $r['empid'];
                  
                                  
                    }

                    
                   for($i= 0 ; $i < $getemprow; $i++ ){
                    //GET ALL SCHEDULE OF EMPLOYEE
                   $query = "SELECT * FROM schedule WHERE  year='$year' AND empid='$empid[$i]' AND week='$week' ORDER BY FIELD(day, 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday')";

                   $result = mysqli_query($conn,$query);

                   //GET EMPLOYEE NAME 
                   $empname = "SELECT * FROM empinfo JOIN empdetails ON empinfo.empid = empdetails.detailid WHERE empinfo.empid='$empid[$i]'";
                   $resultname = mysqli_query($conn,$empname);

                   $empnamerow = mysqli_fetch_assoc($resultname);
                   if (mysqli_num_rows($result) > 0) {
                    if ($empnamerow['status'] == 'Inactive' ){


                    }else{
                      

                    echo '<tr>';
                    echo '<td><strong>'.$empnamerow['empid'].'</strong></td>';
                    echo '<td><strong>'.$empnamerow['firstname'].' '.$empnamerow['surname'].'</strong></td>';
                    }
                    while($row = mysqli_fetch_assoc($result)) {

                      if ($empnamerow['status'] == 'Inactive' ){
             

                      }else{


                        
                        if($row['starttime'] == 'OFF'){
                          echo ' <td style="text-align:center">';
                          echo ' <span class="text-danger">OFF </span> </td>';
                          }else if ($row['starttime'] == 'HOLIDAY OFF') {
                          echo ' <td style="text-align:center">';
                          echo ' <span class="text-danger">HOLIDAY OFF </span> </td>';
                          }
                          else if ($row['idl'] != '') {
                            $idl = $row['idl'];


                            $leavequery = "SELECT * FROM `leave` WHERE idl='$idl' AND status='3' ";
                            $leaveres = mysqli_query($conn, $leavequery);
                            $leaverow = mysqli_fetch_assoc($leaveres);
                            $duration = $leaverow['duration'];

                            $dur = substr($duration, -2);
                          
                            if(mysqli_num_rows($leaveres) > 0){
                              
                    
                              if( $row['leaveduration'] == '0.5' ){

                                echo ' <td style="text-align:center">';
                                echo ' <span class="text-danger">'.$leaverow['name'].' Halfday</span> </td>';

                              }else{

                                echo ' <td style="text-align:center">';
                                echo ' <span class="text-danger">'.$leaverow['name'].'  </span> </td>';



                              }

                            





                            }
                            else{

                              
                              echo '<td style="text-align:center">';
                              $time= date("g:i A", strtotime($row['starttime'])) ;
                              echo $time;
                              echo '</td>';


                            }


                              

                            

                            


                          }

                          
                          else{
                            echo '<td style="text-align:center">';
                            $time= date("g:i A", strtotime($row['starttime'])) ;
                            echo $time;
                            echo '</td>';
                          }
    

                      

                          }



                        }
                          echo '</tr>';
                        }
    
      
      
      
                          
      
      
                        }

    
                    ?>
                  </tr>
                  </tbody>
                </table>
    
                <script>
                // get the table element
                var $table = document.getElementById("myTable"),
                // number of rows per page
                $n = 10,
                // number of rows of the table
                $rowCount = $table.rows.length,
                // get the first cell's tag name (in the first row)
                $firstRow = $table.rows[0].firstElementChild.tagName,
                // boolean var to check if table has a head row
                $hasHead = ($firstRow === "TH"),
                // an array to hold each row
                $tr = [],
                // loop counters, to start count from rows[1] (2nd row) if the first row has a head tag
                $i,$ii,$j = ($hasHead)?1:0,
                // holds the first row if it has a (<TH>) & nothing if (<TD>)
                $th = ($hasHead?$table.rows[(0)].outerHTML:"");
                // count the number of pages
                var $pageCount = Math.ceil($rowCount / $n);
                // if we had one page only, then we have nothing to do ..
                if ($pageCount > 1) {
                  // assign each row outHTML (tag name & innerHTML) to the array
                  for ($i = $j,$ii = 0; $i < $rowCount; $i++, $ii++)
                    $tr[$ii] = $table.rows[$i].outerHTML;
                  // create a div block to hold the buttons
                  $table.insertAdjacentHTML("afterend","<div id='buttons'></div");
                  // the first sort, default page is the first one
                  sort(1);
                }

                // ($p) is the selected page number. it will be generated when a user clicks a button
                function sort($p) {
                  /* create ($rows) a variable to hold the group of rows
                  ** to be displayed on the selected page,
                  ** ($s) the start point .. the first row in each page, Do The Math
                  */
                  var $rows = $th,$s = (($n * $p)-$n);
                  for ($i = $s; $i < ($s+$n) && $i < $tr.length; $i++)
                    $rows += $tr[$i];
                  
                  // now the table has a processed group of rows ..
                  $table.innerHTML = $rows;
                  // create the pagination buttons
                  document.getElementById("buttons").innerHTML = pageButtons($pageCount,$p);
                  // CSS Stuff
                  document.getElementById("id"+$p).setAttribute("class","btn btn-primary btn-sm active");
                }


                // ($pCount) : number of pages,($cur) : current page, the selected one ..
                function pageButtons($pCount,$cur) {
                  /* this variables will disable the "Prev" button on 1st page
                    and "next" button on the last one */
                  var	$prevDis = ($cur == 1)?"disabled":"",
                    $nextDis = ($cur == $pCount)?"disabled":"",
                    /* this ($buttons) will hold every single button needed
                    ** it will creates each button and sets the onclick attribute
                    ** to the "sort" function with a special ($p) number..
                    */
                    $buttons = "<input type='button' class='btn btn-default btn-sm text-primary' value='« Prev' onclick='sort("+($cur - 1)+")' "+$prevDis+"> &nbsp";
                  for ($i=1; $i<=$pCount;$i++)
                    $buttons += "<input type='button' class='btn btn-default btn-sm text-primary' id='id"+$i+"'value='"+$i+"' onclick='sort("+$i+")'> &nbsp";
                  $buttons += "<input type='button' class='btn btn-default btn-sm text-primary' value='Next »' onclick='sort("+($cur + 1)+")' "+$nextDis+"> &nbsp";
                  return $buttons;
                }
                </script>
            

         

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