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

$t = new MyView();
$t->current_network = "Other";
$sortcol = isset($_GET["sortcol"]) ? xssafe($_GET["sortcol"]) : "station";
$network = isset($_GET["network"]) ? xssafe($_GET["network"]) : 'OT';

$mesosite = iemdb('mesosite');
$stname = iem_pg_prepare($mesosite, "SELECT tzname from networks where id = $1");
$rs = pg_execute($mesosite, $stname, array($network));
if (pg_num_rows($rs) < 1) {
    $tzname = "America/Chicago";
} else {
    $row = pg_fetch_assoc($rs, 0);
    $tzname = $row["tzname"];
}

$t->title = "Other Currents";
$t->refresh = 600;

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

$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",
    "drct" => "Wind Direction",
    "max_tmpf" => "Today's High Temperature",
    "gtim" => "Wind Gust Time",
    "station" => "Station ID",
    "relh" => "Relative Humidity"
);

$t->breadcrumbs = <<<EOM
<ol class="breadcrumb">
    <li><a href="/other/">Other Networks</a></li>
    <li class="active">Current Conditions</li>
</ol>
EOM;

$mydata = array();

foreach ($jobj["data"] as $bogus => $iemob) {
    $key = $iemob["station"];
    $mydata[$key] = $iemob;
    $mydata[$key]["ts"] = strtotime($iemob["local_valid"]);
    $mydata[$key]["sped"] = $mydata[$key]["sknt"] * 1.15078;
    $mydata[$key]["relh"] = relh(
        f2c($mydata[$key]["tmpf"]),
        f2c($mydata[$key]["dwpf"])
    );

    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"];
        $val = is_null($mydata[$key]["local_max_sknt_ts"]) ? null : strtotime($mydata[$key]["local_max_sknt_ts"]);
        $mydata[$key]["peak_ts"] = $val;
    }
}

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

$table = "";
// Loop though all data
foreach ($finalA as $key => $parts) {
    $i++;
    $tdiff = $now - $parts["ts"];
    // Don't show stations over 5 days old
    if ($tdiff > 86400 * 5) {
        continue;
    }
    $table .= "<tr";
    if ($i % 2 == 0)  $table .= " bgcolor='#eeeeee'";

    $table .= ">";
    $table .= "<td><input type=\"checkbox\" name=\"st[]\"
               value=\"" . $key . "\"></td>";
    $table .= sprintf(
        "<td><a href=\"/sites/site.php?station=%s&amp;network=%s\">%s</a></td>",
        $key,
        $network,
        $parts["name"]
    );
    $table .= "<td ";
    $format = "h:i A";
    if ($tdiff > 11900) {
        $table .= 'bgcolor="red"';
        $format = "M-d-Y h:i A";
    } else if ($tdiff > 7200) {
        $table .= 'bgcolor="orange"';
    } else if ($tdiff > 3600) {
        $table .= 'bgcolor="green"';
    }
    $table .= ">" . date($format, $parts["ts"]) . "</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"], 1) . "</td>
 <td>" . $parts["relh"] . "</td>
 <td>" . myround($parts["sknt"], 0);
    if (! is_null($parts["gust"]) && $parts["gust"] > 0) {
        $table .= "G" . myround($parts["gust"], 1);
    }
    $table .= "</td>
            <td>" . myround($parts["drct"], 0) . "</td>
        <td>" . myround($parts["peak"], 0) . "@ " . date("h:i A", $parts["peak_ts"]) . "</td>
        </tr>\n";
}

$networks = array(
    "OT" => "IEM Misc Stations",
    "VTWAC" => "Vermont Weather Analytics Center"
);
$buttons = "";
foreach ($networks as $key => $val) {
    $buttons .= sprintf(
        "<a class=\"btn btn-sm%s\" href=\"current.phtml?network=%s\">%s</a>",
        ($key == $network) ? ' active' : '',
        $key,
        $val
    );
}

$t->content = <<<EOM

<p><b>Other Networks:</b> {$buttons}

<br /><strong>Times shown are for timezone: {$tzname}.</strong> The local day summary
is based on that timezone.
<br />Sorted by: <b>({$vals[$sortcol]})</b>  &nbsp; &nbsp; Click on
a column to sort it.<br>

<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">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>
</tr>

<tr>
  <th>
   <a href="current.phtml?network={$network}&amp;sortcol=tmpf">Air</a>
   (<a href="current.phtml?network={$network}&amp;sortcol=max_tmpf">Hi</a> /
   <a href="current.phtml?network={$network}&amp;sortcol=min_tmpf">Lo</a>)
</th>
  <th><a href="current.phtml?network={$network}&amp;sortcol=dwpf">DewP</a></th>
  <th><a href="current.phtml?network={$network}&amp;sortcol=feel">Feels</a></th>
  <th><a href="current.phtml?network={$network}&amp;sortcol=relh">RH%</a></th>
  <th><a href="current.phtml?network={$network}&amp;sortcol=sknt">Speed</a></th>
  <th><a href="current.phtml?network={$network}&amp;sortcol=drct">Direction</a></th>
  <th><a href="current.phtml?network={$network}&amp;sortcol=max_sknt">Gust</a>
    @<a href="current.phtml?network={$network}&amp;sortcol=gtim">Time</a></th>
</tr>
</thead>
<tbody>
{$table}
</tbody>
</table>
<input type="submit" value="Add to Favorites">
<input type="reset" value="Reset">

</form>

<br><br>
EOM;
$t->render("sortables.phtml");
