<?php
require_once "common/header.php";
require_once "include/db_handler.php";
require_once "include/paging.php";
$paginate = new Paging();
$db = new DbHandler();

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

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

    $artID = $_POST['artID'];
    $status = 'inactive';

    if (!empty($artID)) {
        $response = $db->updateStatus($artID, $status);
    } 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> Article Deactivated
                                
                  </div>';
} else if (isset($_POST['btn_delete'])) {

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

    $articleID = $_POST['articleID'];

    if (!empty($articleID)) {
        $response = $db->deleteArticle($articleID);
    } 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="published_article.php" class="close" data-dismiss="alert" aria-label="close">&times;</a>
                    <strong>Success!</strong> Article 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">
                        <a href="add_article.php" class="btn btn-success pull-right mb-3">Add Article
                        </a>
                    </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>Published Articles</h2>
                                        <?php if (isset($response['message'])) echo $response["message"];
                                        $query = "SELECT * FROM `tbl_articles` WHERE STATUS = 'published' ORDER By id DESC";
                                        $new_query = $paginate->getQuery($query, 10);
                                        $response = $db->getAllArticles($new_query);
                                        ?>
                                        <table class="table">
                                            <thead class="thead-light">
                                            <tr>
                                                <th>Title</th>
                                                <th>Author</th>
                                                <th>Image</th>
                                                <th>Status</th>
                                                <th>Action</th>
                                            </tr>
                                            </thead>
                                            <tbody>

                                            <?php foreach ($response as $article) : ?>
                                                <tr>

                                                    <td>
                                                        <a href="single_art.php?id=<?php echo $article['id']; ?>">
                                                            <?php echo $article['title']; ?>
                                                        </a>
                                                    </td>


                                                    <td><?php echo $article['author']; ?></td>
                                                    <td>
                                                        <img src="assets/img/post_pic/<?php echo $article['image']; ?>"
                                                             style="width: 60px!important; height: 60px!important;">
                                                    </td>
                                                    <td><?php echo $article['status']; ?></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_art.php?id=<?php echo $article['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>

                                                                    <div class="row ml-2 mr-3">
                                                                        <form action="published_article.php"
                                                                              method="post">
                                                                            <input type="hidden" name="artID"
                                                                                   value="<?php echo $article['id']; ?>"/>
                                                                            <input type="submit" class="btn btn-info"
                                                                                   name="btn_deactivate"
                                                                                   value="Deactivate">
                                                                        </form>

                                                                    </div>

                                                                    <form action="published_article.php" method="post">
                                                                        <input type="hidden" name="articleID"
                                                                               value="<?php echo $article['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>

                                    <style>
                                        .pagination a {
                                            color: black;
                                            float: left;
                                            padding: 8px 16px;
                                            text-decoration: none;
                                            transition: background-color .3s;
                                            border: 1px solid #ddd;
                                        }
                                    </style>
                                    <div class="row">
                                        <div class="col-md-8"></div>
                                        <div class="col-md-4">
                                            <ul class="pagination">
                                                <li class="button"><?php $records_per_page = 10;
                                                    $paginate->paginglink($query, $records_per_page); ?></li>
                                            </ul>
                                        </div>
                                    </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 "modals/add_user.php";
require_once "common/footer.php";
?>
