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

$db = new DbHandler();

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

    $catName = $_POST['category_name'];

    if ($catName != '') {

        $response = $db->addCat($catName);

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

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


    } else {
        $response["message"] = '<div class="alert alert-danger" style="text-align:center;">
                    <a href="category.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();

    $categoryID = $_POST['categoryID'];

    if (!empty($categoryID)) {
        $response = $db->deleteCategory($categoryID);
    } 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> Category 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="#addCategoryModals">Add Category
                        </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>Categories</h2>
                                        <?php if (isset($response['message'])) echo $response["message"]; ?>
                                        <?php
                                        $query = "SELECT * FROM tbl_categories";
                                        $response = $db->getAllCategories($query);
                                        ?>
                                        <table class="table">
                                            <thead class="thead-light">
                                            <tr>
                                                <th>Name</th>
                                                <th>Date Created</th>
                                                <th>Action</th>
                                            </tr>
                                            </thead>
                                            <tbody>
                                            <?php foreach ($response as $category) : ?>
                                                <tr>
                                                    <td><?php echo $category['name']; ?></td>
                                                    <td><?php echo $category['created_at']; ?></td>
                                                    <td>


                                                        <div class="email-pager-top">
                                                            <div class="email-pager-container">
                                                                <div class="btn-group" role="group"
                                                                     aria-label="Previous or Next Page">
                                                                    <a href="edit_cat.php?id=<?php echo $category['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="category.php" method="post">
                                                                        <input type="hidden" name="categoryID"
                                                                               value="<?php echo $category['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>
                                                    </td>

                                                </tr>
                                            <?php endforeach; ?>
                                            </tbody>
                                        </table>
                                    </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_user.php";
require_once "modals/add_category.php";
require_once "common/footer.php";
?>
