$(document).ready(() => { const STATUS_CONFIG = { active: { label: "Доступна", bgClass: "bg-green-100", textClass: "text-green-800", icon: ``, }, borrowed: { label: "Выдана", bgClass: "bg-yellow-100", textClass: "text-yellow-800", icon: ``, }, reserved: { label: "Забронирована", bgClass: "bg-blue-100", textClass: "text-blue-800", icon: ``, }, restoration: { label: "На реставрации", bgClass: "bg-orange-100", textClass: "text-orange-800", icon: ``, }, written_off: { label: "Списана", bgClass: "bg-red-100", textClass: "text-red-800", icon: ``, }, }; function getStatusConfig(status) { return ( STATUS_CONFIG[status] || { label: status || "Неизвестно", bgClass: "bg-gray-100", textClass: "text-gray-800", icon: "", } ); } const pathParts = window.location.pathname.split("/"); const bookId = pathParts[pathParts.length - 1]; let currentBook = null; if (!bookId || isNaN(bookId)) { Utils.showToast("Некорректный ID книги", "error"); return; } Api.get(`/api/books/${bookId}`) .then((book) => { currentBook = book; document.title = `LiB - ${book.title}`; renderBook(book); if (window.canManage) { $("#edit-book-btn") .attr("href", `/book/${book.id}/edit`) .removeClass("hidden"); } }) .catch((error) => { console.error(error); Utils.showToast("Книга не найдена", "error"); $("#book-loader").html( '

Ошибка загрузки

', ); }); function renderBook(book) { $("#book-title").text(book.title); $("#book-id").text(`ID: ${book.id}`); $("#book-authors-text").text( book.authors.map((a) => a.name).join(", ") || "Автор неизвестен", ); $("#book-description").text(book.description || "Описание отсутствует"); renderStatusWidget(book); if (!window.canManage && book.status === "active") { renderReserveButton(); } if (book.genres && book.genres.length > 0) { $("#genres-section").removeClass("hidden"); const $genres = $("#genres-container"); $genres.empty(); book.genres.forEach((g) => { $genres.append(` ${Utils.escapeHtml(g.name)} `); }); } if (book.authors && book.authors.length > 0) { $("#authors-section").removeClass("hidden"); const $authors = $("#authors-container"); $authors.empty(); book.authors.forEach((a) => { $authors.append(`
${a.name.charAt(0).toUpperCase()}
${Utils.escapeHtml(a.name)}
`); }); } $("#book-loader").addClass("hidden"); $("#book-content").removeClass("hidden"); } function renderStatusWidget(book) { const $container = $("#book-status-container"); $container.empty(); const config = getStatusConfig(book.status); if (window.canManage) { const $dropdownHTML = $(`
`); $container.append($dropdownHTML); const $toggleBtn = $("#status-toggle-btn"); const $menu = $("#status-menu"); $toggleBtn.on("click", (e) => { e.stopPropagation(); $menu.toggleClass("hidden"); }); $(document).on("click", (e) => { if ( !$toggleBtn.is(e.target) && $toggleBtn.has(e.target).length === 0 && !$menu.has(e.target).length ) { $menu.addClass("hidden"); } }); $(".status-option").on("click", function () { const newStatus = $(this).data("status"); if (newStatus !== currentBook.status) { updateBookStatus(newStatus); } $menu.addClass("hidden"); }); } else { $container.append(` ${config.icon} ${config.label} `); } } function renderReserveButton() { const $container = $("#book-actions-container"); $container.html(` `); $("#reserve-btn").on("click", function () { Utils.showToast("Функция бронирования в разработке", "info"); }); } async function updateBookStatus(newStatus) { const $toggleBtn = $("#status-toggle-btn"); const originalContent = $toggleBtn.html(); $toggleBtn.prop("disabled", true).addClass("opacity-75").html(` Обновление... `); try { const payload = { title: currentBook.title, description: currentBook.description, status: newStatus, }; const updatedBook = await Api.put( `/api/books/${currentBook.id}`, payload, ); currentBook = updatedBook; Utils.showToast("Статус успешно изменен", "success"); renderStatusWidget(updatedBook); } catch (error) { console.error(error); Utils.showToast("Ошибка при смене статуса", "error"); $toggleBtn .prop("disabled", false) .removeClass("opacity-75") .html(originalContent); } } });