<!DOCTYPE html>
<html lang="en"
xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5"
>

<!--/*

Idea of the following syntax used in this and other HTML document:

	<foo th:text="${#messages.msgOrNull('<key_from_messages.properties>')} ?: 'myThymeleafText'">
		myHTMLPlainText
	</foo>

-> A) In Thymeleaf, we check if a message key exists. In normal cases, a non-existent key is NOT null.
		Therefore, we use method msgorNull and enforce null value if the message key is not found.

-> B) IF key is null: in section '?: myThymeleafText' we use text 'myThymeleafText' as a replacement
		value for the HTML tag string value.

-> C) The last string value 'myHTMLPlainText' between tags is used if we open plain HTML file without
		Thymeleaf rendering. This is useful for offline viewing and for developers who want correct file
		rendering without Java overhead.

*/-->

<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

	<!--/*
	CSS href fields:

	Plain HTML rendering gets CSS style files directly from the relative file system path.
	This is useful for offline viewing and for developers who want correct file
	rendering without Java overhead.

	Instead, Thymeleaf template engine gets CSS style files via URI refs. Therefore, values of
	href and th:href differ. However, both use cases are covered: Thymeleaf & plain HTML.

	*/-->

	<link type="text/css" rel="stylesheet" href="../static/css/bookstore.css" th:href="@{../css/bookstore.css}" />
	<link type="text/css" rel="stylesheet" href="../static/css/bootstrap.min.css" th:href="@{../css/bootstrap.min.css}" />

	<title th:text="${#messages.msgOrNull('page.title.browser.list')} ?: 'page.title.browser.list'">
	page.title.browser.list
	</title>

