<?php 
include('../dbconnect.php');
include('../phps.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 = phps($_POST['id']);
$name = phps($_POST['name']);
$description = phps($_POST['description']);
$type = phps($_POST['type']);


$id = mysqli_real_escape_string($conn, $id );
$name = mysqli_real_escape_string($conn, $name);
$description = mysqli_real_escape_string($conn, $description);
$type = mysqli_real_escape_string($conn, $type);


// 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`, `type`) VALUES ('$name','$description','$type')";
if (mysqli_query($conn, $insertq)) {
$alert = '<div class="alert alert-success">Successfully Added </div>';
header("location: addtools.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>


    
    <?php include('uppernav.php')?>
    <!-- Sidebar -->
    <?php 
$tools = "active";
include('sidebar.php');
$dashboard="active"; ?>
      <form method="POST">
    <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 p-3" >
          <div class="col-sm-12" >
            <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-3 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-4 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 inline">
        <br>
        <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 class="col-sm-12" >
        <table class="table table-hover" id="toolsTable">
          <thead class="thead-dark">
            <tr class="thead-dark">
              <th scope="col">ID
              </th>
              <th scope="col">Name
              </th>
              <th scope="col">Description
              </th>
              <th scope="col">Type
              </th>
            </tr>
          </thead>
          <tbody>
            <?php 
$query = "SELECT * FROM individualshared";
$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>
</tr>';
}
}
else{
echo '<tr><td><h6> No Record Found</h6> <td> </tr>';
}
mysqli_close($conn);
?>
          </tbody>
        </table>
      </div>
    </div>
    <script>
      document.getElementById("indid").value = "<?php echo $numrow + 1; ?>";
    </script>
    <script>
      // get the table element
      var $table = document.getElementById("toolsTable"),
          // number of rows per page
          $n = 8,
          // 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>
    <!-- Icon Cards-->
    <!-- /.container-fluid -->
    <!-- Sticky Footer -->
    <footer class="sticky-footer">
      <div class="container my-auto">
        <div class="copyright text-center my-auto">
          <span>Copyright © Your Website 2018
          </span>
        </div>
      </div>
    </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>
<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>
</form>
<!-- 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>
