package Gruntmaster::Page::Submit;

use 5.014000;
use strict;
use warnings;
use parent qw/Exporter/;
our @EXPORT_OK = qw/generate/;
our $VERSION = '0.001';

use constant FORMATS => [qw/CPP/];
use constant TITLE => 'Submit job';

use Fcntl qw/:flock/;
use HTML::Template::Compiled;
use IO::File;
use YAML::Any qw/LoadFile/;
use Gruntmaster::Page qw/header footer/;

my %templates = (
  en => <<'HTML',
<form action="https://gm.ieval.ro/action/submit" method="POST" enctype="multipart/form-data">
<label>Problem:<br>
<select name="problem" required>
<tmpl_loop problems><option value="<tmpl_var id>"><tmpl_var name></option>
</tmpl_loop></select></label><p>

<label>File:<br>
<input name="prog" required type="file"></label><p>

<label>File format:<br>
<select name="prog_format" required>
<tmpl_loop formats><option value="<tmpl_var _>"><tmpl_var _></option>
</tmpl_loop></select></label><p>

<input type="submit" value="Submit job">
HTML
);

$templates{$_}  = header($_, TITLE) . $templates{$_} for keys %templates;
$templates{$_} .= footer $_ for keys %templates;

sub generate{
  my $template = $templates{$_[1]};
  my $htc = HTML::Template::Compiled->new(scalarref => \$template);
  IO::File->new('>meta.yml')->close unless -f 'meta.yml';
  flock my $metafh = IO::File->new('<meta.yml'), LOCK_SH;
  my @problems = map {
	my $meta = LoadFile $_;
	my $id = (m,^pb/(.*)/meta.yml$,)[0];
	+{ id => $id, name => $meta->{name} } } <pb/*/meta.yml>;
  $htc->param(problems => \@problems);
  $htc->param(formats => FORMATS);
  $htc->output
}

1
