<?php
require_once "common/front_header.php";

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

    if ($_POST['search_keyword'] != '') {
        $keyWord = $_POST['search_keyword'];
    } else {
        $keyWord = "";
    }
}
?>
<div class="container">
    <div class="row">
        <div class="col-12 col-md-8">
            <div class="row">


                <?php
                $query = "SELECT tbl_articles.*, tbl_categories.name as category FROM `tbl_articles` inner join tbl_categories on
                                          tbl_categories.id=tbl_articles.category WHERE title like '%" . $keyWord . "%'
                                           AND status = 'published' ORDER BY id DESC";
                $response = $db->getAllArticles($query);
                ?>

                <div class="container">
                    <div class="postList">
                        <?php if ($response) {

                            foreach ($response as $article) : ?>
                                <div class="col-12 mt-5 ">
                                    <div class="card p-4 border-0">
                                        <a href="article.php?id=<?php echo $article['id']; ?>"
                                           style="text-decoration: none; color: #999">
                                            <div class="titles py-4"><?php echo $article['title']; ?></div>
                                        </a>
                                        <a href="article.php?id=<?php echo $article['id']; ?>">
                                            <img src="assets/img/post_pic/<?php echo $article['image']; ?>"
                                                 class="card-img-top"
                                                 style="width: 100%!important; height: 100% !important;"
                                                 id="image"
                                                 alt="...">
                                        </a>
                                        <div class="card-body">
                                            <a href="article.php?id=<?php echo $article['id']; ?>"
                                               style="text-decoration: none; color: #999; font-size: 18px">
                                                <p class="card-text">
                                                    <?php
                                                    $summary = substr($article['body'], 0, 250);
                                                    echo $summary; ?>
                                                </p>
                                            </a>
                                            <a href="article.php?id=<?php echo $article['id']; ?>" class="btn btn-dark">Read
                                                More</a>
                                        </div>
                                    </div>
                                </div>
                            <?php endforeach;
                        }else{
                            ?>
                                <div class="alert alert-warning mt-5" role="alert">
                                    No record found!
                            </div>
                        <?php
                        }
                        ?>
                    </div>
                </div>
            </div>


        </div>
        <div class="col-12 col-md-4 mt-5">
            <?php
            include_once "common/front_sidebar.php";
            ?>
        </div>
    </div>
</div>
<?php
require_once "common/front_footer.php";
?>
