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

<th:block th:fragment="post-list">
    <main>
        <th:block th:if="${null != pageQuery}">
            <th:block th:each="post, iterStat : ${pageQuery.records}">
                <th:block th:with="postHasPassword=${!PostPassJar.validFromSession(request.session, post)},
                                   postStatistic=${@statisticsService.getPostStatistic(post, '###,###')}">
                    <article th:class="${!postHasPassword}?'post index-page':'post index-page post-lock'"
                    th:attr="post-id=${post.postId},id=${'post-' + post.postId}">
                    <header>
                        <date class="post-date" 
                            th:text="${#temporals.format(post.createTime, 'MMMM d, yyyy', T(java.util.Locale).ENGLISH)}">
                        </date>
                        <h2 class="post-title">
                            <a th:href="@{${post.link}}" th:text="${!postHasPassword ? post.postTitle : ('加密文章 ' + post.postId)}"></a>
                        </h2>
                    </header>
                        <th:block th:if="${!postHasPassword}">
                            <div class="post-content"
                            th:utext="${@markdownService.renderPostPreface(post)}">
                            </div>
                            <div class="statistic">
                                <p>
                                    <span title="阅读量">
                                        <i class="post-icon icon-eyes"></i>
                                        [[${postStatistic.viewCount}]]
                                    </span>
                                    <a th:href="@{${post.link} + '#comment-page'}" title="评论数">
                                        <i class="post-icon icon-comment"></i>
                                        [[${postStatistic.commentCount}]]
                                    </a>
                                    <span title="字数约">
                                        <i class="post-icon icon-character"></i>
                                        [[${postStatistic.wordCount}]]
                                    </span>
                                    <span title="预计阅读时长">
                                        <i class="post-icon icon-time"></i>
                                        [[${postStatistic.readingTime}]]
                                    </span>
                                    <span th:title="${postStatistic.remains == '0' ? '阅读全部' : ('阅读剩余 ' + postStatistic.remains + ' 字') }">
                                        <th:block th:if="${postStatistic.remains == '0'}">
                                            <i class="post-icon icon-read"></i>
                                            <a th:href="@{${post.link}}"> Read All</a>
                                        </th:block>
                                        <th:block th:if="${postStatistic.remains != '0'}">
                                            <i class="post-icon icon-remains"></i>
                                            <a th:href="@{${post.link}}"> Read All</a>
                                        </th:block>
                                    </span>
                                </p>
                            </div>
                        </th:block>
                        <th:block th:if="${postHasPassword}">
                            <div class="post-content">
                                <p>这是一篇加密文章，请输入密码后查看</p>
                            </div>
                            <div class="statistic">
                                <p>
                                    <span title="阅读量">
                                        <i class="post-icon icon-eyes"></i>
                                        [[${postStatistic.viewCount}]]
                                    </span>
                                    <span title="评论数">
                                        <i class="post-icon icon-comment"></i>
                                        [[${postStatistic.commentCount}]]
                                    </span>
                                    <span title="字数约">
                                        <i class="post-icon icon-character"></i>
                                        [[${postStatistic.wordCount}]]
                                    </span>
                                    <span title="预计阅读时长">
                                        <i class="post-icon icon-time"></i>
                                        [[${postStatistic.readingTime}]]
                                    </span>
                                    <span title="阅读全部">
                                        <i class="post-icon icon-read"></i>
                                        <a th:href="@{${post.link}}"> Read All</a>
                                    </span>
                                </p>
                            </div>
                        </th:block>
                    </article>
                </th:block>
            </th:block>
        </th:block>
        <th:block th:if="${null == pageQuery or pageQuery.total == 0}">
            <article class="post index-page">
                <header>
                    <div class="post-content">没有找到内容<br/>No result found.
                    </div>
                </header>
            </article>
        </th:block>
    <ol class="page-navigator"
        th:attr="data-pagination-prefix=${pageQuery.paginationPrefix}">
        <th:block th:if="${pageQuery.pages >= 1}">
            <th:block th:if="${pageQuery.current > 2}">
                <li>
                    <a th:href="@{${pageQuery.paginationPrefix} + 1}">1</a>
                </li>
                <li th:if="${pageQuery.current > 3}">
                    <span>...</span>
                </li>
            </th:block>
            <th:block th:if="${pageQuery.pages > 1}" 
                th:each="page : 
                    ${#numbers.sequence(
                        T(java.lang.Math).max(pageQuery.current - 1, 1L), 
                        T(java.lang.Math).min(pageQuery.current + 1, pageQuery.pages))}" >
                <li th:class="${pageQuery.current == page}?current">
                    <a th:href="@{${pageQuery.paginationPrefix} + ${page}}" th:text="${page}"></a>
                </li>
            </th:block>
            <th:block th:if="${pageQuery.current < pageQuery.pages - 1}">
                <li th:if="${pageQuery.current < pageQuery.pages - 2}">
                    <span>...</span>
                </li>
                <li>
                    <a th:href="@{${pageQuery.paginationPrefix} + ${pageQuery.pages}}" th:text="${pageQuery.pages}"></a>
                </li>
            </th:block>
        </th:block>
    </ol>
    <footer th:replace="~{blog/bleaching/footer::footer}"></footer>
    </main>
</th:block>
</html>