<?php
require_once "../../config/settings.inc.php";
define("IEM_APPID", 83);
require_once "../../include/myview.php";
require_once "../../include/mlib.php";
require_once "../../include/forms.php";

$t = new MyView();
$t->current_network = 'RWIS';
$sortcol = isset($_GET["sortcol"]) ? xssafe($_GET["sortcol"]) : "ts";
$network = isset($_GET['network']) ? xssafe($_GET['network']) : 'IA_RWIS';
$t->title = "RWIS Sortable Currents";
$t->refresh = 600;

$vals = array(
    "tmpf" => "Air Temperature [F]", "dwpf" => "Dew Point Temp [F]",
    "sknt" => "Wind Speed [knots]", "drct" => "Wind Direction [deg]",
    "pday" => "Precip Accum [inches]", "max_sknt" => "Wind Gust [knots]",
    "ts" => "Observation Time [local]", "feel" => "Feels Like", "peak" => "Wind Gust",
    "min_tmpf" => "Today's Low Temperature", "peak_ts" => "Time of Wind Gust",
    "max_tmpf" => "Today's High Temperature", "name" => "Station Name",
    "sid" => "Station ID", "relh" => "Relative Humidity", "vsby" => "Visibility"
);

$nselect = selectNetworkType("RWIS", $network);
$uri = "current.phtml?network={$network}&sortcol=";

$mydata = array();

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

foreach ($jobj["data"] as $bogus => $ob) {
    $key = $ob["station"];
    $mydata[$key] = $ob;
    $mydata[$key]["ts"] = new DateTime($ob["local_valid"]);
    $mydata[$key]["relh"] = $ob["relh"];
    $mydata[$key]["feel"] = $ob["feel"];
    if ($mydata[$key]["dwpf"] > $mydata[$key]["tmpf"]) {
        $mydata[$key]["feel"] = "";
        $mydata[$key]["dwpf"] = "";
    }
    if ($mydata[$key]["relh"] > 100 || $mydata[$key]["relh"] < 0) {
        $mydata[$key]["feel"] = "";
        $mydata[$key]["dwpf"] = "";
        $mydata[$key]["relh"] = "";
    }
    if ($mydata[$key]["tmpf"] > 120 || $mydata[$key]["tmpf"] < -40) {
        $mydata[$key]["feel"] = "";
        $mydata[$key]["dwpf"] = "";
        $mydata[$key]["relh"] = "";
        $mydata[$key]["tmpf"] = "";
        $mydata[$key]["min_tmpf"] = "";
        $mydata[$key]["max_tmpf"] = "";
    }
    if ($mydata[$key]["vsby"] < 0) {
        $mydata[$key]["vsby"] = "";
    } else {
        $mydata[$key]["vsby"] = myround($mydata[$key]["vsby"], 1);
    }
    $mydata[$key]["peak_ts"] = null;
    if ($mydata[$key]["max_gust"] > $mydata[$key]["max_sknt"]) {
        $mydata[$key]["peak"] = $mydata[$key]["max_gust"];
        $mydata[$key]["peak_ts"] = new DateTime($mydata[$key]["local_max_gust_ts"]);
    } else {
        $mydata[$key]["peak"] = $mydata[$key]["max_sknt"];
        if ($mydata[$key]["local_max_sknt_ts"]) {
            $mydata[$key]["peak_ts"] = new DateTime($mydata[$key]["local_max_sknt_ts"]);
        }
    }

    if ($mydata[$key]["sknt"] < 0) {
        $mydata[$key]["sknt"] = "";
        $mydata[$key]["drct"] = "";
    }
}

