<?php include('../dbconnect.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;
}



   $search = $_POST['searchtxt'];
   $team = $_POST['team'];
   $status = $_POST['status'];


     
              $record_per_page = 6;
              $page = '';
              if(isset($_GET["page"]))
              {
               $page = $_GET["page"];
              }
              else
              {
               $page = 1;
              }
              
              $start_from = ($page-1)*$record_per_page;
             
              $query = "SELECT * FROM empinfo JOIN empdetails ON empinfo.empid = empdetails.detailid LIMIT $start_from, $record_per_page";

              if(isset($_POST['searchb'])){
                $query ="SELECT * FROM empinfo LEFT JOIN empdetails ON empinfo.empid = empdetails.detailid WHERE CONCAT(empinfo.empid, empinfo.surname, empinfo.firstname, empinfo.middlename,empdetails.status, empdetails.team)  LIKE '%".$search."%' LIMIT $start_from, $record_per_page";
              }      

              elseif (isset($_POST["filter"])) {


                if($team != 'All' && $status != 'All'){
                  $query = "SELECT * FROM empinfo JOIN empdetails ON empinfo.empid = empdetails.detailid  WHERE status='$status' AND team ='$team' LIMIT $start_from, $record_per_page";
                  
                }
                else if($status != 'All'){
                  $query = "SELECT * FROM empinfo JOIN empdetails ON empinfo.empid = empdetails.detailid  WHERE status='$status' LIMIT $start_from, $record_per_page";
          
              
                }
                else if($team != 'All'){
                  $query = "SELECT * FROM empinfo JOIN empdetails ON empinfo.empid = empdetails.detailid  WHERE  team='$team' LIMIT $start_from, $record_per_page";
         
                }
                else {
                  $query = "SELECT * FROM empinfo JOIN empdetails ON empinfo.empid = empdetails.detailid LIMIT $start_from, $record_per_page" ;
      
                }

                

               
             
             
               
                
              }
             
              $result = mysqli_query($conn, $query);
              $rows = mysqli_num_rows($result);
             
                $numrow = $rows;

              
              

            
     
