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

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

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

    $id = $_POST['cat_id'];
    $catName = $_POST['title'];


    if ($catName != '') {

        $response = $db->editCat($id, $catName);

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

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

            ?>
            <script>
                window.setTimeout(function () {
                    // Move to a new location or you can do something else
                    window.location.href = "category.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 Article</h2>
                                        <?php if (isset($response['message'])) echo $response["message"]; ?>
                                        <form method="post" enctype="multipart/form-data"
                                              action="edit_cat.php?id=<?php echo $category['0']['id']; ?>">

                                            <input type="hidden" value="<?php echo $id ?>" class="form-control"
                                                   name="cat_id">

                                            <div class="modal-body">

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

                                            </div>


                                            <div class="row">
                                                <div class="col-md-9"></div>

                                                <div class="col-md-3 mt-4">
                                                    <button type="submit" name="btn_update" class="btn btn-info">
                                                        Update
                                                    </button>
                                                </div>
                                            </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";
?>
