<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
session_start();
include_once ($_SERVER['DOCUMENT_ROOT'] . '/_config/functions.php');
spl_autoload_register('my_autoload');
$area                               = 'ADMINS';
$adminArea                          = 1;
include($_SERVER['DOCUMENT_ROOT'] . HOME_DIR . '/Auth/chk.auth.php');


$headline                           = '<h2>Benutzerdaten ändern</h2>';
$site                               = $_SERVER["HTTP_REFERER"];
$sitePrevious                       = $_SERVER["HTTP_REFERER"];

$user                               = new user();

if($_SERVER["REQUEST_METHOD"]       == "POST") {
  $userNameExist                    = $user->checkUsernameEdit($_POST['username'], $_POST['id']);
  
  if ($userNameExist                === false) {
    $errMsg                         = 'Benutzername ist bereits vergeben!';
    echo '<meta http-equiv="refresh" content="0; URL=' . HOME_DIR_HTML . TEMPLATE_DIR . '/users/error.php?err=' . urlencode($errMsg) . '">';
    exit();
  }
  
  $user->updateUserData($_POST);
  echo '<meta http-equiv="refresh" content="0; URL=' . $_POST['site'] . '">';

} else {
  $row                              = $user->getUserDataByUUID($_GET['id']);
  $dropdownUserRoles                = $user->getUserRole();
  include ($_SERVER['DOCUMENT_ROOT'] . HOME_DIR . TEMPLATE_DIR . '/users/edit.php');
}

