<?php 

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

if( !isset($_SESSION['user']) ) {
 header("Location: ../index.php");
 exit;
}
$idsql = "SELECT * FROM individualshared";
$result = mysqli_query($conn, $idsql);
$numrow = mysqli_num_rows($result);

if(isset($_POST['addindivid'])){
  $id = $_POST['id'];
  $name = $_POST['name'];
  $description = $_POST['description'];
  $type = $_POST['type'];
  $uname = $_POST['username'];
  $password = $_POST['password'];

  // DUPLICATE VALIDATION//////////////////
  $vvq = "SELECT * FROM individualshared WHERE name = '$name' AND description = '$description'";
  $resultvv = mysqli_query($conn, $vvq);
  if (mysqli_num_rows($resultvv) > 0) {

    $alert = '<div class="alert alert-danger">Name and description exist </div>';

  }else{
    $insertq = "INSERT INTO `individualshared`(`name`, `description`, `username`, `password`, `type`, `empid`) VALUES ('$name','$description','$uname','$password','$type',NULL)";

    if (mysqli_query($conn, $insertq)) {
      
      
        $alert = '<div class="alert alert-success">Successfully Added </div>';
        header("location: tools.php?alert=$alert");
       
    
      
    }
    else{
      echo "Error updating record: " . mysqli_error($conn);
  } 



  }




}

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



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

        <div class="container-fluid">

          <!-- Breadcrumbs-->
          <ol class="breadcrumb">
            <li class="breadcrumb-item">
              <a href="#">Employee Tools</a>
            </li>
          
          </ol>
    

            <div class="row justify-content-lg-center" style="padding: 10px 100px;" >
              <form method="post" enctype="multipart/form-data">  
              <div class="row">
            

              
                          <div class="col-sm-12 form-group required">
                          <?php echo $alert; echo $_GET['alert'];?>
                          <h4>Employee Tools </h4>
                          </div>
                          <div class="col-sm-1 form-group required">
                              <label class="control-label">ID</label> <br>
                              <input type="text"  class="form-control" name="id"  id="indid" placeholder="ID" value="<?php echo $id?>" required disabled>
                          </div>
                          <div class="col-sm-2 form-group required">
                              <label class="control-label">Name</label> <br>
                              <input type="text" class="form-control" name="name"  placeholder="Name" value="<?php echo $name?>" required >
                          </div>
                          <div class="col-sm-3 form-group required">
                              <label class="control-label">Description</label> <br>
                              <input type="text" class="form-control" name="description"  placeholder="Description"  value="<?php echo $description?>" required >
                          </div>
                          <div class="col-sm-2 form-group required">
                          <label class="control-label">Type</label> <br>
                            <?php  
                            switch($type){
                              case 'Individual':
                               $indi = "selected";
                               break;
                               case 'Shared':
                               $shared = "selected";
                               break; 
                            }
                            ?>
                            <select name="type" class="form-control" required>
                            <option selected>Type</option>
                              <option value="Individual" <?php echo $indi?>>Individual</option>
                              <option value="Shared" <?php echo $shared?>>Shared</option>
                            </select>
                          </div>
                          <div class="col-sm-2 form-group required">
                              <label class="control-label">Username</label> <br>
                              <input type="text" class="form-control" name="username"  placeholder="Username"  value="<?php echo $uname?>" required >
                              
                          </div>
                          <div class="col-sm-2 form-group required">
                              <label class="control-label">Password</label> <br>
                              <input type="text" class="form-control" name="password"  placeholder="Password" value="<?php echo $password?>"   required >
                          </div>


                          <div class="col-sm-12 form-group">
                            <div class="row justify-content-end">
                              <button type="button" class="btn btn-success" data-toggle="modal" data-target="#addindividual"> <i class="fa fa-check"></i> Save </button> &nbsp &nbsp 
                              <button type="reset"   class="btn btn-danger"  required> <i class="fa fa-close"></i> Cancel </button>
                            </div>
          
                          </div>

                          <div class="modal fade" id="addindividual" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
                            <div class="modal-dialog modal-dialog-centered" role="document">
                              <div class="modal-content">
                                  <div class="modal-header">
                                    <h5 class="modal-title" id="exampleModalLabel">Confirm Individual / Shared</h5>
                                    <button class="close" type="button" data-dismiss="modal" aria-label="Close">
                                    <span aria-hidden="true">×</span>
                                    </button>
                                  </div>
                                  <div class="modal-body">Are you sure you want to add individual / shared</div>
                                    <div class="modal-footer">
                                      <input type="submit" class="btn btn-success" name="addindivid" value=" ✓ Yes" style="margin-right: 5px;" > 
                                      <button class="btn btn-danger" type="button" data-dismiss="modal"><i class="fa fa-close"></i> No</button>
                                                      
                                  </div>
                              </div>
                            </div>
                          </div>
                          </div>
                          </form>
                       
                          <div class="col-sm-12" >
                            <table class="table table-hover">
                              <thead class="thead-dark">
                                <tr>
                                  
                                  <th scope="col">ID</th>
                                  <th scope="col">Name</th>
                                  <th scope="col">Description</th>
                                  <th scope="col">Individual / Shared </th>
                                  <th scope="col">Username</th>
                                  <th scope="col">Password</th>
                                  <th scope="col">Action</th>
                        
                                  
                                </tr>
                              </thead>
                              <tbody>

                              <?php 
                            
                            $record_per_page = 8;
                            $page = '';
                            if(isset($_GET["page"]))
                            {
                            $page = $_GET["page"];
                            }
                            else
                            {
                            $page = 1;
                            }
                            
                            $start_from = ($page-1)*$record_per_page;
                            
                            $query = "SELECT * FROM individualshared LIMIT $start_from, $record_per_page";


                            $result = mysqli_query($conn, $query);
                            
                            ?>
                            
                            
                              <?php 
                              
                        
                              if (mysqli_num_rows($result) > 0) {
                                  while($row = mysqli_fetch_assoc($result)) {
                                      echo'
                                      <tr>
                                      <td>'.$row['id'].'</td>
                                      <td>'.$row['name'].'</td>
                                      <td>'.$row['description'].'</td>
                                      <td>'.$row['type'].'</td>
                                      <td>'.$row['username'].'</td>
                                      <td>'.$row['password'].'</td>
                                      <td><a href=updatetools.php?ID='.$row['id'].' class="btn btn-primary"> <i class="fas fa-edit"></i>  Update </td>
                                    
                                      
                                      </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 individualshared";
               
                      
                      $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="tools.php?page=1">First</a> </li>';
                        echo '<li class="page-item"><a class="page-link" href="tools.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="tools.php?page='.$i.'">'.$i.' </a> </li>  ';
                        }
                 
                        else{

                          echo '<li class="page-item"></li> <a class="page-link" href="tools.php?page='.$i.'">'.$i.' </a> </li>';
                        }
                      
                 

          
                    
                      }
                      if($page <= $end_loop)
                      {
                        echo '<li class="page-item"><a class="page-link" href="tools.php?page='.($page + 1).'"> » </a></li>';
                        echo '<li class="page-item"><a class="page-link"href="tools.php?page='.$total_pages.'">Last</a> </li>';
                      }
                      
                      
                      ?>
                    </li>
                  </ul>
                </nav>
                          </div>



    
    
            </div>
            
        
          <script>

            document.getElementById("indid").value = "<?php echo $numrow + 1; ?>";


    
          </script>
         
          

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