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

$t = new MyView();
$conn = iemdb("postgis");
$sel = get_int404("sel", -1);
$t->refresh = 300;
$t->title = "SPC Watches Overview";

$metadata = array();
$rs = pg_query(
    $conn,
    <<<EOM
with data as (
    select sel, rank() OVER (PARTITION by sel ORDER by issued DESC),
    issued, expired, num
    from watches where issued > now() - '120 days'::interval)
select * from data where rank = 1
EOM
    );
while ($row = pg_fetch_assoc($rs)) {
    $metadata[$row["sel"]] = $row;
    $metadata[$row["sel"]]["sts"] = strtotime(substr($row["issued"], 0, 16));
    $metadata[$row["sel"]]["ets"] = strtotime(substr($row["expired"], 0, 16));
}

if ($sel > -1) {
    $row = $metadata["SEL{$sel}"];

    $t->content = <<<EOM
<a href="sel.phtml">View All Watches</a>

<p><img src="/data/sel{$sel}comp.png" />

EOM;
} else {
    $content = "";
    for ($i = 0; $i < 10; $i++) {
        $row = $metadata["SEL{$i}"];
        $content .= "<div class=\"col-md-4 bg-light border rounded p-3 mb-3\">
        <strong>SEL{$i}:</strong> Issued: " . date("j M Y g:i A", $row["sts"])
            . "<br />Expired: " . date("j M Y g:i A", $row["ets"]) . ",
                        <a href=\"/GIS/apps/rview/watch.phtml?year=" . date("Y", $row["sts"]) . "&num=" . $row["num"] . "\">more info</a>
                        <br /><a href=\"sel.phtml?sel={$i}\">
        <img src=\"/data/sel{$i}comp.png\" class=\"img-fluid\"></a>
        </div>";
    }
    $t->content = <<<EOM

<p>Click on an image to get an enlarged view as well as the text for the watch.
These images are for the last 10 areas in which watches have been valid for.
If the watch is currently valid, it will appear on the plot.  The timestamps
presented are in US Central time.

$content

EOM;
}
$t->render('full.phtml');
