<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Sepo CMS</title>
    <link
      href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap"
      rel="stylesheet"
    />
    <style>
      :root {
        --background: hsl(240, 5%, 6%);
        --foreground: hsl(60, 5%, 90%);
        --primary: hsl(240, 0%, 90%);
        --primary-foreground: hsl(60, 0%, 0%);
        --secondary: hsl(240, 4%, 15%);
        --secondary-foreground: hsl(60, 5%, 85%);
        --accent: hsl(240, 0%, 13%);
        --accent-foreground: hsl(60, 0%, 100%);
        --destructive: hsl(0, 60%, 50%);
        --destructive-foreground: hsl(0, 0%, 98%);
        --muted: hsl(240, 5%, 25%);
        --muted-foreground: hsl(60, 5%, 85%);
        --card: hsl(240, 4%, 10%);
        --card-foreground: hsl(60, 5%, 90%);
        --popover: hsl(240, 5%, 15%);
        --popover-foreground: hsl(60, 5%, 85%);
        --border: hsl(240, 6%, 20%);
        --input: hsl(240, 6%, 20%);
        --ring: hsl(240, 5%, 90%);
        --radius: 0.5rem;
      }

      body,
      html {
        margin: 0;
        padding: 0;
        background-color: var(--background);
        display: flex;
        justify-content: center;
        align-items: flex-start;
        flex-wrap: wrap;
        min-height: 100vh;
        width: 100%;
        box-sizing: border-box;
        font-family: "Roboto", sans-serif;
        color: var(--foreground);
      }

      .container {
        width: 90%;
        max-width: 1200px;
        padding: 2rem;
      }

      .header {
        text-align: center;
        margin-bottom: 2rem;
      }

      .header h1 {
        font-size: 2.5rem;
        font-weight: 700;
        color: var(--primary);
      }

      .dashboard-link {
        display: inline-block;
        margin-top: 1rem;
        padding: 0.5rem 1rem;
        background-color: var(--primary);
        color: var(--primary-foreground);
        text-decoration: none;
        border-radius: var(--radius);
        transition: background-color 0.3s;
      }

      .dashboard-link:hover {
        background-color: var(--secondary);
      }

      .posts {
        display: flex;
        flex-direction: column;
        align-items: center;
      }

      .postDiv {
        background-color: var(--card);
        padding: 2rem;
        margin: 1rem 0;
        width: 100%;
        box-sizing: border-box;
        color: var(--card-foreground);
        border-radius: var(--radius);
        cursor: pointer;
        box-shadow: 0 4px 8px var(--secondary);
        transition: transform 0.2s, box-shadow 0.2s;
        position: relative;
      }

      .postDiv:hover {
        transform: translateY(-5px);
        box-shadow: 0 6px 12px var(--secondary);
      }

      .postDiv::before {
        content: "";
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 5px;
        background-color: var(--accent);
        border-top-left-radius: var(--radius);
        border-top-right-radius: var(--radius);
      }

      .postDiv h2 {
        margin: 0 0 1rem 0;
        font-size: 1.5rem;
        font-weight: 700;
        text-align: center;
        text-transform: uppercase;
      }

      .postDiv p {
        margin: 0;
        font-size: 1rem;
        color: var(--muted-foreground);
        text-align: center;
        font-style: italic;
        font-weight: 400;
      }

      @media (max-width: 768px) {
        .postDiv {
          padding: 1.5rem;
        }

        .postDiv h2 {
          font-size: 1.25rem;
        }

        .postDiv p {
          font-size: 0.875rem;
        }
      }
    </style>
  </head>
  <body>
    <div class="container">
      <div class="header">
        <h1>Forum Posts</h1>
        <a href="/dashboard" class="dashboard-link">Go to Dashboard</a>
      </div>
      <div class="posts"></div>
    </div>

    <script>
      const postsEL = document.querySelector(".posts");

      addEventListener("DOMContentLoaded", loadP);

      function loadP() {
        fetch("/posts/getAll", {
          method: "GET",
        })
          .then((res) => {
            if (!res.ok) {
              postsEL.textContent = "Something Went Wrong";
              return;
            }
            return res.json();
          })
          .then((data) => {
            postHandler(data);
          });
      }

      function postHandler(data) {
        for (let i = 0; i < data.length; i++) {
          let postHtml = `
                <div name="${data[i].postid}" class="postDiv">
                    <h2>${data[i].title}</h2>
                    <p>learn more</p>
                </div>
                `;
          postsEL.innerHTML += postHtml;
        }

        document.querySelectorAll(".postDiv").forEach((div) => {
          div.addEventListener("click", function () {
            window.location.href = `/posts/get/${this.getAttribute("name")}`;
          });
        });
      }
    </script>
  </body>
</html>
