<?php

$pageTitle =
    'Danh mục 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']
);


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>

        <a
            href="<?= BASE_URL ?>/?url=owner/posts"
            class="small text-decoration-none"
        >
            ← Bài viết
        </a>

        <h1 class="mt-2">
            Danh mục bài viết
        </h1>

    </div>


    <button
        class="btn btn-primary"
        data-bs-toggle="modal"
        data-bs-target="#addPostCategory"
    >
        + Thêm danh mục
    </button>

</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">

<div class="table-responsive">

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

<thead>

<tr>
    <th>Tên</th>
    <th>Slug</th>
    <th>Bài viết</th>
    <th>Trạng thái</th>
    <th></th>
</tr>

</thead>

<tbody>

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

<tr>

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

<td>
    <code>
        <?= htmlspecialchars(
            $category['slug']
        ) ?>
    </code>
</td>

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

<td>

<?php if (
    (int)
    $category['is_active'] === 1
): ?>

<span class="badge bg-success">
    Hoạt động
</span>

<?php else: ?>

<span class="badge bg-secondary">
    Đã ẩn
</span>

<?php endif; ?>

</td>

<td>

<form
    method="POST"
    action="<?= BASE_URL ?>/?url=owner/post-category/toggle"
>

<input
    type="hidden"
    name="_token"
    value="<?= htmlspecialchars(
        $_SESSION['csrf_token']
    ) ?>"
>

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

<button
    type="submit"
    class="btn btn-sm btn-light"
>
    <?= (int)
        $category['is_active'] === 1
        ? 'Ẩn'
        : 'Hiện'
    ?>
</button>

</form>

</td>

</tr>

<?php endforeach; ?>

</tbody>

</table>

</div>

</section>

</div>

</div>

</div>

</main>


<div
    class="modal fade"
    id="addPostCategory"
    tabindex="-1"
>

<div class="modal-dialog">

<div class="modal-content">

<form
    method="POST"
    action="<?= BASE_URL ?>/?url=owner/post-category/store"
>

<input
    type="hidden"
    name="_token"
    value="<?= htmlspecialchars(
        $_SESSION['csrf_token']
    ) ?>"
>

<div class="modal-header">

<h5 class="modal-title">
    Thêm danh mục
</h5>

<button
    type="button"
    class="btn-close"
    data-bs-dismiss="modal"
></button>

</div>


<div class="modal-body">

<label class="form-label">
    Tên danh mục
</label>

<input
    type="text"
    name="name"
    class="form-control"
    required
>


<div class="form-check mt-3">

<input
    type="checkbox"
    name="is_active"
    class="form-check-input"
    id="postCategoryActive"
    checked
>

<label
    class="form-check-label"
    for="postCategoryActive"
>
    Hiển thị
</label>

</div>

</div>


<div class="modal-footer">

<button
    type="button"
    class="btn btn-light"
    data-bs-dismiss="modal"
>
    Hủy
</button>

<button
    type="submit"
    class="btn btn-primary"
>
    Thêm
</button>

</div>

</form>

</div>

</div>

</div>


<?php

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

?>