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

$t = new MyView();
$sortcol = isset($_GET["sortcol"]) ? xssafe($_GET["sortcol"]) : "state";
$sorder = isset($_GET["sorder"]) ? xssafe($_GET["sorder"]) : "asc";

$t->refresh = 60;
$t->title = "US Climate Reference Network Latest Reports";

$arr = array("network" => "USCRN");
$jobj = iemws_json("currents.json", $arr);

$vals = array(
    "tmpf" => "Air Temperature [°F]", "dwpf" => "Dew Point Temp [°F]",
    "sknt" => "Wind Speed [knots]", "drct" => "Wind Direction [deg]",
    "srad" => "Solar Radiation [W / m2]", "peak" => "Today's Wind Gust [knots]",
    "peak_ts" => "Time of Peak Gust", "relh" => "Relative Humidity",
    "feel" => "Feels Like [°F]", "vsby" => "Visibility [miles]",
    "ts" => "Observation Time", "phour" => "Last Hour Rainfall [inch]",
    "min_tmpf" => "Today's Low Temperature",
    "max_tmpf" => "Today's High Temperature", "name" => "Station Name",
    "state" => "Station State",
    "pday" => "Today Rainfall [inch]"
);

$t->current_network = 'USCRN';

$ar = array(
    "asc" => "Ascending",
    "desc" => "Descending"
);
$aselect = make_select("sorder", $sorder, $ar);

$uri = "uscrn.phtml?sorder=$sorder&sortcol=";

$mydata = array();
foreach ($jobj["data"] as $bogus => $iemob) {
    $key = $iemob["station"];
    $mydata[$key] = $iemob;
    if ($mydata[$key]["relh"] < 5) {
        $mydata[$key]["relh"] = "M";
        $mydata[$key]["dewpf"] = "M";
        if ($sortcol == "feel" || $sortcol == "dwpf" || $sortcol == "relh") {
            $mydata[$key]["feel"] = null;
        }
    }
    if ($mydata[$key]["tmpf"] < -60) {
        $mydata[$key]["tmpf"] = "M";
        if ($sortcol == "tmpf" || $sortcol == "feel" || $sortcol == "dwpf" || $sortcol == "relh") {
            $mydata[$key]["tmpf"] = null;
        }
    }
    if ($mydata[$key]["alti"] < -60) {
        $mydata[$key]["alti"] = "M";
        if ($sortcol == "alti") {
            $mydata[$key]["alti"] = null;
        }
    }
    if ($mydata[$key]["vsby"] < 0) {
        $mydata[$key]["vsby"] = "M";
        if ($sortcol == "vsby") {
            $mydata[$key]["vsby"] = null;
        }
    }

    if ($mydata[$key]["max_gust"] > $mydata[$key]["max_sknt"]) {
        $mydata[$key]["peak"] = $mydata[$key]["max_gust"];
        $mydata[$key]["peak_ts"] = strtotime($mydata[$key]["local_max_gust_ts"]);
    } else {
        $mydata[$key]["peak"] = $mydata[$key]["max_sknt"];
        $mydata[$key]["peak_ts"] = null;
        if ($mydata[$key]["local_max_sknt_ts"]) {
            $mydata[$key]["peak_ts"] = strtotime($mydata[$key]["local_max_sknt_ts"]);
        }
    }
}

$finalA = array();
$finalA = aSortBySecondIndex($mydata, $sortcol, $sorder);
$now = time();
$i = 0;
$table = "";
foreach ($finalA as $key => $val) {
    $i++;
    $parts = $finalA[$key];

    $table .= "<tr";
    if ($i % 2 == 0)  $table .= " bgcolor='#eeeeee'";
    $table .= "><td><input type=\"checkbox\" name=\"st[]\" value=\"" . $key . "\"></td>";

    $tdiff = $now - strtotime($parts["local_valid"]);
    $table .= sprintf(
        "<td>%s [%s] (<a href=\"/sites/site.php?station=%s&amp;" .
            "network=%s\">%s</a>)</td>",
        $parts["name"],
        $parts["state"],
        $key,
        "USCRN",
        $key
    );
    $table .= "<td ";
    if ($tdiff > 10000) {
        $fmt = "d M h:i A";
    } else {
        $fmt = "h:i A";
    }

    $table .= ">" . date($fmt, strtotime($parts["local_valid"])) . "</td>
     <td align='center'>" . myround($parts["tmpf"], 0) . "(<font color=\"#ff0000\">" . myround($parts["max_tmpf"], 0) . "</font>/<font color=\"#0000ff\">" . myround($parts["min_tmpf"], 0) . "</font>)</td>
     <td>" . myround($parts["dwpf"], 0) . "</td>
     <td>" . myround($parts["feel"], 0) . "</td>
        <td>" . $parts["srad"] . "</td>
             <td>" . myround($parts["sknt"], 0);
    if (strlen($parts["gust"] != 0)) {
        $table .= "G" . myround($parts["gust"], 0);
    }
    $table .= "</td>";
    $table .= "<td>" . myround($parts["peak"], 0) . " @ " . date("h:i A", $parts["peak_ts"]) . "</td>
        </tr>\n";
}
$t->content = <<<EOM

<form method="GET" action="uscrn.phtml" name="work">
<input type="hidden" value="{$sortcol}" name="sortcol">

Sort Order: {$aselect}

<input type="submit" value="Go!">
</form>

<p>The displayed timestamps are valid for the local time zone of the weather
station.</p>

<form method="GET" action="/my/current.phtml">

<table class="table table-bordered table-striped">
<thead>
<tr>
  <th rowspan="2">ADD:</th>
  <th rowspan="2">Station:</th>
  <th rowspan="2"><a href="{$uri}ts">Ob Time</a></th>
  <th colspan="3">Temps &deg;F</th>
  <th colspan="1">&nbsp;</th>
  <th colspan="2">Wind [knots]</th>
<tr>
  <th>
 <a href="{$uri}tmpf">Air</a>
 (<a href="{$uri}max_tmpf">Hi</a> / <a href="{$uri}min_tmpf">Lo</a>)
</th>
  <th><a href="{$uri}dwpf">Dewp</a></th>
  <th><a href="{$uri}feel">Feels Like</a></th>
  <th><a href="{$uri}srad">Solar Rad</a></th>
  <th><a href="{$uri}sknt">Speed</a></th>
  <th><a href="{$uri}peak">Gust</a>
    @ <a href="{$uri}peak_ts">Time</a></th>
</tr></thead>
<tbody>
{$table}
</tbody>
</table>

<input type="submit" value="Add to Favorites">
<input type="reset" value="Reset">

</form></div>
EOM;
$t->render('sortables.phtml');
