<?php
require_once "common/header.php";
require_once "include/db_handler.php";

$db = new DbHandler();

if (isset($_POST['submit_user'])) {

    $userName = $_POST['name'];
    $email = $_POST['email'];
    $phoneNumber = $_POST['phone_number'];
    $userType = $_POST['user_type'];
    $password = $_POST['password'];
    $confirmPassword = $_POST['confirm_password'];
    $userPassword = md5($password);

    $userPhoto = $_FILES['profile_photo']['name'];
    move_uploaded_file($_FILES['profile_photo']['tmp_name'], 'front_assets/img/profile_pic/' . $_FILES['profile_photo']['name']);

    if ($userName != '' && $email != '' && $email != '' && $phoneNumber != '' && $password != '' && $confirmPassword != '') {

        if ($password == $confirmPassword) {

            if (!$db->emailExists($email)) {

                $response = $db->addUser($userName, $email, $phoneNumber, $userType, $userPhoto, $userPassword);

                if ($response["error"] == false) {

                    $response["message"] = '<div class="alert alert-success" style="text-align:center;">
                    <a href="users.php" class="close" data-dismiss="alert" aria-label="close">&times;</a>
                    <strong>Success!</strong> User added successfully
                  </div>';
                }
            } else {
                $response["message"] = '<div class="alert alert-danger" style="text-align:center;">
                    <a href="users.php" class="close" data-dismiss="alert" aria-label="close">&times;</a>
                    <strong>Sorry!</strong> Email Taken
                                
                  </div>';
            }

        } else {
            $response["message"] = '<div class="alert alert-danger" style="text-align:center;">
                    <a href="users.php" class="close" data-dismiss="alert" aria-label="close">&times;</a>
                    <strong>Sorry!</strong> Password mismatch
                                
                  </div>';
        }

    } else {
        $response["message"] = '<div class="alert alert-danger" style="text-align:center;">
                    <a href="users.php" class="close" data-dismiss="alert" aria-label="close">&times;</a>
                    <strong>Sorry!</strong> All fields are required 
                                
                  </div>';
    }

} else if (isset($_POST['btn_delete'])) {

    $db = new DbHandler();
    $response = array();

    $userID = $_POST['user_id'];

    if (!empty($userID)) {
        $response = $db->deleteUser($userID);
    } else {
        $response['error'] = true;
        $response['message'] = "An error occurred while deleting";
    }

    $response["message"] = '<div class="alert alert-danger" id="danger-alert" style="text-align:center;">
                    <a href="category.php" class="close" data-dismiss="alert" aria-label="close">&times;</a>
                    <strong>Success!</strong> user deleted
                                
                  </div>';
}

?>
    <body>

<div class="container-fluid">
    <div class="row">
        <?php
        require_once "common/sidebar.php";
        ?>
        <div class="right-column">
            <?php
            require_once "common/navbar.php";
            ?>
            <main class="main-content p-5" role="main">
                <div class="row">
                    <div class="col-md-12">
                        <button type="button" class="btn btn-success pull-right mb-3" data-toggle="modal"
                                data-target="#addUserModals">Add User
                        </button>
                    </div>
                </div>
                <?php if (isset($response['message'])) echo $response["message"]; ?>
                <?php
                $query = "SELECT * FROM users WHERE userType = 'guest' OR userType = 'moderator'";
                $response = $db->getAllUsers($query);
                ?>

                <div class="row mb-5">
                    <?php foreach ($response as $user) : ?>
                        <div class="col-sm-12 col-md-6 col-xl-3 mb-4">
                            <div class="card card-md bg-primary bg-gradient text-center">
                                <div class="card-body">
                                    <div class="profile-picture profile-picture-lg bg-gradient bg-primary mt-5">

                                        <?php
                                        if ($user['photo_url'] == '') {
                                            ?>
                                            <img src="assets/img/profile_pic/avatar.png" width="44" height="44">
                                            <?php
                                        } else {
                                            ?>
                                            <img src="assets/img/profile_pic/<?php echo $user['photo_url']; ?>"
                                                 width="44" height="44">
                                            <?php
                                        }
                                        ?>
                                    </div>
                                    <h6 class="mt-5 mb-4"><?php echo $user['name']; ?></h6>


                                    <div class="btn-group" role="group"
                                         aria-label="Previous or Next Page">
                                        <a href="edit_user.php?id=<?php echo $user['id']; ?>">
                                            <button type="button"
                                                    class="btn btn-primary email-list-previous waves-effect waves-light mr-3"
                                                    data-toggle="tooltip" title=""
                                                    data-original-title="Edit">
                                                <i class="fa fa-pencil"> Edit</i>
                                            </button>
                                        </a>
                                        <form action="users.php" method="post">
                                            <input type="hidden" name="user_id"
                                                   value="<?php echo $user['id']; ?>"/>

                                            <button type="submit" name="btn_delete"
                                                    class="btn btn-danger email-list-next waves-effect waves-light"
                                                    data-toggle="tooltip" title=""
                                                    data-original-title="Delete">
                                                <i class="fa fa-trash"> Delete</i>
                                            </button>

                                        </form>
                                    </div>


                                </div>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>


            </main>
        </div>
    </div>
</div>
<?php
require_once "modals/add_article.php";
require_once "modals/logout.php";
require_once "modals/add_category.php";
require_once "modals/add_user.php";
require_once "common/footer.php";
?>