<?php

$pageTitle =
    'Quản lý bài viết - PET SPA';

$pageStyles = [
    'owner.css'
];

$activeOwnerPage =
    'posts';


$successMessage =
    $_SESSION['flash_success']
    ?? null;

$errorMessage =
    $_SESSION['flash_error']
    ?? null;


unset(
    $_SESSION['flash_success'],
    $_SESSION['flash_error']
);


$statusNames = [
    'draft' => 'Bản nháp',
    'published' => 'Đã đăng',
    'hidden' => 'Đã ẩn'
];


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

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

?>

<main class="owner-area">

    <div class="container-fluid">

        <div class="row g-4">

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

                <?php require BASE_PATH
                    . '/app/views/layouts/owner-sidebar.php';
                ?>

            </div>


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

                <div class="owner-page-header">

                    <div>

                        <h1>
                            Bài viết
                        </h1>

                        <p>
                            Quản lý nội dung của PET SPA.
                        </p>

                    </div>


                    <div class="d-flex gap-2">

                        <a
                            href="<?= BASE_URL ?>/?url=owner/post-categories"
                            class="btn btn-outline-dark"
                        >
                            Danh mục
                        </a>

                        <a
                            href="<?= BASE_URL ?>/?url=owner/post/create"
                            class="btn btn-primary"
                        >
                            + Viết bài
                        </a>

                    </div>

                </div>


                <?php if ($successMessage): ?>

                    <div class="alert alert-success">

                        <?= htmlspecialchars(
                            $successMessage
                        ) ?>

                    </div>

                <?php endif; ?>


                <?php if ($errorMessage): ?>

                    <div class="alert alert-danger">

                        <?= htmlspecialchars(
                            $errorMessage
                        ) ?>

                    </div>

                <?php endif; ?>


                <section class="owner-card mb-4">

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

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

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

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

                        </div>

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

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

                                <option value="">
                                    Tất cả trạng thái
                                </option>

                                <?php foreach (
                                    $statusNames
                                    as $key => $label
                                ): ?>

                                    <option
                                        value="<?= $key ?>"
                                        <?= $status === $key
                                            ? 'selected'
                                            : ''
                                        ?>
                                    >
                                        <?= htmlspecialchars(
                                            $label
                                        ) ?>
                                    </option>

                                <?php endforeach; ?>

                            </select>

                        </div>

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

                            <button
                                class="btn btn-dark w-100"
                            >
                                Lọc
                            </button>

                        </div>

                    </form>

                </section>


                <section class="owner-card">

                    <div class="table-responsive">

                        <table class="table align-middle owner-table">

                            <thead>

                                <tr>
                                    <th>Bài viết</th>
                                    <th>Danh mục</th>
                                    <th>Tác giả</th>
                                    <th>Trạng thái</th>
                                    <th>Ngày tạo</th>
                                    <th></th>
                                </tr>

                            </thead>

                            <tbody>

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

                                    <tr>

                                        <td>

                                            <div class="owner-post-cell">

                                                <div class="owner-post-image">

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

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

                                                    <?php else: ?>

                                                        <i class="bi bi-file-text"></i>

                                                    <?php endif; ?>

                                                </div>


                                                <strong>

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

                                                </strong>

                                            </div>

                                        </td>


                                        <td>

                                            <?= htmlspecialchars(
                                                $post[
                                                    'category_name'
                                                ]
                                                ?? 'Chưa phân loại'
                                            ) ?>

                                        </td>


                                        <td>

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

                                        </td>



                                        <td>

                                            <span
                                                class="post-status post-status-<?= htmlspecialchars(
                                                    $post['status']
                                                ) ?>"
                                            >

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

                                            </span>

                                        </td>


                                        <td>

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

                                        </td>


                                        <td>

                                            <div class="d-flex gap-2 flex-wrap">

                                                <a
                                                    href="<?= BASE_URL ?>/?url=owner/post/edit&id=<?= (int)
                                                        $post['id']
                                                    ?>"
                                                    class="btn btn-sm btn-light"
                                                >
                                                    <i class="bi bi-pencil-square me-1"></i>
                                                    Sửa
                                                </a>

                                                <form
                                                    method="POST"
                                                    action="<?= BASE_URL ?>/?url=owner/post/delete"
                                                    class="d-inline"
                                                    onsubmit="return confirm('Xóa vĩnh viễn bài viết này? Thao tác này không thể hoàn tác.')"
                                                >
                                                    <input
                                                        type="hidden"
                                                        name="_token"
                                                        value="<?= htmlspecialchars(
                                                            $_SESSION['csrf_token']
                                                        ) ?>"
                                                    >

                                                    <input
                                                        type="hidden"
                                                        name="post_id"
                                                        value="<?= (int)
                                                            $post['id']
                                                        ?>"
                                                    >

                                                    <button
                                                        type="submit"
                                                        class="btn btn-sm btn-outline-danger"
                                                    >
                                                        <i class="bi bi-trash3 me-1"></i>
                                                        Xóa
                                                    </button>
                                                </form>

                                            </div>

                                        </td>

                                    </tr>

                                <?php endforeach; ?>


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

                                    <tr>

                                        <td
                                            colspan="7"
                                            class="text-center text-muted py-5"
                                        >
                                            Chưa có bài viết.
                                        </td>

                                    </tr>

                                <?php endif; ?>

                            </tbody>

                        </table>

                    </div>

                </section>


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

                    <nav class="mt-4">

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

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

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

                                    <a
                                        class="page-link"
                                        href="<?= BASE_URL ?>/?url=owner/posts&page=<?= $i ?><?= $status !== ''
                                            ? '&status=' . urlencode($status)
                                            : ''
                                        ?><?= $search !== ''
                                            ? '&search=' . urlencode($search)
                                            : ''
                                        ?>"
                                    >
                                        <?= $i ?>
                                    </a>

                                </li>

                            <?php endfor; ?>

                        </ul>

                    </nav>

                <?php endif; ?>

            </div>

        </div>

    </div>

</main>


<?php

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

?>
