<?php 

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

if( !isset($_SESSION['user']) ) {
 header("Location: ../index.php");
 exit;
}

$getuserid = $_GET['userid'];
$getsql = "SELECT * FROM account WHERE userid='$getuserid'";
$getresult = mysqli_query($conn, $getsql);
$getrows = mysqli_fetch_assoc($getresult);
$getusername = $getrows['userid'];
$getlevel = $getrows['level'];
$getpass =  $getrows['password'];
$getstatus = $getrows['status'];

if(isset($_POST['saveuser'])){
$userid = $_POST['username'];
$level = $_POST['level'];
$password = $_POST['pass'];
$retype = $_POST['retypepassword'];
$status = $_POST['status'];
$psw= hash('sha256', $password);
$checkuserid = "SELECT * FROM account WHERE  userid = '$username'";
$userresult = mysqli_query($conn, $checkuserid);


    $usersql = "UPDATE `account` SET `level`='$level',`status`='$status' WHERE userid = '$getuserid'";

if (mysqli_query($conn, $usersql)) {
  
  
    $alert = '<div class="alert alert-success">User successfully save</div>';
    header("location: updateuser.php?userid=$getuserid&alert=$alert");
   

  
} 





}

if(isset($_POST['resetpass'])){

  $resetpass = hash('sha256', $getuserid);

  echo $usersql = "UPDATE `account` SET  password = '$resetpass' WHERE userid = '$getuserid'";

  if (mysqli_query($conn, $usersql)) {
    
    
      $alert = '<div class="alert alert-success">User password successfully reset</div>';
      header("location: updateuser.php?userid=$getuserid&alert=$alert");
     
  
    
  } 



  

}
mysqli_close($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 
     $user = "active";
     $admintools = "active";
      include('sidebar.php');
      ?>
      <div id="content-wrapper">

        <div class="container-fluid">

          <!-- Breadcrumbs-->
          <ol class="breadcrumb">
            <li class="breadcrumb-item" >
              <a href="user.php">Users</a>
            </li>
            <li class="breadcrumb-item active">Update User</li>

          </ol>
        <form method="post" enctype="multipart/form-data">  
        <div class="row p-3" >
        
            <div class="col-sm-12">
                <?php echo $_GET['alert']; echo $alert; ?>
            </div>
            <div class="col-sm-12">
            
                <h4>Update User</h4>
                <button type="button" class="btn btn-primary float-right" data-toggle="modal" data-target="#resetpass"><i class="fas fa-lock"></i>  Reset Password</button>  &nbsp
            </div>
            <div class="col-sm-6">
                <label class="control-label">Username </label> <br>
                <input type="text"  name="username" class="form-control" placeholder="Username" value="<?php echo $getusername ?>" required readonly>
            </div>
            <div class="col-sm-6">
                <label class="control-label">Password </label> <br>
                <input type="password" name="pass" class="form-control" placeholder="Password" value="<?php echo $getpass; ?>" pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}" title="Must contain at least one number and one uppercase and lowercase letter, and at least 8 or more characters" required readonly>
            </div>
            <div class="col-sm-6">
                <label class="control-label">Level</label> <br>
                <select name="level" class="form-control">
                <?php 
                switch($getlevel){
                    case 'super admin' :
                    $sa = 'selected';
                    break;
                    case 'admin':
                    $ad = 'selected';
                    break;
                    case 'user':
                    $us = 'selected';
                    break;
                    case 'team leader':
                    $tl = 'selected';
                    break;
                    case 'Human Resource':
                    $hr =  'selected';
                    break;
                    case 'Senior Human Resource':
                    $shr = 'selected';
                    break;
                    case 'Manager':
                    $m = 'selected';
                    break;
                    case 'Senior Manager':
                    $srm = 'selected';
                    break;


                }
                
                ?>
                    <option value="" disabled selected> Level </option>
                    <option value="super admin" <?php echo $sa?>>Super Admin</option>
                    <option value="admin" <?php echo $ad ?>>Admin</option>
                    <option value="user" <?php echo $us?>>User</option>
                    <option value="team leader" <?php echo $tl?>>Team Leader</option>
                    
                    <option value="Human Resource" <?php echo $hr?>>Human Resource</option>
                    <option value="Senior Human Resource" <?php echo $shr?>> Senior Human Resource</option>
                    <option value="Manager" <?php echo $m?>>Manager</option>
                    <option value="Senior Manager" <?php echo $srm?>>Senior Manager</option>
                </select>
            </div>
            
        
            <div class="col-sm-6">
                <label class="control-label">Status</label> <br>
                <select name="status" class="form-control" required>
                <?php 
                switch($getstatus){
                    case 'active' :
                    $ac = 'selected';
                    break;
                    case 'inactive':
                    $in = 'selected';
                    break;
                 
                }
                
                ?>
                    <option value="" disabled selected> Status </option>
                    <option value="active" <?php echo $ac?>>Active</option>
                    <option value="inactive" <?php echo $in?>>Inactive</option>
             
                </select>
            </div>
            <div class="col-sm-12 form-group" style="padding: 10px 18px;">
                <div class="row justify-content-end" >


                    <button type="button" class="btn btn-success" data-toggle="modal" onclick="view()" data-target="#adduser"><i class="fas fa-check"></i>  Submit</button>  &nbsp
                        
                    
                      
                    <a href="user.php" class="btn btn-danger" role="button" style="margin-right: 5px;"><i class="fas fa-close"></i> Cancel </a>
                         
                </div>
            </div>

        
        </div>
        
    <!-- Submit modal -->
        <div class="modal fade" id="adduser" 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 User Update</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 Update</div>
            <div class="modal-footer">
                <button type ="submit" class="btn btn-success"  name="saveuser"> <i class="fa fa-check"></i> Yes</button>
                <button class="btn btn-danger" type="button" data-dismiss="modal" aria-label="Close"> <i class="fa fa-close"></i> No </button>
            </div>
            </div>
        </div>
        </div>


        <div class="modal fade" id="resetpass" 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">Reset User Password</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 reset <?php echo $getusername ?> password?</div>
            <div class="modal-footer">
                <button type ="submit" class="btn btn-success"  name="resetpass"> <i class="fa fa-check"></i> Yes</button>
                <button class="btn btn-danger" type="button" data-dismiss="modal" aria-label="Close"> <i class="fa fa-close"></i> No </button>
            </div>
            </div>
        </div>
        </div>

        </form>
          
           

          

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