<& /htmlheader, title => "Submitter summary, Night $nn" &>

<& modelheader &>

<!-- <% $sql %> -->

<table border=1 cellspacing=0 cellpadding=2 width=400>
<tr>
<th valign="top">Submitter</th><th valign="top">Night</th><th valign="top">All</th><th valign="top">Pairs</th>
</tr>
% for my $row (@rows) {
<td width=100><% $row->{sub2} %></td>
% my $nnn = mopslib_mjd2nn($row->{tjd2} + 50000, $mc->{site}->{gmt_offset_hours});
<td align="center" width=100><% $nnn %></td>
<td align="right"><% qq{<a target="_blank" href="userreview.html?submitter=$row->{sub2}&nn=$nnn">$row->{allt}</a>} %></td>
<td align="right"><% qq{<a target="_blank" href="userreview.html?submitter=$row->{sub2}&nn=$nnn&pairs=1">$row->{pairs}</a> | <a target="_blank" href="userreview.html?submitter=$row->{sub2}&nn=$nnn&pairs=1&known=n">Unknown</a>} %></td>
<tr>
% }
</table>

<& /htmlfooter &>

<%args>
$nn => undef
</%args>

<%init>
use URI::Escape;
use Astro::Time;
use PS::MOPS::Lib qw(:all);
use PS::MOPS::Constants qw(:all);


my $inst = $m->notes('INST');
my $dbname = $inst->dbname;
my $model = $m->notes('MODEL');
my $mc = $inst->getConfig();
my $dbh = $inst->dbh();

my $mjd;
my $mjd1;
if (!defined($nn)) {
    $mjd = now2mjd();
    $nn = mopslib_mjd2nn($mjd, $mc->{site}->{gmt_offset_hours});
}
$mjd1 = $mjd - 30;

my $sql = <<"SQL";
select q2.submitter sub2, q2.tjd tjd2, sum(if(nd=2,1,0)) pairs, sum(1) allt
from (
    select submitter, right(left(fpa_id,5),4) tjd, desig, count(*) nd 
    from export.mpc_sub where epoch_mjd between ? and ? 
    group by desig
) as q2 
group by submitter, tjd 
order by submitter, tjd
SQL
my $sth = $dbh->prepare($sql) or die $dbh->errstr;
$sth->execute($mjd1, $mjd) or die $sth->errstr;

my @rows;
my $href;
while ($href = $sth->fetchrow_hashref()) {
    push @rows, $href;
}
$sth->finish();

# Assemble rows into table for each user.
</%init>
