<?php
require_once '../functions/truncate.php';

if (isset($_SESSION['admin'])) {
?>

    <table class="table table-striped table-hover table-sm table-responsive">
        <thead class="text-white bg-dark">
            <tr>
                <th scope="col">Title</th>
                <th scope="col">Body</th>
                <th scope="col">Status</th> 
                <th scope="col">Action</th>
            </tr>
        </thead>
        <tbody>
            <?php
            $dbHandler = DbHandler();
            $query = $dbHandler->prepare("SELECT * FROM tbl_blog ORDER BY created_at desc");
            $query->execute();
            while ($row = $query->fetch()) {
                $id = $row['id'];
                $title = $row['a_title'];
                $body = $row['a_body'];
                $status = $row['status'];
            ?>
                <tr>
                    <td>
                        &nbsp;<?php echo $title; ?>
                    </td>
                    <td>
                        &nbsp;<?php
                                $txt = truncate($body, 20);
                                echo $txt; ?>
                    </td>
                    <td>
                        &nbsp;<b><?php echo $status; ?></b>
                    </td>
                    <td>
                        <a class="btn btn-success btn-sm rounded" href="./edit-blog.php?id=<?php echo base64_url_encode($id); ?>">Edit</a>
                        <a class="btn btn-danger btn-sm rounded" href="../functions/deleteBlogPost.php?id=<?php echo base64_url_encode($id); ?>" onclick="return confirm('Are you sure?')">Delete</a>
                    </td>
                </tr>
        </tbody>
<?php
            }
        } else {
            echo "<script> window.location.assign('../logout.php'); </script>";
        }
?>
    </table>