?>
<!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>Admin | Employee</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">
	<div id="preloader">
   		<img src="../images/loaderS.gif" alt="processing..." height="150px" width="200px"/>
	</div>

  <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')?>

    <div id="wrapper">

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

        <div class="container-fluid">

          <!-- Breadcrumbs-->
          <ol class="breadcrumb">
            <li class="breadcrumb-item">
              <a href="employee.php">Employee</a>
            </li>
         
          </ol>
         
  
          <div class="row p-3" >
        

          <div class="col-sm-12" style="padding: 10px 0;">
            <form class="form-inline" method="post" enctype="multipart/form-data">
            <div class="col-sm-12">
              <h4>Employee</h4> <br>

            </div>

     
            <div class="col-sm-12">
              <button class="btn btn-primary" name="searchb" type="submit"> <i class='fas fa-search'></i> Search </button> &nbsp 

                <input type="text" class="form-control" name="searchtxt" value="<?php echo $search;?>" placeholder="Search here..."> 
           
            </div>
          
            <div class="col-sm-12" style="margin-top: 05px;">
              <button class="btn btn-primary sm" name="filter" type="submit"> <i class='fas fa-filter'></i> Filter  &nbsp </button>  &nbsp
              <?php 
                                        $qt = "SELECT * FROM team";                                                
                                        $rt = mysqli_query ($conn,$qt); 
                                        echo "<select name='team' class='form-control'>"; 
                                        echo "<option value='' disabled selected> Team </option>";
                                        echo "<option value='All'> All </option>";
                                        while($r = mysqli_fetch_array($rt)) {
                                            if($team==$r['name']){
                                                echo "<option value='".$r['name']."' selected='selected'>".$r['name']."</option>";
                                         
                                            }
                                           else{
                                            echo "<option value='".$r['name']."' >".$r['name']."</option>";
                                           }
                                            
                                        }
                                        echo "</select>"; ?>
                <?php 
                                        $qs = "SELECT * FROM empdetails GROUP BY status";                                                
                                        $rs1 = mysqli_query ($conn,$qs); 
                                        echo "<select name='status' class='form-control'>"; 
                                        echo "<option value=''  selected disabled> Status </option>";
                                        echo "<option value='All'> All </option>";
                                        while($rowstat = mysqli_fetch_array($rs1)) {
                                            if($status==$rowstat['status']){
                                                echo "<option value='".$rowstat['status']."' selected='selected'>".$rowstat['status']."</option>";
                                         
                                            }
                                           else{
                                            echo "<option value='".$rowstat['status']."' >".$rowstat['status']."</option>";
                                           }
                                            
                                        }
                                        echo "</select>"; ?>
                
                
           
            </div> 
            <div class="col-sm-12" style="margin-top: 05px;">
         

            </div>
            </form>
   
          </div>

          <table class="table table-hover">
            <thead class="thead-dark">
              <tr>
                <th scope="col">Employee ID</th>
                <th scope="col">First Name</th>
                <th scope="col">Last Name</th>
                <th scope="col">Team</th>
                <th scope="col">Status</th>
                <th scope="col">Action</th>
              </tr>
            </thead>
            <tbody>
              <?php 

               
                if (mysqli_num_rows($result) > 0) {

             
                   while($row = mysqli_fetch_assoc($result)) {
                    $pi = "style='display:block;'";
                       echo'
                       <tr>
                       <td>'.$row['empid'].'</td>
                       <td>'.$row['firstname'].'</td>
                       <td>'.$row['surname'].'</td>
                       <td>'.$row['team'].'</td>
                       <td>'.$row['status'].'</td>
                       <td> <a href="viewemp.php?ID='.$row['empid'].'&pi='.$pi.'" class="btn btn-primary"> <i class="fa fa-eye"></i> View </a>  
                       <a href="updateemp.php?ID='.$row['empid'].'&pi='.$pi.'" class="btn btn-success"> <i class="fas fa-edit"></i> Update </a> 
                       
                       </td>

                       </tr>';

                   }

                  }
                  else{
                    echo '<tr><td><h6> No Record Found</h6> <td> </tr>';
                  }
             
              ?>
        
            </tbody>
          </table>
          <nav aria-label="Page navigation example">
                  <ul class="pagination">
                    <li class="page-item">
                      <?php 
                      

                     
                        $page_query = "SELECT * FROM empinfo JOIN empdetails ON empinfo.empid = empdetails.detailid ";
               
                      
                        $page_result = mysqli_query($conn, $page_query);
                        $total_records = mysqli_num_rows($page_result);
                        $total_pages = ceil($total_records/$record_per_page);
                        $start_loop = $page;
                        $difference = $total_pages - $page;
                     

                        if($difference <= 7)
                        {
                          $start_loop = $total_pages - 5;
                        }
                        $end_loop = $start_loop + 4;
                        if($page > 1)
                        {
                          echo '<li class="page-item"><a class="page-link" href="employee.php?page=1">First</a> </li>';
                          echo '<li class="page-item"><a class="page-link" href="employee.php?page='.($page - 1).'"> « </a></li>';
                          
                        }
                        for($i=1; $i<=$end_loop; $i++)
                        {     
                          echo '<li class="page-item';
                          
                          if($_GET['page'] == $i){
                          echo ' active"> <a class="page-link" href="employee.php?page='.$i.'">'.$i.' </a> </li>  ';
                          }
                   
                          else{
                            
                            echo '<li class="page-item"></li> <a class="page-link" href="employee.php?page='.$i.'">'.$i.' </a> </li>';
                          }
                        
                   
  
            
                      
  
                          
                        }
                        if($page <= $end_loop)
                        {
                          echo '<li class="page-item"><a class="page-link" href="employee.php?page='.($page + 1).'"> » </a></li>';
                          echo '<li class="page-item"><a class="page-link"href="employee.php?page='.$total_pages.'">Last</a> </li>';
                        }
                        
                      

                     
                     
                      
                      ?>
                    </li>
                  </ul>
                </nav>

                <?php echo $nodata; ?>
          
        </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>
