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

require_once "../../include/database.inc.php";
require_once "../../include/network.php";
require_once "../../include/mlib.php";
$nt = new NetworkTable("ISUSM");

$PUBLIC_URI = "{$EXTERNAL_BASEURL}/api/1/currents.json?network=ISUSM";
$arr = array(
    "network" => "ISUSM",
);
$jobj = iemws_json("currents.json", $arr);

function pretty($val, $fmt)
{
    // Pretty print things for the table
    if (is_null($val)) {
        return "M";
    }
    return sprintf($fmt, $val);
}

$table = <<<EOM
<form method="GET" action="/my/current.phtml" name="my">
<table class='table table-striped table-bordered table-sm'>
<thead class="sticky">
<tr>
<th rowspan="2">Add:</th>
<th rowspan='2'>Location</th>
<th rowspan='2'>Valid</th>
<th colspan='3'>Air Temp &deg;F</th>
<th rowspan="2">Dew Pt &deg;F</th>
<th colspan='3'>Wind MPH</th>
<th rowspan='2'>Today's Rain inch</th>
<th>4 inches</th>
<th colspan='2'>12 inches</th>
<th colspan='2'>24 inches</th>
<th colspan='2'>50 inches</th>
</tr>
<tr>
<th>Ob</th><th>High</th><th>Low</th>
<th>Speed</th><th>Gust</th><th>Today's Peak</th>
<th>Temp &deg;F</th><th>Temp &deg;F</th><th>Moist %</th>
<th>Temp &deg;F</th><th>Moist %</th>
<th>Temp &deg;F</th><th>Moist %</th>
</tr>
</thead>
<tbody>
EOM;
$offline = array();
$threshold = time() - 86400;
foreach ($jobj["data"] as $bogus => $data) {
    $sid = $data["station"];
    $uri = sprintf("/sites/site.php?station=%s&network=ISUSM", $sid);

    $ts = strtotime($data["local_valid"]);
    if ($ts < $threshold) {
        $offline[] = sprintf(
            "<a href=\"%s\">%s</a>",
            $uri,
            $nt->table[$sid]['name']
        );
        continue;
    }
    $gts = is_null($data["local_max_gust_ts"]) ? "": date("g:i A", strtotime($data['local_max_gust_ts']));
    $table .= sprintf(
        "<tr><td><input type=\"checkbox\" name=\"st[]\" value=\"%s\"></td>" .
            "<td><a href=\"%s\">%s</a></td><td>%s</td>" .
            "<td>%s</td><td>%s</td><td>%s</td><td>%s</td>" .
            "<td>%s @ %s</td><td>%s</td><td>%s @ %s</td>" .
            "<td>%s</td><td>%s</td><td>%s</td><td>%s</td>" .
            "<td>%s</td><td>%s</td>" .
            "<td>%s</td><td>%s</td></tr>",
        $sid,
        $uri,
        $nt->table[$sid]['name'],
        date("g:i A", $ts),
        pretty($data["tmpf"], '%.1f'),
        pretty($data["max_tmpf"], '%.1f'),
        pretty($data["min_tmpf"], '%.1f'),
        pretty($data["dwpf"], '%.1f'),
        drct2txt($data["drct"]),
        pretty($data["sknt"] * 1.15, '%.0f'),
        pretty($data['gust'] * 1.15, '%.0f'),
        pretty($data['max_gust'] * 1.15, '%.0f'),
        $gts,
        pretty($data['pday'], '%.2f'),
        pretty($data["c1tmpf"], '%.1f'),
        pretty($data["c2tmpf"], '%.1f'),
        ($data["c2smv"] < 0) ? "Frozen" : pretty($data["c2smv"], '%.1f'),
        pretty($data["c3tmpf"], '%.1f'),
        ($data["c3smv"] < 0) ? "Frozen" : pretty($data["c3smv"], '%.1f'),
        pretty($data["c4tmpf"], '%.1f'),
        ($data["c4smv"] < 0) ? "Frozen" : pretty($data["c4smv"], '%.1f')
    );
}

$table .= <<<EOM
</tbody>
</table>
<input type="submit" value="Add to Favorites">
<input type="reset" value="Reset">

</form>
EOM;
if (sizeof($offline) > 0) {
    $table .= sprintf("<br />Sites Offline: %s", implode(", ", $offline));
}
$v = isset($_REQUEST["v"]) ? htmlentities($_REQUEST["v"]) : 'tmpf';
$t = new MyView();
$t->current_network = "ISU Soil Moisture";
$t->title = "ISU Ag Climate Sortable Currents";
$t->refresh = 300;

$t->breadcrumbs = <<<EOM
 <ol class="breadcrumb">
 <li><a href="/agclimate/">ISU Soil Moisture Network</a></li>
 <li class="active">Sortable Currents</li>
 </ol>
EOM;
$t->content = <<<EOM
<p>This table provides the most recent hourly observations collected from the
network.  The data was provided by <a href="{$PUBLIC_URI}">this JSON webservice</a>.</p>

<br />
{$table}

EOM;

$t->render('sortables.phtml');
