<?php

$pageTitle =
    'Bài viết - PET SPA';


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

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

?>


<main>

    <!-- HERO -->

    <section class="post-page-hero">

        <div class="container text-center">

            <span class="post-page-eyebrow">
                PET SPA BLOG
            </span>

            <h1>
                Góc kiến thức thú cưng
            </h1>

            <p>

                Chia sẻ kinh nghiệm chăm sóc,
                dinh dưỡng và những câu chuyện
                thú vị dành cho người yêu thú cưng.

            </p>

        </div>

    </section>


    <section class="section">

        <div class="container">


            <!-- SEARCH -->

            <div class="post-filter-card">

                <form
                    method="GET"
                    class="row g-3"
                >

                    <input
                        type="hidden"
                        name="url"
                        value="posts"
                    >


                    <div class="col-lg-7">

                        <div class="input-group">

                            <span class="input-group-text">

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

                            </span>


                            <input
                                type="text"
                                name="search"
                                class="form-control"
                                placeholder="Tìm bài viết..."
                                value="<?= htmlspecialchars(
                                    $search
                                ) ?>"
                            >

                        </div>

                    </div>


                    <div class="col-lg-3">

                        <select
                            name="category"
                            class="form-select"
                        >

                            <option value="0">
                                Tất cả danh mục
                            </option>


                            <?php foreach (
                                $categories
                                as $category
                            ): ?>

                                <option
                                    value="<?= (int)
                                        $category['id']
                                    ?>"

                                    <?= $categoryId
                                        === (int)
                                        $category['id']
                                            ? 'selected'
                                            : ''
                                    ?>
                                >

                                    <?= htmlspecialchars(
                                        $category['name']
                                    ) ?>

                                    (<?= (int)
                                        $category['post_count']
                                    ?>)

                                </option>

                            <?php endforeach; ?>

                        </select>

                    </div>


                    <div class="col-lg-2">

                        <button
                            class="btn btn-primary w-100"
                            type="submit"
                        >
                            Tìm kiếm
                        </button>

                    </div>

                </form>

            </div>


            <!-- POST GRID -->

            <?php if (empty($posts)): ?>

                <div class="post-empty">

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

                    <h3>
                        Chưa có bài viết
                    </h3>

                    <p>
                        Không tìm thấy bài viết phù hợp.
                    </p>

                </div>

            <?php else: ?>

                <div class="row g-4">

                    <?php foreach (
                        $posts
                        as $post
                    ): ?>

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

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

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

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

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

                                    <?php else: ?>

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

                                    <?php endif; ?>

                                </a>


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

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

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

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

                                        </span>

                                    <?php endif; ?>


                                    <h2>

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

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

                                        </a>

                                    </h2>


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

                                        <p>

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

                                        </p>

                                    <?php endif; ?>


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

                                        <span>

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

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

                                        </span>


                                    </div>

                                </div>

                            </article>

                        </div>

                    <?php endforeach; ?>

                </div>

            <?php endif; ?>


            <!-- PAGINATION -->

            <?php if ($totalPages > 1): ?>

                <nav class="mt-5">

                    <ul
                        class="pagination justify-content-center"
                    >

                        <?php for (
                            $i = 1;
                            $i <= $totalPages;
                            $i++
                        ): ?>

                            <?php

                            $query = [
                                'url' =>
                                    'posts',

                                'page' =>
                                    $i
                            ];


                            if ($search !== '') {

                                $query['search'] =
                                    $search;
                            }


                            if ($categoryId > 0) {

                                $query['category'] =
                                    $categoryId;
                            }

                            ?>


                            <li
                                class="page-item <?= $page === $i
                                    ? 'active'
                                    : ''
                                ?>"
                            >

                                <a
                                    class="page-link"
                                    href="<?= BASE_URL ?>/?<?= htmlspecialchars(
                                        http_build_query(
                                            $query
                                        )
                                    ) ?>"
                                >
                                    <?= $i ?>
                                </a>

                            </li>

                        <?php endfor; ?>

                    </ul>

                </nav>

            <?php endif; ?>

        </div>

    </section>

</main>


<?php

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

?>