$finalA = array();
$finalA = aSortBySecondIndex($mydata, $sortcol, "desc");
$i = 0;
$now = time();
$skippedStations = array();
$table = "";
// Loop though all data
foreach ($finalA as $key => $val) {
    $parts = $finalA[$key];
    $tdiff = $now - strtotime($parts["utc_valid"]);

    if ($tdiff > 6900) {
        $skippedStations[$key] = $parts;
        continue;
    }

    $i++;

    $table .= "<tr>";
    $table .= "<td><input type=\"checkbox\" name=\"st[]\"
               value=\"" . $key . "\"></td>";
    $table .= "<td><a href='../sites/site.php?network=" . $network . "&station=" . $key . "'>" . $parts["name"] . "</a></td><td ";
    $format = "h:i A";
    if ($tdiff > 11900) {
        $table .= 'bgcolor="red"';
        $format = "M-d-Y h:i A";
    } else if ($tdiff > 6900) {
        $table .= 'bgcolor="orange"';
    } else if ($tdiff > 1800) {
        $table .= 'bgcolor="green"';
    }
    $table .= ">" . $parts["ts"]->format($format) . "</td>
  <td align='center'>" . myround($parts["tmpf"], 1) . "(<font color=\"#ff0000\">" .
        myround($parts["max_tmpf"], 1) . "</font>/<font color=\"#0000ff\">" .
        myround($parts["min_tmpf"], 1) . "</font>)</td>
 <td>" . myround($parts["dwpf"], 1) . "</td>
 <td>" . myround($parts["feel"], 0) . "</td>
 <td>" . myround($parts["relh"], 0) . "</td>
 <td>" . $parts["vsby"] . "</td>
 <td>" . myround($parts["sknt"], 0);
    if (!is_null($parts["gust"]) && strlen($parts["gust"]) > 0 && $parts["gust"] > 0) {
        $table .= "G" . myround($parts["gust"], 0);
    }
    $table .= "</td>";
    if (! is_null($parts["peak_ts"])){
        $table .= "<td>" . $parts["drct"] . "</td>
        <td>" . myround($parts["peak"], 0) . "@ " . $parts["peak_ts"]->format("h:i A") . "</td>
            <td>" . myround($parts["pday"], 2) . "</td>";
    }
            
    $table .= "</tr>\n";
}

foreach ($skippedStations as $key => $parts) {
    $table .= "<tr>";
    $table .= "<td><input type=\"checkbox\" name=\"st[]\"
               value=\"" . $key . "\"></td>";
    $table .= "<td>" . $parts["name"] . "</td><td ";
    $table .= 'bgcolor="red"';
    $format = "M-d-Y";
    $table .= ">" . $parts["ts"]->format($format) . "</td><th align='center' colspan='9'>Site Offline</th></tr>\n";
}

$content = <<<EOM
<form method="GET" action="current.phtml">
{$nselect}
<input type="submit" value="Show State">
</form>

<form method="GET" action="/my/current.phtml">
<table class="table table-striped table-sm table-bordered">
<thead>
<tr>
  <th rowspan="2">ADD:</th>
  <th rowspan="2"><a href="{$uri}name">Station:</a></th>
  <th rowspan="2"><a href="{$uri}ts">Ob Time</a></th>
  <th colspan="3" bgcolor="#acdcda">Temps &deg;F</th>
  <th>&nbsp;</th>
  <th>&nbsp;</th>
  <th colspan="3" bgcolor="#EEEEEE">Wind [knots]</th>
  <th><a href="{$uri}pday">Precip</a></th>
</tr>
<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</a></th>
  <th><a href="{$uri}relh">RH%</a></th>
  <th><a href="{$uri}vsby">Vis</a></th>
  <th><a href="{$uri}sknt">Speed</a></th>
  <th><a href="{$uri}drct">Direction</a></th>
  <th><a href="{$uri}peak">Gust</a>
    @<a href="{$uri}peak_ts">Time</a></th>
  <th><a href="{$uri}pday">Accum</a> [1]</th>
</tr></thead>
<tbody>
{$table}
</tbody>
</table>
<input type="submit" value="Add to Favorites">
<input type="reset" value="Reset">

</form>

<br><br>

<b>Notes:</b><br>
<b>1</b> Not all sites have the ability to measure precip nor visibility.<br>
<b>2</b> The Iowa City (I-80) RWIS site does not have atmospheric sensors on it
, so it is not included on this page.<br>

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