<?php
require_once "../../config/settings.inc.php";
require_once "../../include/database.inc.php";
require_once "../../include/forms.php";
require_once "../../include/network.php";
require_once "../../include/myview.php";

$pgconn = iemdb("hads");
$mesosite = iemdb("mesosite");
$t = new MyView();

$network = get_str404("network", 'IA_DCP', 9);
$minute = get_int404("minute", 0);
$hour = get_int404("hour", date("H"));
$day = get_int404("day", date("d"));
$month = get_int404("month", date("m"));
$year = get_int404("year", date("Y"));


$now = time() - 3600;
$ts = mktime($hour, $minute, 0, $month, $day, $year);
$nt = new NetworkTable(array($network));
$cities = $nt->table;

$t->title = "GOES/DCP/HADS Display Observations";

$dstr = date("d M Y h:i a", $ts);

$nselect = "<select name=\"network\">";
$rs = pg_query($mesosite, "SELECT id, name from networks " .
    "where id ~* '_DCP' or id ~* '_COOP' ORDER by name ASC");
for ($i = 0; $row = pg_fetch_assoc($rs); $i++) {
    $sel = '';
    if ($network == $row["id"]) {
        $sel = " selected='SELECTED'";
    }
    $nselect .= sprintf(
        "<option value='%s'%s>%s</option>\n",
        $row["id"],
        $sel,
        $row["name"]
    );
}
$nselect .= "</select>";
$ys = yearSelect(2002, $year, "year");
$ms = monthSelect($month);
$ds = daySelect($day);
$hs = hourSelect($hour, 'hour');

$ar = array("00" => "00", "15" => "15", "30" => "30", "45" => "45");
$mis = make_select("minute", $minute, $ar);

$tbl = sprintf("raw%s", date("Y_m", $ts));
$dstr = date("Y-m-d H:i", $ts);
$ldate = date("d M Y h:i A", $ts);

$stname = iem_pg_prepare($pgconn, "SELECT t.* from $tbl t, stations s WHERE " .
    "valid = $1 and s.id = t.station and s.network = $2");

$rs = pg_execute($pgconn, $stname, array($dstr, $network));

$data = array();
$datakeys = array();
while ($row = pg_fetch_assoc($rs)) {
    $station = $row["station"];
    if (!array_key_exists($station, $cities)) {
        continue;
    }
    $data[$row["station"]][substr($row["key"], 0, 3)] = $row["value"];
    $datakeys[substr($row["key"], 0, 3)] = 1;
}
$stations = array_keys($data);
$keys = array_keys($datakeys);
asort($stations);
asort($keys);

$table = '<table class="table table-sm table-striped table-bordered">';
$header = '<tr style="background: #eee;">
 <th>NWS ID</th><th>Station</th>';
foreach ($keys as $k => $v) {
    $header .= sprintf('<th>%s</th>', $v);
}
$header .= '</tr>';

$row = 0;
foreach ($stations as $k => $v) {
    if ($row % 25 == 0) {
        $table .= $header;
    }
    $row += 1;

    $table .= sprintf(
        '<tr><td><a href="site.phtml?station=%s&year=%s&month=%s&day=%s">%s</a></td><td>%s</td>',
        $v, date("Y", $ts), date("m", $ts), date("d", $ts), $v, $cities[$v]['name']);
    reset($keys);
    foreach ($keys as $k2 => $key) {
        $table .= sprintf('<td>%s</td>', $data[$v][$key] ?? '');
    }
    $table .= '</tr>';
}
$table .= '</table>';

$t->content = <<<EOM
<div class="breadcrumb">
        <li><a href="/DCP/">DCP/HADS Mainpage</a></li>
        <li class="active">View All Reports at One Time</li>
    </div>

<h3>DCP Data Report for time: {$dstr}</h3>

<p>This application will display a table of observations valid at a given
time.  The SHEF encoded variable identifier is used to show which sites
reported which variables.  All times displayed are in <strong>central
standard/daylight time</strong>.  If you click on the site identifier, you will
get a listing of all observations for that site for the date of your
choice.

<form method="GET" action="cat.phtml">
<table>
<tr>
  <th>State</th>
  <th>Year</th>
  <th>Month</th>
  <th>Day</th>
  <th>Hour</th>
  <th>Minute</th>
  <td></td>
</tr>

<tr>
  <td>
  Select State: {$nselect}
</td>
  <td>{$ys}</td>
  <td>{$ms}</td>
  <td>{$ds}</td>
  <td>{$hs}</td>
  <td>{$mis}</td>
  <td><input type="Submit" value="View Data"></td>
</tr>

{$table}
</form>

<strong>Legend:</strong>  TAI = Air Temp (F), PCI = Rainfall Counter, PPH = Hourly Precip,
UDI = Wind Direction, USI = Wind Speed

</table>
EOM;
$t->render('full.phtml');
