package Gruntmaster::Page::Log;

use 5.014000;
use strict;
use warnings;
use Gruntmaster::Page::Base log => 'Job log';
our @ISA = qw/Gruntmaster::Page::Base/;
our $VERSION = '0.001';

use constant PAGE_SIZE => 10;

use constant TEMPLATES => {
	en => <<'HTML',
<table border class="table table-bordered table-striped">
<thead>
<tr><th>ID<th>Problem<th>Date<th>Size<th>User<th>Result
<tbody>
<tmpl_loop log><tr><td><a href="job/<tmpl_var id>"><tmpl_var id></a>
<td><a href="../pb/<tmpl_var problem>"><tmpl_var name></a>
<td><tmpl_var date>
<td><a href="src/<tmpl_var id>.<tmpl_var extension>"<tmpl_if private> data-private</tmpl_if>><tmpl_var size></a>
<td><tmpl_var user><td class="r<tmpl_var result>"><tmpl_var result_text>
</tmpl_loop>
</table>

<ul class="pager">
<tmpl_if prev><li class="previous"><a href="<tmpl_var prev>">Previous</a></tmpl_if>
<tmpl_if next><li class="next"><a href="<tmpl_var next>">Next</a></tmpl_if>
</ul>
HTML
};

sub _generate{
	my ($self, $htc, $lang, $env, $ct, $page) = @_;
	debug $env => "language is '$lang', contest is '$ct' and page is '$page'";
	local $Gruntmaster::Data::contest = $ct if $ct;

	my $pages = POSIX::floor (jobcard / PAGE_SIZE);
	$pages ||= 1;
	$page ||= $pages;

	my @log = sort { $b->{id} <=> $a->{id} } map +{
		id => $_,
		(job_private() ? (private => job_private) : ()),
		date => (job_date() ? strftime ('%c' => localtime job_date) : '?'),
		extension => job_extension,
		name => problem_name job_problem,
		problem => job_problem,
		result => job_result,
		result_text => job_result_text,
		size => sprintf ("%.2f KiB", job_filesize() / 1024),
		user => job_user}, ($page - 1) * PAGE_SIZE + 1 .. ($page == $pages ? jobcard : $page * PAGE_SIZE);
	$htc->param(log => \@log);
	$htc->param(next => $page + 1) unless $page == $pages;
	$htc->param(prev => $page - 1) unless $page == 1;
}

1
