<?php
require("wdllib.php");

?>
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title><?php if (isset($data[STATION])) {echo $data[STATION];} ?></title>
    <link href="https://fonts.googleapis.com/css?family=Karla|Roboto+Mono" rel="stylesheet">
    <link rel="stylesheet" href="style.css" media="screen" title="no title" charset="utf-8">
  </head>
  <body>

    <?php if (isset($_GET['submit'])) {
      //main
        $site = $_REQUEST["site"];
        $rawdatafile = $site . "clientraw.txt";
        $csv = file_get_contents($rawdatafile);
        $data = explode(' ',$csv);
        // replace underscores with spaces.
        $data[SUMMARY] = str_replace("_", " ", $data[SUMMARY]);
        $data[WINDDIRECTION] = degree_to_compass_point($data[WINDDIRECTION]);
      ?>

      <h1><?php echo $data[STATION] ?></h1>
      <br>
      <a href="../../dsa.php">Home.</a>
      <br>
      <a href="<?php echo $data[STATION] ?>">Weather Station Website.</a>
      <p><b>Weather at <?php echo $data[TIMEHH] ?>:<?php echo $data[TIMEMM] ?>.</b></p>
      <a href="http://www.wws-map.com" target="_blank">Get a weather station.</a>

      <table>
        <tr>
          <th>Summary.</th>
          <th colspan="2">Wind Speed.</th>
          <th colspan="2">Wind Direction.</th>
          <th colspan="2">Temperature.</th>
          <th colspan="2">Pressure.</th>
        </tr>
        <tr>
          <td colspan="2"><?php echo $data[SUMMARY] ?></td>
          <td colspan="2"><?php echo $data[WINDSPEED] ?> knots</td>
          <td colspan="2"><?php echo $data[WINDDIRECTION] ?> &#0176;</td>
          <td colspan="2"><?php echo $data[TEMPERATURE] ?> &#0176;C</td>
          <td colspan="2"><?php echo $data[BAROMETER] ?></td>
        </tr>
      </table>

      <h1>Please enter a weather station URL:</h1>
      <form class="" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">
        <input type="text" name="site" value="<?php if (isset($_GET["site"])) { echo $_GET["site"]; }?>" placeholder="Enter weather station url.">
        <input class="button" type="submit" name="submit" value="Get weather data.">
      </form>

    <?php } else { ?>
      <h1>Weather RSS</h1>
      <p>Get weather information by submiting a weather station url.</p>
      <p><i>Script will look for clientraw.txt by it self.</i></p>
      <p>Try <a href="http://www.stephenc.co.za/weather/" target="_blank">http://www.stephenc.co.za/weather/</a></p>
      <a href="http://www.wws-map.com" target="_blank">Get a weather station.</a>

      <h1>Please enter weather station URL:</h1>
      <form class="" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">
        <input type="text" name="site" value="http://www.alvestonweather.co.uk/" placeholder="Enter weather station url.">
        <input class="button" type="submit" name="submit" value="Get weather data.">
      </form>

    <?php } ?>
  </body>
</html>
