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

$t = new MyView();
$count = get_int404("count", 10);

/**
 * Do the necessary work.
 * @param int $count
 * @param string $sort
 */
function dowork($count, $sort)
{
    $wsargs = Array(
        "count" => $count,
        "sort" => $sort,
    );
    $baseurl = "/json/mcd_bysize.py";
    $public = sprintf("%s?%s", $baseurl, http_build_query($wsargs));
    $s = <<<EOM

    <p>JSON Webservice for the data below:<br />
    <code>{$public}</code></p>
    <table class="table table-sm table-striped">
    <tr><th>Year</th><th>Number</th><th>Issuance UTC</th><th>Size (sq km)</th></tr>

EOM;
    $json = require_json_response($baseurl, $wsargs);
    foreach ($json["mcds"] as $key => $val) {
        $s .= sprintf(
            "<tr><td>%s</td><td><a href=\"%s\">%s</a></td><td>%s</td><td>%.2f</td></tr>",
            $val["year"],
            $val["spcurl"],
            $val["product_num"],
            $val["utc_issue"],
            $val["area_sqkm"]
        );
    }
    $s .= "</table>\n";
    return $s;
}

$ltable = dowork($count, "DESC");
$stable = dowork($count, "ASC");

$t->content = <<<EOM
<ol class="breadcrumb">
 <li><a href="/current/severe.phtml">Severe Weather Mainpage</a></li>
 <li class="active">Top {$count} SPC Mesoscale Convective Discussions by size</li>
</ol>

<h3>Top {$count} SPC Mesoscale Convective Discussions by Size</h3>

<p>The IEM maintains a database of <a href="https://www.spc.noaa.gov">NWS Storm Prediction Center</a>
 Mesoscale Convective Discussions (MCD)s.  These products contain a polygon for the
 alerted region for the product. This page provides an unofficial listing
 of the top {$count} largest and smallest polygons associated with the MCDs.</p>

 <p><strong>Pro Tip:</strong> If you want to use this data, try copying the table
 contents and pasting into a spreadsheet program like Excel.</p>

<p><strong>Related</strong>:
<a href="spc_top10.phtml" class="btn btn-primary">Top 10 Outlooks by Size</a>
</p>

 <p><form method="GET" name="u">
 Number of Events to Show:
 <input size="10" type="text" name="count" value="{$count}">
 <input type="submit" value="Update Table">
 </form></p>

<h3>Top {$count} Largest:</h3>
$ltable

<h3>Top {$count} Smallest:</h3>
$stable

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