</head>
<body>
	<div>

		<div id="bookstore-topform-base">
			<div>
				<h1
				th:text="${#messages.msgOrNull('page.title.webform.list')} ?: 'page.title.webform.list'">
				page.title.webform.list
				</h1>
			</div>

			<th:block th:replace="fragments/loginout :: loginout"/>

		</div>

		<!--/*
		TODO: Use proper object notation here?
		For example syntax, see
		https://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#order-details
		*/-->
		<table class="table table-striped" sec:authorize="isAuthenticated()">

			<tr>
				<th th:text="${#messages.msgOrNull('book.author')} ?: 'book.author'">book.author</th>
				<th th:text="${#messages.msgOrNull('book.title')} ?: 'book.title'">book.title</th>
				<th th:text="${#messages.msgOrNull('book.isbn')} ?: 'book.isbn'">book.isbn</th>
				<th th:text="${#messages.msgOrNull('book.year')} ?: 'book.year'">book.year</th>
				<th th:text="${#messages.msgOrNull('book.category')} ?: 'book.category'">book.category</th>
				<th th:text="${#messages.msgOrNull('book.price')} ?: 'book.price'">book.price</th>

				<th
				th:text="${#messages.msgOrNull('page.text.list.published')} ?: 'page.text.list.published'"
				th:if="${#authorization.expression('hasAuthority(''MARKETING'')')}"
				>
				page.text.list.published
				</th>
				
				<th th:unless="${#authorization.expression('hasAuthority(''MARKETING'')')}">
				</th>

				<th
				th:text="${#messages.msgOrNull('page.text.list.actions')} ?: 'page.text.list.actions'"
				th:if="${#authorization.expression('hasAnyAuthority(''MARKETING'', ''HELPDESK'')')}"
				>
				page.text.list.actions
				</th>
				
				<th th:unless="${#authorization.expression('hasAnyAuthority(''MARKETING'', ''HELPDESK'')')}">
				</th>

				<th></th>
				<th th:text="${#messages.msgOrNull('book.json')} ?: 'book.json'">book.json</th>
			</tr>

			<tr 
			th:each="book : ${books}"
			th:object="${book}"
			th:if="${book.publish == true or #authorization.expression('hasAuthority(''MARKETING'')')}"
			>

				<!--/*
				Check if book.author exists. If yes, then:
					|-> Check if firstName exists. If yes, then:
					|	 Set author first name
					|   else:
					|	 Set empty string for first name
					|----------
					|-> Check if lastName exists, if yes, then:
					|	 Set author last name
					|   else:
					|	 Set empty string for last name
				##########
				else:
					|-> Check if book.null.author exists. If yes, then:
					|	 Get value from book.null.author message key
					|   else:
					|	 Author is string value 'book.null.author'
				*/-->

				<td th:text="${book.author != null} ? ( (${book.author.firstName != null} ? (${book.author.firstName} + ' ') : '') + (${book.author.lastName != null} ? ${book.author.lastName} : '') ) : (${#messages.msgOrNull('book.null.author')} ?: 'book.null.author')">
				(obj) book.author.firstName (obj) book.author.lastName
				</td>

				<td th:text="${book.title}">(obj) book.title</td>
				<td th:text="${book.isbn}">(obj) book.isbn</td>
				<td th:text="${book.year}">(obj) book.year</td>


				<!--/*
				Check if book.category exists (book entity object). If yes, then:
					Get string value of book.category.name (book entity object (method))
				else:
					|-> Check if book.null.category (message key) exists. If yes, then:
					|	 Get string value of book.null.category message key
					|   else:
					|	 Category is string value 'book.null.category'
				*/-->

				<td th:text="${book.category != null} ? ${book.category.name} : (${#messages.msgOrNull('book.null.category')} ?: 'book.null.category')">
				(obj) book.category.name
				</td>

				<!--/*
				Get book price with two decimal precision.
				If price is not found, fall back to string value 0.00.

				Combine with message key 'page.symbols.currency' value.
				*/-->

				<td th:text="(${#numbers.formatDecimal(book.price, 1, 2)} ?: '0.00') + ' ' + (${#messages.msgOrNull('page.symbols.currency')} ?: 'page.symbols.currency')">
				0.00 page.symbols.currency
				</td>

				<td>
					<p sec:authorize="hasAuthority('MARKETING')"
					th:text="${book.publish == true} ? (${#messages.msgOrNull('page.text.list.yes')} ?: 'page.text.list.yes') : (${#messages.msgOrNull('page.text.list.no')} ?: 'page.text.list.no')"
					>
					page.text.list.[yes|no]
					</p>
				</td>

				<td sec:authorize="hasAuthority('ADMIN')">
					<a class="btn btn-danger" 
					th:attr="onclick='javascript:return confirm(\'' + 'Delete book: ' + ${book.title} + '?' + '\');'"
					th:href="@{__${deletepage}__/{hash_id}(hash_id=${book.bookHash.hashId})}"
					th:text="${#messages.msgOrNull('page.text.list.delete')} ?: 'page.text.list.delete'">
					page.text.list.delete
					</a>
				</td>

				<td>
					<a class="btn btn-warning" sec:authorize="hasAnyAuthority('MARKETING', 'HELPDESK')"
					th:href="@{__${editpage}__/{hash_id}(hash_id=${book.bookHash.hashId})}"
					th:text="${#messages.msgOrNull('page.text.list.edit')} ?: 'page.text.list.edit'">
					page.text.list.edit
					</a>
				</td>

				<td th:unless="${#authorization.expression('hasAuthority(''ADMIN'')')}">
				</td>

				<td>
					<a class="btn btn-info"
					th:href="@{__${restpage}__/book/{hash_id}(hash_id=${book.bookHash.hashId})}"
					th:text="${#messages.msgOrNull('page.text.list.json')} ?: 'page.text.list.json'">
					page.text.list.json
					</a>
				</td>

			</tr>

			<tr>
				<td>
					<a class="btn btn-success" th:href="@{__${addpage}__}" sec:authorize="hasAuthority('MARKETING')"
					th:text="${#messages.msgOrNull('button.book.add')} ?: 'button.book.add'">
					button.book.add
					</a>
				</td>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td>
					<a class="btn btn-dark" th:href="@{__${restpage}__} + '/' + @{__${listpage}__}"
					th:text="${#messages.msgOrNull('button.page.list.json')} ?: 'button.page.list.json'">
					button.page.list.json
					</a>
				</td>
			</tr>

			<tr style="background-color: #FFF !important;">
				<td>
					<a class="btn btn-info" th:href="@{__${apirefpage}__}" sec:authorize="hasAuthority('ADMIN')"
					th:text="${#messages.msgOrNull('button.page.apiref')} ?: 'button.page.apiref'">
					button.page.apiref
					</a>
				</td>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
			</tr>

		</table>
		
		<div sec:authorize="isAnonymous()">
			<p th:text="${#messages.msgOrNull('page.text.list.anon.info')} ?: 'page.text.list.anon.info'">
			page.text.list.anon.info
			</p>
			<img src="http://dreamicus.com/data/abyss/abyss-08.jpg" style="max-width: 600px; height: auto;" alt="abyss">
		</div>
		
	</div>
</body>
</html>
