<& /htmlheader &>

<& modelheader &>
<h2>Nonsynthetic Detections</h2>
<h3>Field <% $field_id %><br>
<% $utdate_str %>
</h3>

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

<& /lib/pager, rooturi => "?field_id=$field_id&status=$status&flags=$flags&flags2=$flags2", limit => $limit, offset => $offset, found => $found, nostamp => $nostamp &>

<table border=1 cellspacing=0 cellpadding=2 width="70%">
<tr>
    <th class="darkheader"> FPA ID </th>
    <th class="darkheader"> Detection ID </th>
    <th class="darkheader"> DIFFIM_ID </th>
    <th class="darkheader"> Status </th>
    <th class="darkheader"> RA </th>
    <th class="darkheader"> DEC </th>
    <th class="darkheader"> Filter </th>
    <th class="darkheader"> Mag </th>
    <th class="darkheader"> S/N </th>
    <th class="darkheader"> Stamps </th>
</tr>
<tr>
% foreach $href (@rows) {
<td align="center"> <span class="L14bold"><% $href->{fpa_id} %></a></span><br> </td>
<td align="center"> <span class="L14bold"><% $href->{det_id} %></a></span><br> </td>
<td align="center"> <span class="L14bold"><% $href->{diffimid} %></a></span><br> </td>
<td align="center"> <% $href->{status} %> </td>
<td>
    <% sprintf "%.6f", $href->{ra_deg} %><br>
    <span class="L10G"><% turn2str(deg2turn($href->{ra_deg}), 'H', 2, 'hms') %>
</td>
<td>
    <% sprintf "%.6f", $href->{dec_deg} %><br>
    <span class="L10G"><% turn2str(deg2turn($href->{dec_deg}), 'D', 2, 'hms') %>
</td>
<td align="center"> <% $href->{filt} %> </td>
<td align="center"> <% sprintf "%.2f", $href->{mag} %> </td>
<td align="center"> <% sprintf "%.2f", $href->{s2n} %> </td>
<td> <& stampmini, size => 80, dets => [ modcd_retrieve($inst, detId => $href->{det_id}) ] &> </td>
</tr>
% }
</table>
<p>
<& /lib/pager, rooturi => "?field_id=$field_id&status=$status&flags=$flags&flags2=$flags2", limit => $limit, offset => $offset, found => $found, nostamp => $nostamp &>
<& /htmlfooter &>

<%args>
$field_id
$status => 'all'
$flags => ''
$flags2 => ''
$limit => 100
$offset => 0
$found => undef
$nostamp => undef      # set to show no postage stamps
</%args>

<%init>
use Astro::Time;
use PS::MOPS::Lib qw(:all);
use PS::MOPS::Constants qw(:all);
use PS::MOPS::DC::Field;
use PS::MOPS::DC::Detection;

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


# 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 = modcf_retrieve($inst, fieldId => $field_id);
my $utdate_str = mopslib_mjd2utctimestr($field->nn + 1);
$utdate_str =~ s/T.*//; # trim everything after date
$utdate_str .= 'UT';

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

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 $status_str = '';
if ($status ne 'all') {
    $status_str = "and d.status='$status'";
}

my $flags_str = '';
if ($flags) {
    $flags_str = "and (d.flags & $flags > 0)";
}

my $flags2_str = '';
if ($flags2) {
    $flags2_str = "and (dr.flags2 & $flags2 > 0)";
}

my $sql = <<"SQL";
select $found_str
f.fpa_id, d.det_id, d.status, d.ra_deg ra_deg, d.dec_deg dec_deg, d.filter_id filt, d.mag, d.s2n, abs(d.proc_id) diffimid
from det_rawattr_v2 dr 
    join detections d using(det_id)
    join fields f using(field_id)
where field_id=?
$status_str
$flags_str
$flags2_str
order by d.status, s2n desc
$limit_str
SQL

my $sth = $dbh->prepare($sql) or die $dbh->errstr;
$sth->execute($field_id) 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>
