<?php
require_once "../../config/settings.inc.php";
define("IEM_APPID", 26);
require_once "../../include/myview.php";
$t = new MyView();
require_once "../../include/forms.php";
require_once "../../include/database.inc.php";

$year = get_int404("year", date("Y", time() - 86400));
$month = get_int404("month", date("m", time() - 86400));
$day = get_int404("day", date("d", time() - 86400));
$pvar1 = isset($_GET["pvar1"]) ? xssafe($_GET["pvar1"]) : "fc_wpl";
$pvar2 = isset($_GET["pvar2"]) ? xssafe($_GET["pvar2"]) : "le_wpl";
$pvar3 = isset($_GET["pvar3"]) ? xssafe($_GET["pvar3"]) : "hs";
$pvar4 = isset($_GET["pvar4"]) ? xssafe($_GET["pvar4"]) : "rn_total_avg";

$other = iemdb("other");
$sql = "SELECT * from flux_vars ORDER by details ASC";
$rows = pg_query($other, $sql);
$var_names = array();
$var_details = array();
while ($row = pg_fetch_assoc($rows)) {
    $var_names[] = $row["name"];
    $var_details[] = $row["details"];
}

function genselect($name, $selected, $ids, $values)
{
    $s = "<select name=\"$name\">\n";
    foreach ($ids as $k => $v) {
        $s .= "<option value=\"$v\" ";
        if ($selected == $v) $s .= "selected=\"selected\"";
        $s .= ">" . $values[$k] . "</option>";
    }
    $s .= "</select>\n";
    return $s;
}
$t->title = "NLAE Flux Sites";

$ys = yearSelect(2002, $year, "year");
$ms = monthSelect($month, "month");
$ds = daySelect($day, "day");
$spvar1 = genselect("pvar1", $pvar1, $var_names, $var_details);
$spvar2 = genselect("pvar2", $pvar2, $var_names, $var_details);
$spvar3 = genselect("pvar3", $pvar3, $var_names, $var_details);
$spvar4 = genselect("pvar4", $pvar4, $var_names, $var_details);

$u1 = sprintf("plot.php?pvar=%s&year=%s&month=%s&day=%s", $pvar1, $year, $month, $day);
$u2 = sprintf("plot.php?pvar=%s&year=%s&month=%s&day=%s", $pvar2, $year, $month, $day);
$u3 = sprintf("plot.php?pvar=%s&year=%s&month=%s&day=%s", $pvar3, $year, $month, $day);
$u4 = sprintf("plot.php?pvar=%s&year=%s&month=%s&day=%s", $pvar4, $year, $month, $day);

$t->content = <<<EOM
<nav aria-label="breadcrumb">
 <ol class="breadcrumb">
 <li class="breadcrumb-item"><a href="/nstl_flux/">NLAE Flux Stations</a></li>
 <li class="breadcrumb-item active" aria-current="page">Time Series Plots</li>
 </ol>
</nav>

<p>This page generates 4 plots of your choice.  Select the date and variables
you wish to plot and then click on the 'Generate Plots' button.


<form method="GET" name="selector">

<table class="table table-bordered">
<thead>
<tr>
 <th colspan="3">Select Date:</th>
 <th>Select Plot Variable:</th></tr></thead>
<tbody>
<tr><th>Year:</th><th>Month:</th><th>Day:</th>
<td rowspan="2"><b>1.</b> {$spvar1}
 <br /><b>2.</b> {$spvar2}
 <br /><b>3.</b> {$spvar3}
 <br /><b>4.</b> {$spvar4}
</td></tr>
<tr>
 <td>{$ys}</td>
 <td>{$ms}</td>
 <td>{$ds}</td>

</tr>
</table>
<input type="submit" value="Generate Plots">
<p>
<br /><img src="{$u1}" class="img-fluid">
<br /><img src="{$u2}" class="img-fluid">
<br /><img src="{$u3}" class="img-fluid">
<br /><img src="{$u4}" class="img-fluid">
EOM;
$t->render('single.phtml');
