% if ($report) {
<html>
<head>
<style type="text/css">
<& '/css/report.css' &>
</style>
</head>
<body>
% } else {
<& /htmlheader, title => $htmltitle &>
% }

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

<h2><% $title %></h2>
% if (!$all) {
<span class="L12">
TJD <% $tjd %> (MOPS Night Number <% $tjd + 50000 - 1 %>)<br>
<% $utdate_str %>
</span><p>
% }

% unless ($report) {
<form style='display: inline' action="submitted_details.html">Find submitted designation (e.g. "P100105"): <input type="text" name="desig" size="10"> | <a href="/subsearch_mpc.html">Search using MPC observation records</a>
</form>
<p>
% } # unless ($report)

<table><tr><td width="500">
<b>Disclaimer: this report is a summary of data submitted by PS1 to the Minor Planet Center (MPC).  It is not an official record of PS1 NEO candidates as determined by the MPC.  Digest values may differ between PS1 and MPC, and not all submitted objects by PS1 are guaranteed to be deemed NEO candidates.
</b>
<p>
</td></tr></table>

% unless ($tjd) { # don't show totals table if single-night


% if ($discsum) {
<table border=1 cellspacing=0 cellpadding=2 width="30%">
<tr>
<th class="darkheader"> Discoveries by Type </th>
<th class="darkheader"> Count </th>
</tr>
%   foreach my $row (@discoveries) {
<tr>
<td><% $dispmap{$row->{disposition}} %></td>
<td align="right"><% $row->{ct} %></td>
</tr>
%   } # for
<tr><td><b>Total</b>
% unless ($report) {
<a href="/submitted_neos.html?disp_sel=CARM">List All</a>
% }
</td><td align="right"><b><% $discsum %></b></td><tr>
</table>
<p><p>
% } # discsum


% if ($discsum_A) {
<table border=1 cellspacing=0 cellpadding=2 width="30%">
<tr>
<th class="darkheader"> PHAs by Survey </th>
<th class="darkheader"> Count </th>
</tr>
%   foreach my $row (@disc_A) {
<tr>
<td><% $row->{sm} %></td>
<td align="right"><% $row->{ct} %></td>
</tr>
%   } # for
<tr><td><b>Total</b>
% unless ($report) {
<a href="/submitted_neos.html?disp_sel=CARM">List All</a>
% }
</td><td align="right"><b><% $discsum_A %></b></td><tr>
</table>
<p><p>
% } # discsum_A


% if ($discsum_C) {
<table border=1 cellspacing=0 cellpadding=2 width="30%">
<tr>
<th class="darkheader"> NEOs by Survey </th>
<th class="darkheader"> Count </th>
</tr>
%   foreach my $row (@disc_C) {
<tr>
<td><% $row->{sm} %></td>
<td align="right"><% $row->{ct} %></td>
</tr>
%   } # for
<tr><td><b>Total</b>
% unless ($report) {
<a href="/submitted_neos.html?disp_sel=CARM">List All</a>
% }
</td><td align="right"><b><% $discsum_C %></b></td><tr>
<tr><td colspan=2><b>Note: tracklets spanning two survey modes will be counted twice</b></td></tr>
</table>
<p><p>
% } # discsum_C

% if (@incidental) {
<table border=1 cellspacing=0 cellpadding=2 width="30%">
<tr>
<th class="darkheader"> Other Submissions </th>
<th class="darkheader"> Count (Tracklets) </th>
</tr>
%   foreach my $row (grep { $_->{disposition} ne 'S' } @incidental) {
<tr>
<td><% $dispmap{$row->{disposition}} %></td>
<td align="right"><% $row->{ct} %></td>
</tr>
%   } # for
</table>
% } # incidental


<p><p>
% } # if ($tjd)

% unless ($report) {
<& /lib/pager, rooturi => "?disp_sel=$disp_sel", limit => $limit, offset => $offset, found => $found &>
% }

