<?php
   include('session.php');
   include('header1.php');
   include('ietpartial.php');
   include('calculations.php');

// start role specific 
$user_role = $_SESSION['role'];

if(isset($_GET['updated'])) {

      $updated = $_GET['updated'];

}


    $sqlView = "SELECT 
          users.userid AS userid,
          users.username AS username,
          users.enabled AS enabled,
          roles.rolename AS role
          FROM users
          INNER JOIN roles
          ON roles.roleid = users.role;";

  
    $result = mysqli_query($db,$sqlView);

    $count = mysqli_num_rows($result);



    echo "<main role=\"main\" class=\"col-md-9 ml-sm-auto col-lg-10 px-4\">";

    echo "<div class=\"d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom\">";

    echo "<h1 class=\"h2\">Users</h1>";

      echo "<div class=\"btn-toolbar mb-2 mb-md-0\">"; // TOOLBAR STARTS HERE

            echo "<div class=\"btn-group mr-2\">";
                echo "<a href=\"addUser.php\" class=\"btn btn-sm btn-outline-secondary\" role=\"button\">Add User</a>";
                echo "<a href=\"technicalsettings.php\" class=\"btn btn-sm btn-outline-secondary\" role=\"button\">Back</a>";
            echo "</div>";

          echo "</div>";

    echo "</div>";

    if(isset($updated)) {

      switch ($updated) {
        case 1:
            echo "<div class=\"alert alert-primary\" role=\"alert\" >User Added!</div>";
            break;
        case 2:
            echo "<div class=\"alert alert-primary\" role=\"alert\" >User Password Updated!</div>";
            break;
        case 3:
            echo "<div class=\"alert alert-danger\" role=\"alert\" >User Revoked!</div>";
            break;
      }



}


    


    if ($count !== 0) {


 
    // TABLE STARTS HERE

  echo "<table class = \"table\">";

  echo "<thead>";
  echo "<td>Username</td>";
  echo "<td>Role</td>";
  echo "<td>Password</td>";
  echo "<td>Delete</td>";
  echo "</thead>";
  echo "<tbody>";

  while ($rowuser = mysqli_fetch_array($result)) { // FETCHING  DATA START
    if ($rowuser['username'] != 'irshaad') {
    echo "<tr>";
    echo "<td>" . $rowuser['username'] . "</td>";
    echo "<td>" . $rowuser['role'] . "</td>";
    echo "<td><a href = \"changePassword.php?userid=" . $rowuser['userid'] . "\">Change Password</a></td>";

    if ($rowuser['enabled'] == 1 && $rowuser['username'] != 'irshaad') {

      echo "<td><a href = \"deleteUser.php?userid=" . $rowuser['userid'] . "\">Revoke User</a></td>";
    } else {
      if ($rowuser['username'] != 'irshaad'){ 
      echo "<td>User Revoked</td>";
    } else {
      echo "<td></td>";
    }
    }
    

   
    echo "</tr>";

}

  } // FETCHING  DATA END

  echo "</tbody>";
  echo "</table>";

    // TABLE ENDS HERE


    } // COUNT EMPLOYEES END HERE
    else 
    {

      echo "Audit Log empty..";
    }



include('footer1.php');

?>