<& /htmlheader, title => (sprintf "%s TJD %d, %d-%d", ($nn + 1 - 50000), $offset + 1, $offset + $limit) &>

<& modelheader &>
% my $sm_str = '';
<h1> The Milk Carton Page (<% $search_str %>)</h1>
<h2> <a href="<% qq{?nn=55860&search=$search} %>">Night 5861</a> | <a href="<% qq{?nn=55862&search=$search} %>">Night 5863</a>
<h2>
<a href="<% qq{?nn=$nn&search=A} %>">All Asteroids</a> | <a href="<% qq{?nn=$nn&search=I} %>">IPP Only</a> | <a href="<% qq{?nn=$nn&search=J} %>">JT Only</a> | <a href="<% qq{?nn=$nn&search=B} %>">Both</a> | <a href="<% qq{?nn=$nn&search=N} %>">Neither</a>
</h2>

<h3>TJD <% $nn + 1 - 50000 %> (MOPS Night Number <% $nn %>)<br>
<% $utdate_str %>
</h3>

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

<& stamp_pager, rooturi => "?nn=$nn&maxv=$maxv&minv=$minv&search=$search", limit => $limit, offset => $offset, found => $found, nostamp => $nostamp &>

<table border=1 cellspacing=0 cellpadding=2 width="95%">
<tr>
    <th class="darkheader" valign="top"> Tracklet ID </th>
    <th class="darkheader" valign="top"> Asteroid<br>Name </th>
    <th class="darkheader" valign="top"> V<sub>tot</sub> (deg/day) </th>
    <th class="darkheader" valign="top"> Predicted<br>Apparent Mag (V) </th>
    <th class="darkheader" valign="top"> Pos Ang (deg) </th>
    <th class="darkheader" valign="top"> digest2 </th>
    <th class="darkheader" valign="top"> GCR (arcsec) </th>
    <th class="darkheader" valign="top"> IPP-MOPS </th>
    <th class="darkheader" valign="top"> JT </th>
    <th class="darkheader" valign="top"> Stamps </th>
<//tr>
<tr>
% foreach $href (@rows) {
% my $trkId = $href->{tracklet_id};
<td align="center"> 
    <span class="L18bold"><% $trkId %></a></span><br>
    <a target="_blank" href="tracklet?tid=<% $trkId %>">Review</a>
</td>

<td valign="center" align="center"> 
% my $known_name = $href->{object_name}; $known_name =~ s/^SK//;
<span class="L14"><& /mpc_link, desig => $known_name &></span>
</td>

% my $trk = modct_retrieve($inst, trackletId => $trkId);
<td valign="center" align="center"> 
<span class="L14bold"> <% sprintf "%.3f &deg;/day", $href->{v_tot} %> </span><br>
<% sprintf "RA %.3f", $href->{v_ra_dd} %><br>
<% sprintf "DEC %.3f", $href->{v_dec_dd} %>
</td>
<td valign="center" align="center"> <span class="L14bold"> <% sprintf "%.1f V", $href->{appmag} %> </class> </td>
<td valign="center" align="center"> <% sprintf "%.1f&deg;", $href->{pos_ang_deg} %> </td>
<td valign="center" align="center"> <% sprintf "%.1f", $href->{digest} %> </td>
<td valign="center" align="center"> <% sprintf "%.2f&quot;", $href->{gcr_arcsec} %> </td>

<td width=64> <% $href->{sumipp} > 2 ? '<img width=64 height=64 src="/images/red.png">' : '' %> </td>
<td width=64> <% $href->{sumjt} > 2 ? '<img width=64 height=64 src="/images/purple.png">' : '' %> </td>

% if ($nostamp) {
<td align="center"> 
    Hidden
% } else {
<td>
% $trk = modct_retrieve($inst, trackletId => $trkId);
    <& milkstamps_td, size => 64, dets => $trk->detections, short => 1, dlname => ('ipp-' . $href->{object_name}) &>
% }
</td>
</tr>
% }
</table>
<p>