<table border=1 cellspacing=0 cellpadding=2 width="90%">
<tr>
    <th class="darkheader"> Internal<br>Designation </th>
    <th class="darkheader"> Database/Tracklet ID </th>
    <th class="darkheader"> Observation<br>Date </th>
    <th class="darkheader"> Source Chunk </th> </th>
    <th class="darkheader"> Digest2 </th>
    <th class="darkheader"> Disposition </th>
    <th class="darkheader"> MPC<br>Designation </th>
</tr>
% foreach $href (@rows) {
% my $trk_dbname = $href->{dbname};
% my $trkId = $href->{tracklet_id};
<tr>

<td valign="center" align="center">
% if ($report) {
    <% $href->{desig} %>
% } else {
    <a href="/czar/manage_neo.html?desig=<% $href->{desig} %>"><% $href->{desig} %></a>
% }
</td>
<td valign="center" align="left">
% unless ($report) {
%   if ($href->{dbname} and $href->{tracklet_id}) {
    <a href="<% qq{/model/$href->{dbname}/search?id=T$href->{tracklet_id}} %>"><% "$href->{dbname}/T$href->{tracklet_id}" %></a>
%   } else {
    NA
%   }
% } else {
    <% "$href->{dbname}/T$href->{tracklet_id}" %>
% }
</td>
<td valign="center" align="left">
    <% mopslib_mjd2utctimestr($href->{epoch_mjd}, ' ') %>
</td>
<td valign="center" align="left">
    <% $href->{survey_mode} %>
</td>
<td valign="center" align="center">
    <% defined($href->{digest}) ? sprintf("%.1f", $href->{digest}) : 'NA' %>
</td>
% my $bgstyle;
% if ($href->{disposition} eq 'C') { $bgstyle = 'style="background-color: #9ACD32"' }
% elsif ($href->{disposition} eq 'R') { $bgstyle = 'style="background-color: #B0C4DE"' }
% elsif ($href->{disposition} eq 'A') { $bgstyle = 'style="background-color: #FF4500"' }
% elsif ($href->{disposition} eq 'M') { $bgstyle = 'style="background-color: #CDC9C9"' };
<td valign="center" <% $bgstyle %> align="center">
    <% $dispmap{$href->{disposition}} || 'NA' %>
</td>
<td valign="center" align="center">
<& /mpc_link, desig => $href->{mpc_desig}, neocp_desig => $href->{desig} &>
</td>
</tr>

% if ($report) {
<tr><td colspan=7>
<pre>
% my $dets = $dbh->selectall_arrayref($det_sql, { Slice => {} }, $href->{desig});
% foreach my $h (@$dets) {
<% PS::MOPS::MPC::det2mpc($h, $href->{desig}) %>
% } # foreach
</pre>
</td></tr>
% } # if report

% } # foreach
</table>

% unless ($report) {
<& /lib/pager, rooturi => "?disp_sel=$disp_sel", limit => $limit, offset => $offset, found => $found &>
<p>
%   if (!$all) {
<a href="?all=1">All NEO submissions to date</a>
%   }
% }

% if ($report) {
Report generated <% scalar gmtime %> UT.<p>
</body>
</html>
% } else {
<& /htmlfooter &>
% }

<%args>
$tjd => undef
$limit => 100
$offset => 0
$found => undef
$report => undef
$disp_all => undef
$disp_sel => ''
$summary => 1
</%args>

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

my %dispmap = (
    C => 'DISCOVERY, NEO (non-PHA)',
    A => 'DISCOVERY, PHA',
    M => 'DISCOVERY, COMET',
    R => 'RECOVERY, NEO',
    N => 'DISCOVERY, NON-NEO',
    '1' => 'NEOCP (1-NIGHTER)',
    '2' => 'NEOCP (FOLLOWED UP)',
    S => 'SUBMITTED',
    T => 'SUBMITTED (INCIDENTAL)',
    K => 'KNOWN',
    D => 'DOWNGRADED',
    O => 'OUTREACH',
    J => 'REJECTED',
);
my $inst = PS::MOPS::DC::Instance->new(DBNAME => 'psmops_export');
my $dbh = $inst->dbh();


