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

$jobid = isset($_REQUEST["jobid"]) ? xssafe($_REQUEST["jobid"]) : null;
if (is_null($jobid)) {
    http_response_code(422);
    die("jobid required");
}
$status = "PROCESSING";
if ($jobid) {
    $mesosite = iemdb("mesosite");
    $stname = iem_pg_prepare($mesosite, "SELECT * from racoon_jobs where jobid = $1");
    $rs = pg_execute($mesosite, $stname, array($jobid));
    if (pg_num_rows($rs) < 1) die();
    $row = pg_fetch_assoc($rs, 0);
    $sts = strtotime($row["sts"]);
    $ets = strtotime($row["ets"]);
    $basefn = sprintf(
        "%s-%s-%s-%s-%s.ppt",
        $row["wfo"],
        str_replace(",", "_", $row["wtype"]),
        $row["radar"],
        gmdate("YmdH", $sts),
        gmdate("YmdH", $ets)
    );
    $fn = sprintf("/mesonet/share/pickup/raccoon/%s",  $basefn);
    if (is_file($fn)) {
        $status = "DONE!";
    }
}

$t->title = "Raccoon - Warnings overview in PowerPoint";
if ($status != "DONE!") {
    $t->headextra = "<meta http-equiv=\"refresh\" content=\"60\">";
}

$c = "";
if ($status == "DONE!") {
    $c = sprintf(
        '<p>Download your powerpoint <a href="/pickup/raccoon/%s">here</a>',
        $basefn
    );
}
$t->content = <<<EOM
<h3>IEM Raccoon</h3>

<p>This page will refresh once per minute with the status of your request. It 
typically takes 1-3 minutes for the powerpoint to be generated!

<p>Current status is: {$status}
{$c}

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