<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Guestbook</title>
    <link href="../style.css" rel="stylesheet" type="text/css" media="all">
    <style type="text/css">
      table {
        border-collapse: collapse;
        table-layout: fixed;
      }
      td {
        padding: 0.3rem;
      }

      form > table {
        width: 100%;
        margin: auto;
        margin-bottom: 1.5rem;
      }
      form td:first-child {
        text-align: right;
      }

      article {
        width: 90%;
        margin: 2rem auto;
      }
      article > table {
        width: 100%;
      }
      article td {
        padding: 0.5rem 0.3rem;
      }
      article td:nth-child(2) {
        text-align: center;
      }
      article td:last-child {
        text-align: right;
      }
      article td:first-child {
        text-align: left;
      }

      input, textarea {
        border: none;
        font-family: var(--stdfont);
      }
      input[type="text"], input[type="email"], textarea {
        width: 100%;
        background: none;
        border-bottom: 2px solid #444444;
        color: gainsboro;
        padding: 0.35rem;
      }
      input[type="submit"], input[type="reset"] {
        padding: 0.35rem 0.5rem;
        box-shadow: 2px 2px 6px black;
        margin: 0.3rem;
      }
      input[type="submit"]:active, input[type="reset"]:active {
        transform: translate(1px, 1px); box-shadow: 0 0 2px black;
      }
      input[type="submit"]:hover, input[type="reset"]:hover {
        cursor: pointer;
      }

      hr {
        width: 30%;
      }
      .error {
        color: red;
        font-size: 0.85em;
      }
      .success {
        color: #00ff00;
        font-size: 0.85em;
      }
      .reply {
        border-left: 3px solid gainsboro;
        padding-left: 1rem;
      }
      .message {
        padding-top: 1.5rem;
      }
    </style>
    <?php include($_SERVER['DOCUMENT_ROOT'] . '/seasonal.php'); ?>
  </head>
  <body>
    <?php include($_SERVER['DOCUMENT_ROOT'] . '/nav.php'); ?>
    <h1>guestbook</h1>
    <h2 id="caption">say hi!</h2>
    <nav>
      <a href="feed.php">RSS</a>
    </nav>
    <?php
      $return = $_GET["return"];
    ?>
    <form action="submit.php" method="post" autocomplete="off">
      <table>

        <?php if($return == "noname") { echo "<td></td><td><span class='error'>Name required!</span></td>"; } ?>
        <tr>
          <td>Name<span style="color:red;">*</span></td>
          <td><input type="text" name="name"></td>
        </tr>

        <?php if($return == "invalidwebsite") { echo "<td></td><td><span class='error'>Invalid URL!</span></td>"; } ?>
        <tr>
          <td>Website</td>
          <td><input type="text" name="website" placeholder="https://"></td>
        </tr>

        <?php if($return == "invalidlink") { echo "<td></td><td><span class='error'>Invalid URL!</span></td>"; } ?>
        <tr>
          <td>Link something cool</td>
          <td><input type="text" name="cool" placeholder="https://"></td>
        </tr>

        <tr>
          <td>How did you find me?</td>
          <td><textarea name="found"></textarea></td>
        </tr>

        <?php if($return == "nomessage") { echo "<td></td><td><span class='error'>Message required!</span></td>"; } ?>
        <tr>
          <td>Message<span style="color:red;">*</span></td>
          <td><textarea name="message"></textarea></td>
        </tr>
        
        <?php if($return == "botcheckfailed") { echo "<td></td><td><span class='error'>You failed the bot check!</span></td>"; } ?>
        <tr>
          <td>Bot test (type <u>isopod</u>)<span style="color:red;">*</span></td>
          <td><input type="text" name="botcheck"></td>
        </tr>

        <?php if($return == "success") { echo "<td></td><td><span class='success'>Successfully posted!</span></td>"; } ?>
        <tr>
          <td></td>
          <td><input type="reset"><input type="submit" value="Post"></td>
        </tr>

      </table>
    </form>
    <?php 
      $totaloutput = "";
      $postlist = json_decode(file_get_contents("posts.json"));
      $index = 1;
      foreach($postlist as $post) {
        $output = "";
        $date = date("Y-m-d h:iA", $post->date);
        $name = $post->name;
        $message = $post->message;
        $found = $post->found;
        $website = $post->website;
        $cool = $post->cool;
        $coolsanitized = htmlentities(preg_replace("/https?:\/\//i", "", $cool));
        $reply = htmlentities($post->reply);

        $output = $output."<hr/><article><table><tr><td>";
        if($website) { $output = $output."<a href='$website'>$name</a>"; }
        else { $output = $output.$name; }
        $output = $output."</td><td><time>$date</time></td><td>Entry #$index</td></tr>";
        if($cool) {
          $output = $output."<tr><td colspan='3'><b>cool thing</b>: <a href='$cool'>$coolsanitized</a></td></tr>";
        }
        if($found) {
          $output = $output."<tr><td colspan='3'><b>How did you find me?</b><br/>$found</td></tr>";
        }
        $output = $output."<tr><td colspan='3' class='message'>$message</td></tr>";
        if($reply) {
          $output = $output."<tr><td colspan='3' class='reply'><b>Reply</b>:<br/>$reply</td></tr>";
        }
        $output = $output."</table></article>";
        $index++;
        $totaloutput = $output.$totaloutput;
      }
      echo $totaloutput; ?>
    <div id="leftsidebg" style="background-image: url(bg.png);"></div>
    <?php include($_SERVER['DOCUMENT_ROOT'] . '/chromealert.php'); ?>
  </body>
</html>
