extends vibenews.admin.layout

block title
	- title = "View articles of " ~ info.groupname;
	
block vibenews.navigation
	ul
		li
			a(href="../../") Admin panel
		li
			a(href="./show") Edit group #{info.groupname}
		li
			a(href="./articles")= title

block vibenews.body
	- import std.algorithm;
	- import std.array : join;
	- import std.string : icmp;
	
	p
		a(href="show") Back to group
	
	- void pageLinks()
		- if( info.pageCount > 1 )
			p.pages Pages: 
				- bool last = true;
				- foreach( i; 0 .. info.pageCount )
					- if( i > 9 && i+10 < info.pageCount && (i+10 < info.page || i > info.page+10) )
						- last = false;
						- continue;
					- if( !last )
						| â€¦
						- last = true;
					- if( info.page == i )
						|= i+1
					- else
						a(href="#{req.rootDir}groups/#{info.groupname}/articles?page=#{i+1}#{info.onlyActive?`&only_active=1`:``}")= i+1

	- pageLinks();

	table
		tr
			th.spam SPAM
			th Date
			th Origin
			th Poster
			th Subject
			th Action
		
		- foreach( art; info.articles )
			- bool is_undecided = !art.hasHeader("X-Spam-Status");
			- bool is_spam = !is_undecided && art.getHeader("X-Spam-Status").icmp("yes") == 0;
			tr(class='#{art.active?"active":"inactive"}')
				td(class=is_undecided ? "undecided" : is_spam ? "spam" : "ham") &nbsp;
				td= art.getHeader("Date")
				td= join(art.peerAddress, ", ")
				td
					- auto from = art.getHeader("From");
					- auto idx = countUntil(from, '<');
					- if (idx >= 0 && idx+1 < from.length)
						a(href="mailto:#{from[idx+1 .. $-1]}")= from[0 .. idx]
					- else
						|= from
				td= art.subject
				td(style="text-align:right")
					form(action='#{req.rootDir}articles/#{art._id}/#{art.active?"deactivate":"activate"}', method="POST")
						input(type="hidden", name="groupname", value="#{info.groupname}")
						input(type="hidden", name="page", value="#{info.page+1}")
						- if (info.onlyActive)
							input(type="hidden", name="only_active", value="1")
						button(type="submit")= art.active ? "Deactivate" : "Activate"
					- if (is_undecided || is_spam)
						form(action='#{req.rootDir}articles/#{art._id}/mark_ham', method="POST")
							input(type="hidden", name="groupname", value="#{info.groupname}")
							input(type="hidden", name="page", value="#{info.page+1}")
							- if (info.onlyActive)
								input(type="hidden", name="only_active", value="1")
							button(type="submit") Mark non-spam
					- if (is_undecided || !is_spam)
						form(action='#{req.rootDir}articles/#{art._id}/mark_spam', method="POST")
							input(type="hidden", name="groupname", value="#{info.groupname}")
							input(type="hidden", name="page", value="#{info.page+1}")
							- if (info.onlyActive)
								input(type="hidden", name="only_active", value="1")
							button(type="submit") Mark Spam

	- pageLinks();

	p
		form(method="GET", action="articles")
			input(type="hidden", name="page", value="1")
			- if (!info.onlyActive)
				input(type="hidden", name="only_active", value="1")
			- if (info.onlyActive)
				button(type="submit") Show all articles
			- else
				button(type="submit") Show only active articles

		form(method="POST", action="#{req.rootDir}groups/#{info.groupname}/articles/mark_page_as_spam")
			input(type="hidden", name="page", value=info.page+1)
			- if (info.onlyActive)
				input(type="hidden", name="only_active", value="1")
			button(type="submit") MARK WHOLE PAGE AS SPAM
