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

$year1 = get_int404("year1", date("Y"));
$year2 = get_int404("year2", date("Y"));
$month1 = get_int404("month1", date("m"));
$month2 = get_int404("month2", date("m"));
$day1 = get_int404("day1", date("d"));
$day2 = get_int404("day2", date("d"));
$model = strtoupper(get_str404("model", "GFS"));
$station = strtoupper(get_str404("station", "KAMW"));

$t->title = "NWS MOS Download Interface";

$ys1 = yearSelect(2000,$year1, "year1");
$ms1 = monthSelect($month1, "month1");
$ds1 = daySelect($day1, "day1");
$ys2 = yearSelect(2000,$year1, "year2");
$ms2 = monthSelect($month1, "month2");
$ds2 = daySelect($day1, "day2");

$table = "";
/** @disregard false positive for mosvars being undefined */
foreach($mosvars as $k => $v){
    $table .= sprintf("<tr><th>%s</th><td>%s</td></tr>\n", $k, $v);
}
$ar = Array(
    "GFS" => "AVN/GFS",
    "MEX" => "GFS-X (MEX)",
    "NAM" => "Eta/NAM",
    "NBE" => "NBE",
    "NBS" => "NBS",
);
$modselect = make_select("model", $model, $ar);

$t->content = <<<EOM
<h3>NWS MOS Download Interface</h3>

<p>This page allows you to download from the IEM's archive of NWS MOS
data. The archive goes back to June 2000. You can set the start and end times
to the same time to get that particular run.</p>

<p>For dates after 25 Feb 2020, the NBS was only archived for the 1, 7, 13,
and 19 UTC cycles as per guidance from the MOS developers. For dates before
25 Feb 2020, the NBS was only archived for 0, 7, 12, and 19 UTC runs.</p>

<p><a href="/cgi-bin/request/mos.py?help" class="btn btn-secondary">
<i class="bi bi-file-earmark-text"></i> Backend documentation</a> exists for those wishing
to script against this service.</p>

<div class="card mb-4">
    <div class="card-header">
        <h4 class="card-title mb-0">Data Request Form</h4>
    </div>
    <div class="card-body">
        <form name="selector" action="/cgi-bin/request/mos.py" method="GET">
            <div class="row mb-3">
                <div class="col-md-6">
                    <label for="station" class="form-label">Enter 4-Char Station ID:</label>
                    <input type="text" id="station" name="station" value="{$station}" class="form-control" maxlength="4" placeholder="e.g., KAMW">
                </div>
                <div class="col-md-6">
                    <label for="model" class="form-label">Select Model:</label>
                    {$modselect}
                </div>
            </div>

            <div class="row mb-3">
                <div class="col-12">
                    <h5>Start Date</h5>
                </div>
                <div class="col-md-3">
                    <label class="form-label">Year:</label>
                    {$ys1}
                </div>
                <div class="col-md-3">
                    <label class="form-label">Month:</label>
                    {$ms1}
                </div>
                <div class="col-md-3">
                    <label class="form-label">Day:</label>
                    {$ds1}
                </div>
                <div class="col-md-3">
                    <label for="hour1" class="form-label">Hour (UTC):</label>
                    <select name="hour1" id="hour1" class="form-select">
                        <option value="0">00 UTC</option>
                        <option value="1">01 UTC</option>
                        <option value="6">06 UTC</option>
                        <option value="7">07 UTC</option>
                        <option value="12">12 UTC</option>
                        <option value="13">13 UTC</option>
                        <option value="18">18 UTC</option>
                        <option value="19">19 UTC</option>
                    </select>
                </div>
            </div>

            <div class="row mb-3">
                <div class="col-12">
                    <h5>End Date (inclusive)</h5>
                </div>
                <div class="col-md-3">
                    <label class="form-label">Year:</label>
                    {$ys2}
                </div>
                <div class="col-md-3">
                    <label class="form-label">Month:</label>
                    {$ms2}
                </div>
                <div class="col-md-3">
                    <label class="form-label">Day:</label>
                    {$ds2}
                </div>
                <div class="col-md-3">
                    <label for="hour2" class="form-label">Hour (UTC):</label>
                    <select name="hour2" id="hour2" class="form-select">
                        <option value="0">00 UTC</option>
                        <option value="1">01 UTC</option>
                        <option value="6">06 UTC</option>
                        <option value="7">07 UTC</option>
                        <option value="12">12 UTC</option>
                        <option value="13">13 UTC</option>
                        <option value="18">18 UTC</option>
                        <option value="19">19 UTC</option>
                    </select>
                </div>
            </div>

            <div class="row">
                <div class="col-12">
                    <button type="submit" class="btn btn-primary btn-lg">
                        <i class="bi bi-download"></i> Generate Data
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>

<div class="card">
    <div class="card-header">
        <h4 class="card-title mb-0">Description of MOS Variables</h4>
    </div>
    <div class="card-body">
        <div class="table-responsive">
            <table class="table table-striped table-hover">
                <thead class="table-dark">
                    <tr>
                        <th>Variable</th>
                        <th>Description</th>
                    </tr>
                </thead>
                <tbody>
                    {$table}
                </tbody>
            </table>
        </div>
    </div>
</div>
EOM;
$t->render('single.phtml');
