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

if (isset($_GET['id'])) {
    $id = $_GET['id'];
    $user = $db->getSingleUser($id);
}

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

    $id = $_POST['user_id'];
    $userName = $_POST['name'];
    $email = $_POST['email'];
    $phone = $_POST['phone_number'];


    if ($userName != '' && $email != '' && $phone != '') {

        $response = $db->editUser($id, $userName, $email, $phone);

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

            $response["message"] = '<div class="alert alert-success" style="text-align:center;">
                    <strong>Success!</strong> User updated successfully
                  </div>';

            ?>
            <script>
                window.setTimeout(function () {
                    // Move to a new location or you can do something else
                    window.location.href = "users.php";
                }, 1000);

            </script>

            <?php

        }
    } else {
        $response["message"] = '<div class="alert alert-danger" style="text-align:center;">
                    <strong>Sorry!</strong> All fields are required 
                                
                  </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="#addArticleModal">Add Article
                        </button>
                    </div>
                </div>
                <div class="row mb-5">
                    <div class="col-md-12">
                        <div class="card">
                            <div class="card-body">
                                <div class="row">
                                    <div class="col-lg-12 pb-5">
                                        <h2>Edit User</h2>
                                        <?php if (isset($response['message'])) echo $response["message"]; ?>

                                        <form method="post" action="edit_user.php">
                                            <div class="modal-body">
                                                <input type="hidden" value="<?php echo $user[0]['id'] ?>"
                                                       name="user_id">
                                                <div class="form-group my_form">
                                                    <label for="email">Name:</label>
                                                    <input type="text" value="<?php echo $user[0]['name'] ?>"
                                                           class="form-control" name="name">
                                                </div>

                                                <div class="form-group my_form">
                                                    <label for="email">Email:</label>
                                                    <input type="email" value="<?php echo $user[0]['email'] ?>"
                                                           class="form-control" name="email">
                                                </div>

                                                <div class="form-group my_form">
                                                    <label for="email">Phone Number:</label>
                                                    <input type="text" value="<?php echo $user[0]['phone'] ?>"
                                                           class="form-control" name="phone_number">
                                                </div>


                                            </div>
                                            <div class="modal-footer">
                                                <button type="submit" name="btn_update" class="btn btn-info">Update
                                                </button>
                                            </div>
                                        </form>


                                    </div>

                                </div>
                            </div>
                        </div>
                    </div>
                </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 "common/footer.php";
?>
