<?php
require_once "../../config/settings.inc.php";
require_once "../../include/myview.php";
require_once "../../include/mlib.php";
require_once "../../include/network.php";
require_once "../../include/forms.php";
$nt = new NetworkTable("SCAN");
$cities = $nt->table;

$sortcol = isset($_GET["sortcol"]) ? xssafe($_GET["sortcol"]) : 'station';
$t = new MyView();
$t->title = "SCAN Sortable Currents";
$t->refresh = 3600;

function make_link($sid)
{
    return sprintf("/sites/site.php?network=SCAN&station=%s", $sid);
}


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

foreach ($jobj["data"] as $bogus => $iemob) {
    $key = $iemob["station"];
    $mydata[$key] = $iemob;
    $mydata[$key]["ts"] = new DateTime($iemob["local_valid"]);
    $mydata[$key]["sped"] = $mydata[$key]["sknt"] * 1.15078;
    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"]);
        }
    }
}

$finalA = aSortBySecondIndex($mydata, $sortcol);
$i = 0;
$now = new DateTime();

$t1 = "";
// Loop though all data
foreach ($finalA as $key => $val) {
    $parts = $finalA[$key];
    $i++;
    $t1 .= "<tr";
    if ($i % 2 == 0)  $t1 .= " bgcolor='#eeeeee'";

    $t1 .= ">";
    $t1 .= "<td><input type=\"checkbox\" name=\"st[]\"
               value=\"" . $key . "\"></td>";
    $t1 .= "<td><a href=\"" . make_link($key) . "\">" . $cities[$key]["name"] . "</a></td><td>";
    $t1 .= $parts["ts"]->format("M-d-Y h:i A") . "</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["relh"] . "</td>
 <td>" . myround($parts["sknt"], 1);
    if (!is_null($parts["gust"]) && $parts["gust"] > 0) {
        $t1 .= "G" . round($parts["gust"], 1);
    }
    $t1 .= "</td><td>" . myround($parts["drct"], 0) . "</td>";
    $t1 .= "<td>". myround($parts["peak"], 0);
    $t1 .= "</td><td>" . myround($parts["srad"], 0) . "</td></tr>";
}

$t2 = "";
$n = $finalA;
foreach ($n as $key => $parts) {
    $i++;
    $t2 .= "<tr";
    if ($i % 2 == 0)  $t2 .= " bgcolor='#eeeeee'";

    $t2 .= ">";
    $t2 .= "<td><a href=\"" . make_link($key) . "\">" . $cities[$key]["name"] . "</a></td></td><td>";
    $t2 .= $parts["ts"]->format("M-d-Y h:i P") . "</td>
    <td>" . myround($parts["c1smv"], 0) . "</td><td>" . myround($parts["c1tmpf"], 0) . "</td>
    <td>" . myround($parts["c2smv"], 0) . "</td><td>" . myround($parts["c2tmpf"], 0) . "</td>
    <td>" . myround($parts["c3smv"], 0) . "</td><td>" . myround($parts["c3tmpf"], 0) . "</td>
    <td>" . myround($parts["c4smv"], 0) . "</td><td>" . myround($parts["c4tmpf"], 0) . "</td>
    <td>" . myround($parts["c5smv"], 0) . "</td><td>" . myround($parts["c5tmpf"], 0) . "</td>";

    $t2 .= "</tr>";
}

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

$t->current_network = 'SCAN';
$t->content = <<<EOM
<h3>Current SCAN Conditions</h3>

Sorted by: <b>({$vals[$sortcol]})</b>  &nbsp; &nbsp; Click on
a column to sort it. Timestamps are in the local station's timezone.<br>

<form method="GET" action="/my/current.phtml">
<table class="table table-striped">
<thead class="sticky">
<tr>
  <th rowspan="2">ADD:</th>
  <th rowspan="2">Station:</th>
  <th rowspan="2"><a href="current.phtml?sortcol=ts">Ob Time</a></th>
  <th colspan="3">Temps &deg;F</th>
  <th>&nbsp;</th>
  <th colspan="3">Wind [knots]</th>
  <th>&nbsp;</th>
<tr>
  
  <th>
   <a href="current.phtml?sortcol=tmpf">Air</a>
   (<a href="current.phtml?sortcol=max_tmpf">Hi</a> /
   <a href="current.phtml?sortcol=min_tmpf">Lo</a>)
</th>
  <th><a href="current.phtml?sortcol=dwpf">DewP</a></th>
  <th><a href="current.phtml?sortcol=feel">Feels</a></th>
  <th><a href="current.phtml?sortcol=relh">RH%</a></th>
  <th><a href="current.phtml?sortcol=sknt">Speed</a></th>
  <th><a href="current.phtml?sortcol=drct">Direction</a></th>
  <th><a href="current.phtml?sortcol=max_sknt">Gust</a>
    @<a href="current.phtml?sortcol=gtim">Time</a></th>
  <th><a href="current.phtml?sortcol=srad">Solar Rad</a></th>
</tr></thead>
<tbody>
{$t1}
</tbody>
</table>
<input type="submit" value="Add to Favorites">
<input type="reset" value="Reset">

</form>

<br><br>

<h3>SCAN Soil Conditions</h3>

<table class="table table-striped">
<thead class="sticky">
 <tr>
  <th colspan=2>&nbsp;</th>
  <th colspan=2>2 inch</th>
  <th colspan=2>4 inch</th>
  <th colspan=2>8 inch</th>
  <th colspan=2>20 inch</th>
  <th colspan=2>40 inch</th>
 </tr>
 <tr>
  <th>Station:</th>
  <th>Ob Time:</th>
  <th>Moist:</th><th>Temp:</th>
  <th>Moist:</th><th>Temp:</th>
  <th>Moist:</th><th>Temp:</th>
  <th>Moist:</th><th>Temp:</th>
  <th>Moist:</th><th>Temp:</th>
 </tr></thead>
<tbody>{$t2}</tbody>
<tfoot>
<tr>
<td colspan="12">&nbsp;</td>
</tr>
</tfoot>
</table>
EOM;
$t->render("sortables.phtml");
