<?php
include "../../classes/autoload.php";
$loader = new loading();

$loader->loading_dots();
if (strlen($_POST["password"]) >= 1) {
    $password = hash("sha256", $_POST["password"]);
    $db->query("users", "UPDATE `users` SET `username` = '{$_POST["username"]}', `email` = '{$_POST["email"]}', `password` = '{$password}', `type` = '{$_POST["user_type"]}', `initials` = '{$_POST["initials"]}',`status` = {$_POST["status"]} WHERE `record_id` = {$_POST["record_id"]}");
    if ($_POST['record_id'] == $_SESSION['user_id']) {
        ?>
        <script>
            // ajax to logout
            logout();
            function logout() {
                var xmlhttp = new XMLHttpRequest();
                xmlhttp.open("GET", "../../logout.php", true);
                xmlhttp.send();
            }
        </script>
        <?php
    }

} else {
    $db->query("users", "UPDATE `users` SET `username` = '{$_POST["username"]}', `email` = '{$_POST["email"]}' ,`type` = '{$_POST["user_type"]}', `initials` = '{$_POST["initials"]}',`status` = {$_POST["status"]} WHERE `record_id` = {$_POST["record_id"]}");
}

echo "<script>window.location.href='home.php';</script>";