<?php

if (isset($_SESSION['user_id'])) {
    if (isset($_POST["delluser"])) {
        $username = "'" . $_POST["username"] . "'";
        if ($user_data['rola'] == "admin" && $user_data["name"] != $_POST["username"]) {
            $sql = "SELECT * FROM `users` WHERE `name`=$username && `active`!=1  && `id`!='1' limit 1";
            $iflogin = mysqli_query($con, $sql);
            if (mysqli_num_rows($iflogin) != 1) {
                $id = 1;
            } else {
                $sql = "DELETE FROM `users` WHERE `name`=$username && `active`!=1  && `id`!='1'";
                $id = 0;
            }
        } else {
            $id = "'" . $user_data['user_id'] . "'";
            $sql = "DELETE FROM `users` WHERE `name`=$username && `user_id` =$id  && `id`!='1'";
            $id = $user_data['user_id'];
        }
        $username = $_POST["username"];
        try {
            if ($id == 1) {
                echo <<<script
                <script> type ="text/JavaScript">
                    alert(" użytkownik jest zalogowany wyloguj $username i spróbuj ponownie"); window.location = '../admin/usermenager.php';
                </script>
                script;
            } else {
                mysqli_query($con, $sql);
                mysqli_close($con);
                if ($user_data["name"] == $username && $user_data["user_id"] == $id) {
                    unset($_SESSION['user_id']);
                    header('Location: ../other/index.php');
                } else {
                    echo <<<script
                <script> type ="text/JavaScript">
                    alert(" usunięto użytkownika  $username "); window.location = '../admin/usermenager.php';
                </script>
                script;
                }
            }
        } catch (Exception $e) {
            mysqli_close($con);

            echo <<<script
                <script> type ="text/JavaScript">
                    alert(" usuń wszystikie posty użytkownika  $username "); window.location = '../manipulation/postviewsman.php?name=$username';
                </script>
                script;
        }
    }

    if (isset($_POST["changepassword"])) {
        $id_user = "'" . $user_data["user_id"] . "'";
        $pas = htmlspecialchars($_POST["nowpassword"]);
        $nowpass = "'" . password_hash($pas, PASSWORD_DEFAULT) . "'";
        $sql = "UPDATE `users` SET `password`=$nowpass WHERE `user_id` =$id_user";
        if (mysqli_query($con, $sql)) {
            echo <<<script
                <script>
                    alert("zmieniono hasło"); window.location = '../admin/paneluser.php';
                </script>
            script;
        } else {
            echo <<<script
                <script>
                    alert("wystąpił błąd przy zmienie hasła"); window.location = '../admin/paneluser.php';
                </script>
            script;
        }
    }

    if ($user_data["rola"] == "admin") {

        if (isset($_POST["restartpass"])) {
            $name = "'" . htmlspecialchars($_POST["username"]) . "'";
            $namepas =
                "'" .  password_hash($_POST["username"], PASSWORD_DEFAULT) . "'";
            $sql = "UPDATE `users` SET `password`=$namepas WHERE `name`=$name  && `id`!='1'";
            if (mysqli_query($con, $sql)) {
                echo <<<script
                <script>
                    alert("hasło zostało zrestartowane na domyslne czyli nazwe użytkownika"); window.location = '../admin/usermenager.php';
                </script>
            script;
            } else {
                echo <<<script
                <script>
                    alert("wystąpił błąd przy zmienie hasła"); window.location = '../admin/usermenager.php;
                </script>
            script;
            }
        }

        if (isset($_POST["changuser"])) {
            $username = "'" . $_POST["username"] . "'";
            $sql = "SELECT `rola` FROM `users` WHERE `name` = $username LIMIT 1";
            $result = mysqli_query($con, $sql);
            $rol = mysqli_fetch_assoc($result);
            $rol = $rol["rola"];
            if ($rol == "user") {
                $sql = " UPDATE `users` SET `rola`='admin' WHERE `name`=$username  && `id`!='1'";
            } else {
                $sql = " UPDATE `users` SET `rola`='user' WHERE `name`=$username  && `id`!='1'";
            }
            $result = mysqli_query($con, $sql);
            $username =  $_POST["username"];
            echo <<<script
                <script>
                    alert("zmieniono role użytkownika $username "); window.location = '../admin/usermenager.php';
                </script>
            script;
            mysqli_close($con);
        } else {
        }
    }
} else {
    header('Location: ../other/index.php');
}
