<?php

$pageTitle =
    $post['title']
    . ' - PET SPA';


$pageDescription =
    $post['excerpt']
    ?: 'Bài viết từ PET SPA';

require_once BASE_PATH
    . '/app/helpers/PostContent.php';
require BASE_PATH
    . '/app/views/layouts/header.php';

require BASE_PATH
    . '/app/views/layouts/navbar.php';

?>


<main>

    <article class="public-post-detail">

        <div class="container">

            <div class="row justify-content-center">

                <div class="col-xl-9 col-lg-10">


                    <!-- BREADCRUMB -->

                    <nav
                        aria-label="breadcrumb"
                        class="mb-4"
                    >

                        <ol class="breadcrumb">

                            <li class="breadcrumb-item">

                                <a
                                    href="<?= BASE_URL ?>/"
                                >
                                    Trang chủ
                                </a>

                            </li>


                            <li class="breadcrumb-item">

                                <a
                                    href="<?= BASE_URL ?>/?url=posts"
                                >
                                    Bài viết
                                </a>

                            </li>


                            <li
                                class="breadcrumb-item active"
                            >
                                <?= htmlspecialchars(
                                    $post['title']
                                ) ?>
                            </li>

                        </ol>

                    </nav>


                    <!-- CATEGORY -->

                    <?php if (
                        !empty(
                            $post[
                                'category_name'
                            ]
                        )
                    ): ?>

                        <span
                            class="public-post-category"
                        >

                            <?= htmlspecialchars(
                                $post[
                                    'category_name'
                                ]
                            ) ?>

                        </span>

                    <?php endif; ?>


                    <!-- TITLE -->

                    <h1
                        class="public-post-detail-title"
                    >

                        <?= htmlspecialchars(
                            $post['title']
                        ) ?>

                    </h1>


                    <!-- META -->

                    <div
                        class="public-post-detail-meta"
                    >

                        <span>

                            <i class="bi bi-person"></i>

                            <?= htmlspecialchars(
                                $post[
                                    'author_name'
                                ]
                            ) ?>

                        </span>


                        <span>

                            <i
                                class="bi bi-calendar3"
                            ></i>

                            <?= date(
                                'd/m/Y H:i',
                                strtotime(
                                    $post[
                                        'published_at'
                                    ]
                                )
                            ) ?>

                        </span>


                    </div>


                    <!-- EXCERPT -->

                    <?php if (
                        !empty(
                            $post['excerpt']
                        )
                    ): ?>

                        <div
                            class="public-post-detail-excerpt"
                        >

                            <?= htmlspecialchars(
                                $post['excerpt']
                            ) ?>

                        </div>

                    <?php endif; ?>


                    <!-- IMAGE -->

                    <?php if (
                        !empty(
                            $post[
                                'featured_image'
                            ]
                        )
                    ): ?>

                        <div
                            class="public-post-detail-image"
                        >

                            <img
                                src="<?= BASE_URL ?>/<?= htmlspecialchars(
                                    ltrim(
                                        $post[
                                            'featured_image'
                                        ],
                                        '/'
                                    )
                                ) ?>"
                                alt="<?= htmlspecialchars(
                                    $post['title']
                                ) ?>"
                            >

                        </div>

                    <?php endif; ?>


                    <!-- CONTENT -->

                    <div class="public-post-content">

    <?= sanitize_post_html(
        (string) $post['content']
    ) ?>

</div>

                </div>

            </div>

        </div>

    </article>


    <!-- RELATED -->

    <?php if (!empty($relatedPosts)): ?>

        <section
            class="section bg-light"
        >

            <div class="container">

                <div
                    class="section-heading text-center"
                >

                    <h2>
                        Bài viết liên quan
                    </h2>

                </div>


                <div class="row g-4">

                    <?php foreach (
                        $relatedPosts
                        as $related
                    ): ?>

                        <div class="col-md-4">

                            <article
                                class="public-post-card"
                            >

                                <a
                                    href="<?= BASE_URL ?>/?url=post&slug=<?= urlencode(
                                        $related['slug']
                                    ) ?>"
                                    class="public-post-image"
                                >

                                    <?php if (
                                        !empty(
                                            $related[
                                                'featured_image'
                                            ]
                                        )
                                    ): ?>

                                        <img
                                            src="<?= BASE_URL ?>/<?= htmlspecialchars(
                                                ltrim(
                                                    $related[
                                                        'featured_image'
                                                    ],
                                                    '/'
                                                )
                                            ) ?>"
                                            alt="<?= htmlspecialchars(
                                                $related[
                                                    'title'
                                                ]
                                            ) ?>"
                                        >

                                    <?php else: ?>

                                        <div
                                            class="public-post-placeholder"
                                        >
                                            🐾
                                        </div>

                                    <?php endif; ?>

                                </a>


                                <div
                                    class="public-post-body"
                                >

                                    <h2>

                                        <a
                                            href="<?= BASE_URL ?>/?url=post&slug=<?= urlencode(
                                                $related[
                                                    'slug'
                                                ]
                                            ) ?>"
                                        >

                                            <?= htmlspecialchars(
                                                $related[
                                                    'title'
                                                ]
                                            ) ?>

                                        </a>

                                    </h2>

                                </div>

                            </article>

                        </div>

                    <?php endforeach; ?>

                </div>

            </div>

        </section>

    <?php endif; ?>

</main>


<?php

require BASE_PATH
    . '/app/views/layouts/footer.php';

?>
