<div class="content">
	<div class="banner">
		<div class="container">
			<h1>归档</h1>
		</div>
	</div>
	<div class="container">
		<article class="post archive">
			<%- list_tags({
				show_count: true,
				style: "unordered list",
				separator: ", "
			}) %>

			<% if(theme.archives.switch) { %> 
				<p>当前默认显示的分类为：
					<% for (index in theme.archives.categories) { %>
						<%- theme.archives.categories[index] %>
					<% } %>
					（<a class="display-btn" onclick="displayAll()">显示所有</a>）
				</p>
			<% } %> 

			<% // 参考：https://github.com/SukkaW/hexo-theme-suka/blob/master/layout/_pages/archive.ejs %>
			<% function buildArchive(posts, year) { %>
				<h2><%= year %></h2>
				<ul  class="catalogue">
				<% posts.sort('date',-1).each(post => { %>
					<% post.categories.map(function(category){  %>
						<% 
							// 判断是否为已设置需要显示的分类
							let displayTF = false;
							if(theme.archives.switch) {
								for (index in theme.archives.categories) { 
									if(category.name == theme.archives.categories[index]){ 
										displayTF = true;
									}
								} 
							} else {
								displayTF = true;
							}
						%> 
						<% if(displayTF){ %> 
							<li class="display"><a href="<%- url_for(post.path) %>" >
								<span class="title"><%- category.name %>：<%- post.title %></span>
								<span class="date"><span><%= date(post.date, 'MM-DD') %></span></span>
							</a></li>
						<% } else { %>
							<li class="hidden"><a href="<%- url_for(post.path) %>" >
								<span class="title"><%- category.name %>：<%- post.title %></span>
								<span class="date"><span><%= date(post.date, 'MM-DD') %></span></span>
							</a></li>
						<% } %>
					<% }) %>
				<% }) %>
				</ul>
			<% } %>

			<%
			if (!page.year) {
				let years = {};
				let allpost = page.tag ? site.tags.findOne({name: page.tag}).posts :  site.posts;
				allpost.each(post => years[post.date.year()] = null);
				for (let year of Object.keys(years).sort((a, b) => b - a)) {
					let posts = allpost.filter(p => p.date.year() == year);
					buildArchive(posts, year)
				}
			} else {
				let year = page.year;
				let posts = site.posts.filter(p => p.date.year() == year);
				buildArchive(posts, year)
			}
			%>
		</article>
	</div>
</div>

<script>

</script>