# Get a tally of totals so far.
my @discoveries;
my @disc_A;
my @disc_C;
my @incidental;
my $tots;
my $tots_A;
my $tots_C;

if ($summary and !$tjd and $offset == 0) {
    $tots = $dbh->selectall_arrayref(<<"SQL", { Slice => {} });
select disposition, count(distinct desig) ct
from export.mpc_sub group by disposition
order by ct desc
SQL

    $tots_A = $dbh->selectall_arrayref(<<"SQL", { Slice => {} });
select 
    if(survey_mode regexp 'SS|3PI',substring_index(substring_index(survey_mode, '.', 2), '.', -1),'Other') sm, 
    count(distinct desig) ct 
from export.mpc_sub 
where disposition='A' 
group by if(survey_mode regexp 'SS|3PI',substring_index(substring_index(survey_mode, '.', 2), '.', -1),'Other')
order by ct desc
SQL

    $tots_C = $dbh->selectall_arrayref(<<"SQL", { Slice => {} });
select 
    if(survey_mode regexp 'SS|3PI',substring_index(substring_index(survey_mode, '.', 2), '.', -1),'Other') sm, 
    count(distinct desig) ct 
from export.mpc_sub 
where disposition='C' 
group by if(survey_mode regexp 'SS|3PI',substring_index(substring_index(survey_mode, '.', 2), '.', -1),'Other')
order by ct desc
SQL
}

my $discsum = 0;
foreach my $row (@$tots) {
    if ($row->{disposition} =~ /^(C|A|R)$/) {
        push @discoveries, $row;
        $discsum += $row->{ct};
    }
    else {
        push @incidental, $row;
    }
}

my $discsum_A = 0;
foreach my $row (@$tots_A) {
    push @disc_A, $row;
    $discsum_A += $row->{ct};
}

my $discsum_C = 0;
foreach my $row (@$tots_C) {
    push @disc_C, $row;
    $discsum_C += $row->{ct};
}

my $limit_str;
if ($report) {
    $limit = 250;
    $offset = 0;
}

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

my $where = '';
my $utdate_str;
my $title;
my $htmltitle = 'Submitted NEOs';
my $all;

if (!defined($tjd)) {
    $utdate_str = 'To date';
    if ($disp_sel =~ /^CAR/) {
        $title = 'PS1 Discoveries';
        $htmltitle = $title;
    }
    else {
        $title = $report ? "$limit most recent PS1 NEO submissions" : 'PS1 Submitted NEOs to Date';
    }
    $all = 1;
}
else {
    $utdate_str = mopslib_mjd2utctimestr($tjd + 50000);
    $utdate_str =~ s/T.*//; # trim everything after date
    $utdate_str .= 'UT';
    my $epoch1 = $tjd + 50000;
    my $epoch2 = $epoch1 + 1;
    $where = "epoch_mjd between $epoch1 and $epoch2 and ";
    $title = 'PS1 Submitted NEOs';
}

my $disp_str;
if ($disp_sel) {
    # convert ABC to 'A', 'B', 'C'
    $disp_str = sprintf "disposition in (%s)", join ',', map { qq{'$_'} } split //, $disp_sel;
}
else {
    $disp_str = $disp_all ? "disposition <> 'J'" : "disposition not in ('T', 'J', 'O', 'D')";
}

# SQL to get dets only.
my $det_sql = <<"SQL";
select 
    epoch_mjd as epoch,
    ra_deg as ra,
    dec_deg as `dec`,
    filter_id as filter,
    mag as mag,
    obscode as obscode
from export.mpc_sub 
where desig=?
order by epoch_mjd
SQL

my $sql = <<"SQL";
select $found_str
    epoch_mjd,
    survey_mode,
    ra_deg,
    dec_deg,
    filter_id,
    mag,
    obscode,
    desig,
    mpc_desig,
    digest,
    dbname,
    tracklet_id,
    disposition
from export.mpc_sub 
where $where $disp_str
group by desig
order by epoch_mjd desc
$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>