<& stamp_pager, rooturi => "?nn=$nn&maxv=$maxv&minv=$minv&search=$search", limit => $limit, offset => $offset, found => $found, nostamp => $nostamp &>
<hr style="visibility: hidden; clear: both; margin-top: 14px">
<& /htmlfooter &>

<%args>
$nn => 55860
$limit => 100
$offset => 0
$found => undef
$minv => 0.1        # default min deg/day
$maxv => 20
$nostamp => undef      # set to show no postage stamps
$field_id => undef      # single field_id
$search => 'A'
</%args>

<%init>
use List::Util qw(sum);
use URI::Escape;
use Astro::Time;
use PS::MOPS::DC::Orbit;
use PS::MOPS::Lib qw(:all);
use PS::MOPS::Constants qw(:all);
use PS::MOPS::DC::Field;
use PS::MOPS::DC::Tracklet;
use PS::MOPS::DC::Known;


my $ISS_Q_THRESHOLD = 5.5;      # don't submit anything with known q > this
my $MPC_DIGEST_THRESHOLD = 50;  # NEO digest threshold

# If $offset and $inc are defined, use them for the mysql query.
# Otherwise use $limit, then as a last resort, all of em.
my $limit_str = '';
my $found_str = '';

if (defined($offset)) {
    $limit = 100 unless $limit;
    $limit_str = sprintf 'limit %d,%d', $offset, $limit;
}
else {
    $limit_str = $limit ? "limit $limit" : '';
}

my $field_sel = '';
if ($field_id) {
    $field_sel = "and field_id=$field_id";
}


my $order_str;
my $search_str = '?';
if ($search eq 'A') {
    $order_str = 'order by appmag';
    $search_str = 'Showing All Asteroids';
}
elsif ($search eq 'I') {
    $order_str = 'having sumipp > 2 and sumjt <= 2 order by appmag';
    $search_str = 'Showing IPP Only';
}
elsif ($search eq 'J') {
    $order_str = 'having sumjt > 2 and sumipp <= 2 order by appmag';
    $search_str = 'Showing JT Only';
}
elsif ($search eq 'B') {
    $order_str = 'having sumjt > 2 and sumipp > 2 order by appmag';
    $search_str = 'Showing Both IPP and JT';
}
elsif ($search eq 'N') {
    $order_str = 'having sumjt <= 2 and sumipp <= 2 order by appmag';
    $search_str = 'Showing Neither IPP nor JT';
}

my $utdate_str = mopslib_mjd2utctimestr($nn + 1);
$utdate_str =~ s/T.*//; # trim everything after date
$utdate_str .= 'UT';

if (!defined($found)) {
    $found_str = 'SQL_CALC_FOUND_ROWS';
}

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

my $substh = $dbh->prepare(<<"SQL") or die $dbh->errstr;
select disposition, desig, mpc_desig from export.mpc_sub where dbname=? and tracklet_id=? limit 1
SQL

my $sql = <<"SQL";
select $found_str
    t.tracklet_id tracklet_id,
    t.digest digest,
    t.v_tot v_tot,
    t.v_ra v_ra_dd,
    t.v_dec v_dec_dd,
    t.pos_ang_deg pos_ang_deg,
    t.gcr_arcsec gcr_arcsec,
    avg(d.mag) appmag,
    s.object_name object_name,
    f.se_deg sol_elong,
    f.eb_deg ecl_lat,
    f.fpa_id,
    sum(if(proc_id&1!=0,1,0)) sumipp,
    sum(if(proc_id&2!=0,1,0)) sumjt
from fields f
    join tracklets t ignore index(classification) using(field_id)
    join tracklet_attrib ta using(tracklet_id)
    left join known k using(known_id)
    join ssm s on t.ssm_id=s.ssm_id
    join detections d on ta.det_id=d.det_id
where f.nn = $nn and t.classification='C'
$field_sel
group by t.tracklet_id
$order_str
$limit_str
SQL
my $sth = $dbh->prepare($sql) or die $dbh->errstr;
$sth->execute() or die $sth->errstr;

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

if ($found_str) {
    ($found) = $dbh->selectrow_array("select FOUND_ROWS()");
}

</%init>
