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

$wfo = get_str404("wfo", 'OAX');
$radar = get_str404("radar", 'OAX');
$radar_product = get_str404("product", 'N0Q');
$year = get_int404("year", date("Y"));
$month1 = get_int404("month1", date("m"));
$day1 = get_int404("day1", date("d"));
$hour1 = get_int404("hour1", 0);
$month2 = get_int404("month2", date("m"));
$day2 = get_int404("day2", date("d"));
$hour2 = get_int404("hour2", 23);
$sts = gmmktime($hour1, 0, 0, $month1, $day1, $year);
$ets = gmmktime($hour2, 0, 0, $month2, $day2, $year);
$wtype = isset($_GET["wtype"]) ? $_GET["wtype"] : array('TO', 'SV');

// Double check that each wtype is 2 char long
foreach ($wtype as $key => $val) {
    if (strlen($val) != 2) {
        die405();
    }
}

if (isset($_REQUEST["wfo"])) {
    /* Check to make sure we have warnings first! */
    $postgis = iemdb("postgis");
    $stname = iem_pg_prepare($postgis, "SELECT issue from sbw_{$year} " .
        "WHERE wfo = $1 and phenomena in ('SV','TO','FF', 'MA', 'SQ', 'DS') " .
        "and significance = 'W' and issue BETWEEN $2 and $3");
    $rs = pg_execute($postgis, $stname, array(
        $wfo,
        gmdate("Y-m-d H:i", $sts) . "+00", gmdate("Y-m-d H:i", $ets) . "+00"
    ));
    if (pg_num_rows($rs) < 1) {
        $err = "Sorry, did not find any warnings for your selected WFO and
        time period, please try again!";
    } else {
        $mesosite = iemdb("mesosite");
        $stname = iem_pg_prepare($mesosite, "INSERT into racoon_jobs(wfo, sts, ets,
                radar, nexrad_product, wtype) values ($1,$2,$3,$4,$5,$6)
                returning jobid");
        $rs = pg_execute($mesosite, $stname, array(
            $wfo,
            date("Y-m-d H:i", $sts), date("Y-m-d H:i", $ets), $radar,
            $radar_product, implode(",", $wtype)
        ));
        $row = pg_fetch_assoc($rs, 0);
        $jobid = $row["jobid"];
        Header("Location: wait.phtml?jobid=$jobid");
        die();
    }
}

$t->title = "Raccoon - Warnings overview in PowerPoint";
$t->headextra = "<meta name='robots' content='index, nofollow'/>";

$e = "";
if (isset($err)) {
    $e = sprintf("<div class=\"alert alert-warning\">%s</div>", $err);
}

$ar = array(
    "FF" => "Flash Flood",
    "MA" => "Marine",
    "SQ" => "Snow Squall",
    "DS" => "Dust Storm",
    "SV" => "Severe Thunderstorm",
    "TO" => "Tornado"
);
$wtselect = "";
foreach ($ar as $p => $n) {
    $wtselect .= sprintf(
        "<input name=\"wtype[]\" type=\"checkbox\"
            value=\"%s\"%s>%s (%s.W)</input><br />",
        $p,
        in_array($p, $wtype) ? " CHECKED" : "",
        $n,
        $p
    );
}


$wselect = networkSelect("WFO", $wfo, array(), "wfo");
$nselect = networkSelect("NEXRAD", $radar, array(), "radar");
$yselect = yearSelect(2002, $year, "year");
$m1select = monthSelect($month1, "month1");
$d1select = daySelect($day1, "day1");
$h1select = gmtHourSelect($hour1, "hour1");
$m2select = monthSelect($month2, "month2");
$d2select = daySelect($day2, "day2");
$h2select = gmtHourSelect($hour2, "hour2");
$t->content = <<<EOM
{$e}
<h3>IEM Raccoon</h3>

<p>IEM Raccoon generates a Microsoft Powerpoint presentation of a National
Weather Service office's storm based warnings for a time period of your choice.
The powerpoint contains a number of slides for each warning creating an easy
way to look back through an event.</p>

<div class="row">
<div class="col-md-4 col-sm-4">
 <img src='raccoon.jpg' class="img-fluid" />
<br />Yawning Raccoon by <a href="http://www.flickr.com/photos/32541690@N02">Alan Vernon</a>

</div>
<div class="col-md-4 col-sm-4">
<img src="ss1.png" class="img-fluid"/><br />Overview Stats
</div>
<div class="col-md-4 col-sm-4">
<img src="ss2.png" class="img-fluid"/><br />NEXRAD Snapshots in Time
</div>
</div>

<p>Please fill out the form below by selecting the National Weather Service
office you are interested in viewing warnings from and the RADAR source to use
for graphics generated in the Powerpoint.  The RADAR archives are not complete
prior to February 2012, but have been selectively backfilled.  If there is a
case you are really interested in, please
<a href="/info/contacts.php">let us know</a> and we can
probably back fill imagery for the event.

<form method="GET" name="raccoon">
<table>
<tr><th>Select Weather Office</th>
    <td colspan="4">{$wselect}</td></tr>
<tr><th>Select RADAR source</th>
    <td colspan="4">{$nselect}</td></tr>
<tr><th>Select Level 3 Product:</th>
    <td colspan="4">
    <select name="product">
        <option value="N0Q">Base Reflectivity</option>
        <option value="N0U">Base Velocity</option>
    </select>
    </td></tr>
<tr><th>View Warning Types:</th>
    <td colspan="4">
{$wtselect}
    </td></tr>

    <tr>
    <td></td>
    <th>Year</th><th>Month</th><th>Day</th>
    <th>Hour</th>
  </tr>

  <tr>
    <th>Start:</th>
    <td rowspan="2">{$yselect}</td>
    <td>{$m1select}</td>
    <td>{$d1select}</td>
    <td>{$h1select}</td>
  </tr>

  <tr>
    <th>End Time:</th>
    <td>{$m2select}</td>
    <td>{$d2select}</td>
    <td>{$h2select}</td>
  </tr>
</table>
<p><input type="submit" value="Make my powerpoint!" />
</form>
EOM;
$t->render('single.phtml');
