<?php
require_once '../auth/auth.php';
require_once '../models/db_connection.php';
require_once '../models/user_model.php';
require_once '../models/log_model.php';
$log = new Log($conn);
$user = new User($conn);
if (isset($_GET['id'])) {
  $userData = $user->getUserById($_GET['id']);
} else {
  $userData = $user->getUserById($_SESSION['user']);
}

$userData = $userData[0];
if (isset($_POST['editUsernmae'])) {
  $username =   $_POST['username'];
  $email = $_POST['email'];
  $pass =   $_POST['password'];

  if (isset($_POST['id'])) {
    $success = $user->editUser($username, $email, $pass, $_POST['id']);
    $logged = $log->createLog($user_id, $activity);
    header("Location: all_users.php?message=User edited successfully");
    exit(); // Stop further execution
  } else {
    $success = $user->editUser($username,$email, $pass, $_SESSION['user']);
     $logged = $log->createLog($user_id, $activity);
    header("Location: user_profile.php?message=User edited successfully");
    exit(); // Stop further execution
  }
}
?>
<!DOCTYPE html>
<html lang="en">

<head>
  <title>Exam | Add Examiniation </title>
  <?php include("../templates/header.php"); ?>
</head>

<body>
  <?php
  if (isset($_GET["message"])) {
    $message = $_GET["message"];
    echo "<script>alert('" . $message . "')</script>";
  }
  ?>
  <div class="container-scroller">
    <!-- partial:partials/_navbar.html -->
    <?php include("../templates/nav.php"); ?>
    <!-- partial -->
    <div class="container-fluid page-body-wrapper">
      <!-- partial:partials/_sidebar.html -->
      <?php include("../templates/sidenav.php"); ?>
      <!-- partial -->
      <div class="main-panel">
        <div class="content-wrapper">
          <form action="user_profile.php" method="POST">
            <div class="col-md-6 grid-margin stretch-card">

              <div class="card">
                <div class="card-body">
                  <h4 class="card-title">User Profile</h4>

                  <div>
                    <p><b>Username:</b> <?= $userData['username'] ?></p>
                    <p><b>Role:</b> <?= $userData['role'] ?></p>
                  </div>
                  <hr>
                  <p class="card-description">
                    Edit Username
                  </p>
                  <div class="form-group">
                    <label for="exampleFormControlSelect2">Username</label>

                    <input name="username" required class="form-control" type="text">
                    <label for="exampleFormControlSelect2">Email</label>

                    <input name="email" required class="form-control" type="email">
                    <label for="exampleFormControlSelect2">Password</label>
                    <input name="password" required class="form-control" type="password">

                    <input name="id" type="hidden" value="<?php echo ($_GET['id']); ?>">
                    <div class="m-4"></div>
                    <button type="submit" class="btn btn-dark btn-lg btn-block"" name=" editUsernmae">Edit</button>
                  </div>
                </div>
              </div>
            </div>

        </div>
        </form>
        <!-- content-wrapper ends -->
        <!-- partial:partials/_footer.html -->
        <?php include("../templates/footer.php"); ?>

        <!-- partial -->
      </div>
      <!-- main-panel ends -->
    </div>
    <!-- page-body-wrapper ends -->
  </div>
  <?php include("../templates/inc_scripts.php"); ?>
</body>

</html>