<?php
require_once "../../config/settings.inc.php";
require_once "../../include/myview.php";
require_once "../../include/cameras.inc.php";
require_once "../../include/forms.php";
$camid = isset($_GET["camid"]) ? xssafe($_GET["camid"]) : "IDOT-000";

$cdict = array();
$selectform = "<select name=\"camid\">\n";
foreach ($cameras as $id => $cam) {
    if ($cam["network"] != "IDOT") {
        continue;
    }
    $base = substr($id, 0, 8);
    if (!array_key_exists($base, $cdict)) {
        $cdict[$base] = 0;
    }
    $cdict[$base] += 1;

    if (substr($id, 9, 2) == "00") {
        $selectform .= "<option value=\"$base\"";
        if ($base == $camid) {
            $selectform .= " selected=\"selected\" ";
        }
        $selectform .= ">" . $cam["name"] . "</option>";
    }
}
$selectform .= "</select>\n";

$t = new MyView();
$t->title = "Roadway Weather Information System (RWIS) Data";

$camimgs = "";
for ($i = 0; $i < $cdict[$camid]; $i++) {
    $camimgs .= sprintf(
        "<div class=\"col-md-3\"><img src=\"/data/camera/stills/%s-%02d.jpg?%s\" class=\"img img-fluid\"></div>",
        $camid,
        $i,
        time()
    );
}


$t->content = <<<EOM
<div class="container-fluid px-0">
  <div class="row mb-4">
    <div class="col-lg-9 col-md-8">
      <h1 class="display-5 mt-2 mb-3">Roadway Weather Information System</h1>
      <p class="lead">
        The IEM curates an archive of RWIS information. The Iowa data has been provided by the Iowa DOT through a long standing partnership since the founding of the IEM. Data for other states is provided via the <a href="https://madis.ncep.noaa.gov/">NOAA MADIS</a> system.
      </p>
    </div>
    <div class="col-lg-3 col-md-4 d-flex align-items-center justify-content-lg-end justify-content-md-end justify-content-start mt-3 mt-md-0">
      <a href="/sites/locate.php?network=IA_RWIS" class="btn btn-outline-primary btn-lg">Iowa RWIS Locations</a>
    </div>
  </div>

  <div class="card mb-4">
    <div class="card-header d-flex flex-wrap align-items-center justify-content-between">
      <h2 class="h4 mb-0">Roadside Webcams</h2>
      <div class="btn-group mt-2 mt-md-0">
        <a href="camera.phtml" class="btn btn-primary">RWIS Archive Viewer</a>
        <a href="/current/viewer.phtml" class="btn btn-primary">IEM Webcam Viewer</a>
      </div>
    </div>
    <div class="card-body">
      <form method="" action="" name="widget" class="row g-3 align-items-center mb-3">
        <div class="col-auto">
          <label for="camid" class="col-form-label fw-bold">Available sites:</label>
        </div>
        <div class="col-auto">
          {$selectform}
        </div>
        <div class="col-auto">
          <input type="submit" value="View Webcam" class="btn btn-primary">
        </div>
      </form>
      <div class="row g-3">
        {$camimgs}
      </div>
    </div>
  </div>

  <div class="row g-4">
    <div class="col-lg-4 col-md-6">
      <div class="card h-100">
        <div class="card-body">
          <h3 class="h5">Current Conditions</h3>
          <ul class="list-group list-group-flush mb-3">
            <li class="list-group-item"><a href="current.phtml">Atmospherics</a></li>
            <li class="list-group-item"><a href="currentSF.phtml">Pavement</a></li>
            <li class="list-group-item"><a href="soil.phtml">Soil Probes</a></li>
            <li class="list-group-item"><a href="traffic.phtml">Traffic Data</a></li>
            <li class="list-group-item">
              <a href="/GIS/apps/iem/freeze.phtml">IEM Freeze</a>
              <div class="small text-muted">Combine air and pavement temperatures with current RADAR.</div>
            </li>
          </ul>
          <h3 class="h6">Time Series Plots</h3>
          <ul class="list-group list-group-flush mb-3">
            <li class="list-group-item"><a href="/plotting/rwis/sf_fe.php">Recent Air/Pavement Temps</a></li>
          </ul>
          <h3 class="h6">Plots with RWIS data</h3>
          <ul class="list-group list-group-flush">
            <li class="list-group-item"><a href="/data/mesonet.gif">Combined Mesonet</a></li>
            <li class="list-group-item"><a href="/data/iowa_rwis.png">RWIS Mesonet</a></li>
            <li class="list-group-item"><a href="/data/rwis_sf.png">Pavement Temperatures</a></li>
            <li class="list-group-item"><a href="/data/heat.gif">Heat Index</a></li>
            <li class="list-group-item"><a href="/data/wcht.gif">Wind Chill Index</a></li>
          </ul>
        </div>
      </div>
    </div>
    <div class="col-lg-4 col-md-6">
      <div class="card h-100">
        <div class="card-body">
          <h3 class="h5">Station Plot with NEXRAD</h3>
          <ul class="list-group list-group-flush">
            <li class="list-group-item"><a href="/data/DMX_radar.gif">Des Moines</a></li>
            <li class="list-group-item"><a href="/data/OAX_radar.gif">Omaha</a></li>
            <li class="list-group-item"><a href="/data/DVN_radar.gif">Davenport</a></li>
            <li class="list-group-item"><a href="/data/FSD_radar.gif">Sioux Falls</a></li>
            <li class="list-group-item"><a href="/data/ARX_radar.gif">LaCrosse</a></li>
          </ul>
        </div>
      </div>
    </div>
    <div class="col-lg-4 col-md-12">
      <div class="card h-100">
        <div class="card-body">
          <h3 class="h5">Data Download</h3>
          <ul class="list-group list-group-flush mb-3">
            <li class="list-group-item"><a href="/request/rwis/fe.phtml">Atmosphere &amp; Pavement Data</a></li>
            <li class="list-group-item"><a href="/request/rwis/soil.phtml">Soil Probe Data</a></li>
            <li class="list-group-item"><a href="/request/rwis/traffic.phtml">Traffic Data</a></li>
          </ul>
          <h3 class="h6">Comparisons</h3>
          <ul class="list-group list-group-flush mb-3">
            <li class="list-group-item"><a href="/data/temps.gif">Temperatures vs ASOS/AWOS</a></li>
            <li class="list-group-item"><a href="/data/dewps.gif">Dew Points vs ASOS/AWOS</a></li>
          </ul>
          <h3 class="h6">QC Info</h3>
          <ul class="list-group list-group-flush">
            <li class="list-group-item">Sites <a href="/QC/offline.php">offline</a></li>
          </ul>
        </div>
      </div>
    </div>
  </div>
</div>
EOM;

$t->render("single.phtml");
