<!DOCTYPE html>
<html lang="ru" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{default}">

<head>
    <title>Панель администратора (книги)</title>
</head>

<body th:with="bodyClass='bg-white', mainClass='my-0'">
    <main layout:fragment="content">
        <th:block th:switch="${items.size()}">
            <h2 class="mt-4 text-center" th:case="0">Данные отсутствуют</h2>
            <th:block th:case="*">
                <div class="row gy-3">
                    <div class="col-12 mt-4 px-0">
                        <div class="block d-flex justify-content-center fs-2 fw-bold mb-3 mb-sm-4" id="admin-title">
                            Книги
                        </div>
                    </div>
                    <form th:action="@{/admin/book}" method="get" class="row mb-4">
                        <div class="col-sm-10">
                            <input type="hidden" th:name="page" th:value="${page}">
                            <select th:name="filter" id="filter" class="form-select">
                                <option selected value="">Фильтр по категории</option>
                                <option th:each="category : ${categories}" th:value="${category.id}" th:selected="${category.id==filter}">
                                    [[${category.name}]]
                                </option>
                            </select>
                        </div>
                        <button type="submit" class="btn btn-primary col-sm-2">Показать</button>
                    </form>
                    <div class="col-12 px-0">
                        <div class="block table-responsive">
                            <table class="table table-hover table-bordered align-middle">
                                <caption></caption>
                                <thead>
                                    <tr>
                                        <th scope="col">ID</th>
                                        <th scope="col">Название книги</th>
                                        <th scope="col">Автор</th>
                                        <th scope="col">Цена</th>
                                        <th scope="col">Категория каталога</th>
                                        <th scope="col">Дата</th>
                                        <th scope="col"></th>
                                        <th scope="col"></th>
                                    </tr>
                                </thead>
                                <tbody class="table-group-divider">
                                    <tr th:each="book : ${items}">
                                        <th scope="row" th:text="${book.id}"></th>
                                        <td th:text="${book.title}"></td>
                                        <td th:text="${book.author}"></td>
                                        <td th:text="${#numbers.formatDecimal(book.price, 1, 2)}"></td>
                                        <td th:text="${book.categoryName}"></td>
                                        <td th:text="${book.date}"></td>
                                        <td>
                                            <form th:action="@{/admin/book/edit/{id}(id=${book.id})}" method="get">
                                                <input type="hidden" th:name="page" th:value="${page}">
                                                <input type="hidden" th:name="filter" th:value="${filter}">
                                                <button type="submit" class="btn btn-primary w-100">Редактировать</button>
                                            </form>
                                        </td>
                                        <td>
                                            <form th:action="@{/admin/book/delete/{id}(id=${book.id})}" method="post">
                                                <input type="hidden" th:name="page" th:value="${page}">
                                                <input type="hidden" th:name="filter" th:value="${filter}">
                                                <button type="submit" class="btn btn-primary w-100"
                                                    onclick="return confirm('Вы уверены?')">Удалить</button>
                                            </form>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                    <div class="col-12 px-0">
                        <div class="block mb-4">
                            <a class="btn btn-primary" th:href="@{/admin/book/edit/(page=${page}, filter=${filter})}">Добавить книгу</a>
                        </div>
                    </div>
                    <th:block th:replace="~{ pagination :: pagination (
                                                                url=${'admin/book'},
                                                                totalPages=${totalPages},
                                                                currentPage=${currentPage},
                                                                filter=${filter},
                                                                searchInfo='') }" />
                </div>  
        </th:block>
    </main>
</body>

</html>