<& /htmlheader, title => "$desig Details" &>

<h2><% $desig %></h2>

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

<form action='update_neo' method='POST'>
<input type="hidden" name="desig" value="<% $desig %>">
<input type="hidden" name="submitter" value="<% $href->{submitter} || $ENV{REMOTE_USER} || 'MOPS' %>">
<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"> Submitter </th>
    <th class="darkheader"> MPC<br>Designation </th>
</tr>
% my $trk_dbname = $href->{dbname};
% my $trkId = $href->{tracklet_id};
<tr>

<td valign="center" align="center">
    <% $href->{desig} %>
</td>
<td valign="center" align="center">
% 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
% }
</td>
<td valign="center" align="center">
    <% mopslib_mjd2utctimestr($href->{epoch_mjd}, ' ') %>
</td>
<td valign="center" align="center">
    <% $href->{survey_mode} %>
</td>
<td valign="center" align="center">
    <% defined($href->{digest}) ? sprintf("%.1f", $href->{digest}) : 'NA' %>
</td>
<td valign="center" align="center">
    <select name="disposition">
% foreach my $sel (@dispsel) {
        <option value="<% $sel %>" <% $href->{disposition} eq $sel ? 'selected' : ''%>><% $dispmap{$sel} %></option>
% }
    </select>
</td>
<td valign="center" align="center">
    <% $href->{submitter} || 'MOPS' %>
</td>
<td valign="center" align="center">
<input type=text name="mpc_desig" size=20 value="<% $href->{mpc_desig} %>">
<!-- <& /mpc_link, desig => $href->{mpc_desig} &> -->
</td>
</tr>

<tr>
<td colspan=8 style='padding: 8'>
<div style='font-size: 14'> <p> <b>Observations<% $href->{revised} ? qq{<span style="color: red"> (REVISED)</span>} : '' %>:</b> <p> <pre>
% foreach my $ob (@obs) {
<% PS::MOPS::MPC::det2mpc($ob, $desig) %>
% }
</pre> <p> </div>
</td>
</tr>
</table>
<p>
<input type="submit" value="Update Record">
</form>

<& /htmlfooter &>

<%args>
$desig
</%args>

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

my %dispmap = (
    '1' => 'NEOCP (1-NIGHTER)',
    '2' => 'NEOCP (FOLLOWED UP)',
    S => 'SUBMITTED',
    T => 'SUBMITTED, NON-NEO',
    C => 'DISCOVERY, NEO',
    A => 'DISCOVERY, PHA',
    M => 'DISCOVERY, COMET',
    N => 'DISCOVERY, NON-NEO',
    R => 'RECOVERY, NEO',
    K => 'KNOWN',
    D => 'DOWNGRADED',
    O => 'OUTREACH',
    J => 'REJECTED',
);
my @dispsel = qw(1 2 S T C A M N R K D O J);    # allowed dispositions

# LD XXX need to wrap this into PS::MOPS::MPC::Export.
my $inst = PS::MOPS::DC::Instance->new(DBNAME => 'psmops_export');
my $dbh = $inst->dbh();

my @obs;
my $sql = <<"SQL";
select
    epoch_mjd,
    survey_mode,
    ra_deg,
    dec_deg,
    filter_id,
    mag,
    obscode,
    desig,
    mpc_desig,
    digest,
    dbname,
    tracklet_id,
    disposition,
    submitter,
    revised
from export.mpc_sub 
where desig=?
order by epoch_mjd desc
SQL

my $sth = $dbh->prepare($sql) or die $dbh->errstr;
$sth->execute($desig) or die $sth->errstr;

my $href;
while (my $h = $sth->fetchrow_hashref()) {
    push @obs, { 
        epoch => $h->{epoch_mjd}, 
        ra => $h->{ra_deg}, 
        dec => $h->{dec_deg}, 
        mag => $h->{mag}, 
        filter => $h->{filter_id}, 
        obscode => $h->{obscode}
    };
    $href = $h unless $href;        # keep first one only as ref to group
}
$sth->finish();

</%init>
