<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>NGE EMS | Add User</title>
    
  <?php
  include 'header.php'; //Pages Head
  include 'navbar.php'; //Main Sidebar Container
  ?>
  <div class="content-wrapper">
          <!-- Content Header (Page header) -->
    <div class="content-header">
      <div class="container-fluid">
        <div class="row mb-2">
          <div class="col-sm-6">
            <h1 class="m-0">Add User</h1>
          </div><!-- /.col -->
          <div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
              <li class="breadcrumb-item"><a href="#">Home</a></li>
              <li class="breadcrumb-item active">Add User</li>
            </ol>
          </div><!-- /.col -->
        </div><!-- /.row -->
      </div><!-- /.container-fluid -->
    </div>
    <section class="content">
      <div class="container-fluid register-box col-8">
      <div class="card card-outline card-primary">
        <div class="card-header text-center">
        <a href="./index.php" class="h1"><b>NGE | </b>EMS</a>
        </div>
        <div class="card-body">
          <p class="login-box-msg">Register a new membership</p>

          <form action="" method="post" name="newUserForm">
            <div class="input-group mb-3">
              <input type="text" id="name" name="name" class="form-control required" placeholder="Full name">
              <div class="input-group-append">
                <div class="input-group-text">
                  <span class="fas fa-user"></span>
                </div>
              </div>
            </div>
            <div class="input-group mb-3">
              <input type="email" id="emailAddress" name="email" class="form-control required" placeholder="Email">
              <div class="input-group-append">
                <div class="input-group-text">
                  <span class="fas fa-envelope"></span>
                </div>
              </div>
            </div>
            <div class="input-group mb-3">
              <input type="password" id="password" name="password" class="form-control" placeholder="Password">
              <div class="input-group-append">
                <div class="input-group-text">
                  <span class="fas fa-lock"></span>
                </div>
              </div>
            </div>
            <div>
                <button type="submit" name="submit" class="btn btn-primary btn-block">Add</button>
            </div>
            <script src="//cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<?php
if(isset($_POST["submit"])) {    
    $user_name = $_POST['name'];
    $user_email = $_POST['email'];
    $user_pass = $_POST['password'];
    //$user_pass = password_hash($user_pass, PASSWORD_DEFAULT);
    // if(password_verify($password, $hashed_password)) { } // to check in login form
    if(empty($user_name) || empty($user_email) || empty($user_pass)) {                
            echo "<script type='text/javascript'>
            var toastMixin = Swal.mixin({
                toast: true,
                icon: 'success',
                title: 'General Title',
                animation: false,
                position: 'top-right',
                showConfirmButton: false,
                timer: 3000,
                timerProgressBar: true,
                didOpen: (toast) => {
                  toast.addEventListener('mouseenter', Swal.stopTimer)
                  toast.addEventListener('mouseleave', Swal.resumeTimer)
                }
              });
              toastMixin.fire({
                title: 'Bad entry, Please fill all fields',
                icon: 'error'
              });
                </script>";
    } else { 
$sql = "INSERT INTO users (user_name, user_pass, user_email, user_role) VALUES ('".$user_name."',password('".$user_pass."'),'".$user_email."', 2)";

//$statusMsg = '';

if ($db->query($sql) === TRUE) {
    echo "<script type='text/javascript'>
            var toastMixin = Swal.mixin({
                toast: true,
                icon: 'success',
                title: 'General Title',
                animation: false,
                position: 'top-right',
                showConfirmButton: false,
                timer: 3000,
                timerProgressBar: true,
                didOpen: (toast) => {
                  toast.addEventListener('mouseenter', Swal.stopTimer)
                  toast.addEventListener('mouseleave', Swal.resumeTimer)
                }
              });
              toastMixin.fire({
                animation: true,
                title: 'User Added Successfully'
              });
        </script>";
} else {
//else
}}};

$db->close(); 
?>
          </form>


        </div>
        <!-- /.form-box -->
      </div><!-- /.card -->
    </div>
    <!-- /.register-box -->
  </div>

<?php
  include 'footer.php'; //Pages footer
?>
