<?php
$page_title = 'Edit User';
require_once('includes/load.php');
// Checkin What level user has permission to view this page
page_require_level_array([1, 2]);
?>
<?php
$e_user = find_by_id('users', (int)$_GET['id']);
$groups  = find_all('user_groups');
if (!$e_user) {
  $session->msg("d", "Missing user id.");
  redirect('users.php', false);
}
?>

<?php
//Update User basic info
if (isset($_POST['update'])) {
  // Define required fields
  $req_fields = array('name', 'username', 'level');
  validate_fields($req_fields);

  // Check if there are no errors
  if (empty($errors)) {
    // Escape and sanitize input
    $id = (int)$e_user['id'];
    $name = remove_junk($db->escape($_POST['name']));
    $username = remove_junk($db->escape($_POST['username']));
    $level = (int)$db->escape($_POST['level']);
    $status = remove_junk($db->escape($_POST['status']));

    // Prepare SQL query for updating user details
    $sql = "UPDATE users SET name ='{$name}', username ='{$username}', user_level='{$level}', status='{$status}' WHERE id='{$db->escape($id)}'";

    // Execute the query
    $result = $db->query($sql);

    // Check if the query was successful and affected rows
    if ($result && $db->affected_rows() === 1) {
      // Success: Set success message and trigger redirect using JavaScript
      $session->msg('s', "Account Updated");
      echo "<script type='text/javascript'>window.location = 'users.php';</script>";
      exit();
    } else {
      // Failure: Set failure message and trigger redirect using JavaScript
      $session->msg('d', 'Sorry, failed to update!');
      echo "<script type='text/javascript'>window.location = 'users.php';</script>";
      exit();
    }
  } else {
    // Validation errors: Set error messages and trigger redirect using JavaScript
    $session->msg("d", $errors);
    echo "<script type='text/javascript'>window.location = 'users.php';</script>";
    exit();
  }
}
?>
<?php
// Update user password
if (isset($_POST['update-pass'])) {
  $req_fields = array('password');
  validate_fields($req_fields);
  if (empty($errors)) {
    $id = (int)$e_user['id'];
    $password = remove_junk($db->escape($_POST['password']));
    $h_pass   = sha1($password);
    $sql = "UPDATE users SET password='{$h_pass}' WHERE id='{$db->escape($id)}'";
    $result = $db->query($sql);
    if ($result && $db->affected_rows() === 1) {
      $session->msg('s', "User password has been updated ");
      echo "<script type='text/javascript'>window.location = 'users.php';</script>";
    } else {
      $session->msg('d', ' Sorry failed to updated user password!');
      echo "<script type='text/javascript'>window.location = 'users.php';</script>";
    }
  } else {
    $session->msg("d", $errors);
    echo "<script type='text/javascript'>window.location = 'users.php';</script>";
  }
}

?>
<?php include_once('layouts/header.php'); ?>
<div class="row">
  <div class="col-md-12"> <?php echo display_msg($msg); ?> </div>
  <div class="col-md-6">
    <div class="panel panel-default">
      <div class="panel-heading">
        <strong>
          <span class="glyphicon glyphicon-th"></span>
          Update <?php echo remove_junk(ucwords($e_user['name'])); ?> Account
        </strong>
      </div>
      <div class="panel-body">
        <form method="post" action="edit_user.php?id=<?php echo (int)$e_user['id']; ?>" class="clearfix">
          <div class="form-group">
            <label for="name" class="control-label">Name</label>
            <input type="name" class="form-control" name="name"
              value="<?php echo remove_junk(ucwords($e_user['name'])); ?>">
          </div>
          <div class="form-group">
            <label for="username" class="control-label">Username</label>
            <input type="text" class="form-control" name="username"
              value="<?php echo remove_junk(ucwords($e_user['username'])); ?>">
          </div>
          <div class="form-group">
            <label for="level">User Role</label>
            <select class="form-control" name="level">
              <?php foreach ($groups as $group): ?>
              <option <?php if ($group['group_level'] === $e_user['user_level']) echo 'selected="selected"'; ?>
                value="<?php echo $group['group_level']; ?>"><?php echo ucwords($group['group_name']); ?></option>
              <?php endforeach; ?>
            </select>
          </div>
          <div class="form-group">
            <label for="status">Status</label>
            <select class="form-control" name="status">
              <option <?php if ($e_user['status'] === '1') echo 'selected="selected"'; ?>value="1">Active</option>
              <option <?php if ($e_user['status'] === '0') echo 'selected="selected"'; ?> value="0">Deactive</option>
            </select>
          </div>
          <div class="form-group clearfix">
            <button type="submit" name="update" class="btn btn-info">Update</button>
          </div>
        </form>
      </div>
    </div>
  </div>
  <!-- Change password form -->
  <div class="col-md-6">
    <div class="panel panel-default">
      <div class="panel-heading">
        <strong>
          <span class="glyphicon glyphicon-th"></span>
          Change <?php echo remove_junk(ucwords($e_user['name'])); ?> password
        </strong>
      </div>
      <div class="panel-body">
        <form action="edit_user.php?id=<?php echo (int)$e_user['id']; ?>" method="post" class="clearfix">
          <div class="form-group">
            <label for="password" class="control-label">Password</label>
            <input type="password" class="form-control" name="password" placeholder="Type user new password">
          </div>
          <div class="form-group clearfix">
            <button type="submit" name="update-pass" class="btn btn-danger pull-right">Change</button>
          </div>
        </form>
      </div>
    </div>
  </div>

</div>
<?php include_once('layouts/footer.php'); ?>