<?php
require_once "../../config/settings.inc.php";
require_once "../../include/myview.php";
require_once "../../include/forms.php";
$t = new MyView();
/* Script to generate a loop of whatever */
$prod = get_str404('prod', 'mesonet');
$java = get_str404('java', 'none');
$frames = get_int404('frames', 10);

$products = array(
    "mesonet" => array(
        "aformat" => 'mesonet_%H00.gif',
        "tint" => 3600, "width" => 900, "height" => 700, "minute" => 8,
        "name" => "Iowa Mesonet Plot"
    ),
    "mwmesonet" => array(
        "aformat" => 'MWmesonet_%H00.gif',
        "tint" => 3600, "width" => 900, "height" => 700, "minute" => 8,
        "name" => "Midwest Mesonet Plot"
    ),
);

$baseurl2 = '/archive/data/';
$files = "";
$hrefs = "";
$loopendts = time();
$m = date("i");
if ($m < $products[$prod]["minute"])  $loopendts = time() - 3600;

$q = $frames - 1;
for ($i = $q; $i >= 0; $i--) {
    $ts = $loopendts - ($products[$prod]["tint"] * $i);
    $gts = strtotime(gmdate("Y-m-d H:i", $ts));
    $aa = str_replace("%H", date("H", $gts), $products[$prod]["aformat"]);
    $u = $baseurl2 . date("Y/m/d/", $gts) . $aa;
    $hrefs .= "<br><img src=\"$u\">";
    $files .= $u . ",";
}

$t->title = "Product Looper";
$ar = array(
    "none" => "List Images"
);
$loopselect = make_select("java", $java, $ar);

$prodselect = "<select name=\"prod\">";
foreach ($products as $k => $v) {
    $prodselect .= "<option value=\"$k\" ";
    if ($prod == $k) $prodselect .= " SELECTED ";
    $prodselect .= ">" . $products[$k]["name"] . "\n";
}
$prodselect .= "</select>";

$control =  $hrefs;

$t->content = <<<EOM
<h3>IEM Product Looper</h3>

<div class="bg-light border p-3">

<form method="GET" action="loop.phtml">
<div class="row mb-3">
<div class="col-md-3">
 <label class="form-label">Loop Mode:</label>
 {$loopselect}
</div>
<div class="col-md-3">
 <label class="form-label">Select Product:</label>
 {$prodselect}
</div>
<div class="col-md-3">
 <label class="form-label">Frames:</label>
 <input type="text" name="frames" value="{$frames}" size="3" class="form-control">
</div>
<div class="col-md-3">
 <input type="submit" value="Generate Loop" class="btn btn-primary mt-4">
</div>
</div>
</form>

{$control}

</div>
EOM;
$t->render('single.phtml');
