<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />

    <!-- BOOTSTRAP CDN - CSS only -->
    <link
      href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta2/dist/css/bootstrap.min.css"
      rel="stylesheet"
      integrity="sha384-BmbxuPwQa2lc/FVzBcNJ7UAyJxM6wuqIj61tLrc4wSX0szH/Ev+nYRRuWlolflfl"
      crossorigin="anonymous"
    />

    <!-- GOOGLE FONTS CDN -->
    <link
      href="https://fonts.googleapis.com/css2?family=Encode+Sans+Expanded:wght@400;700&display=swap"
      rel="stylesheet"
    />

    <title>Blog</title>
  </head>

  <!-- INTERNAL CSS -->
  <style>
    * {
      font-family: 'Encode Sans Expanded', sans-serif;
    }
  </style>

  <body>
    <div class="container py-4">
      <h1 class="mb-4 text-uppercase">Blog Articles</h1>
      <a href="/article/new" class="btn btn-success">New Article</a>

      <div class="row mt-4">
        <% if (!articles.length) { %>
        <h4 class="text-muted">No articles so far. Check back later.</h4>
        <% } else {%> <% for(const article of articles) { %>
        <div class="col-md-4 mb-4">
          <div class="card">
            <img
              class="card-img-top"
              src="<%= article.image %>"
              alt="<%= article.title %>"
            />
            <div class="card-body">
              <h4 class="card-title"><%= article.title %></h4>
              <p class="text-muted fs-6">
                <%= article.createdAt.toLocaleDateString('en-US', { weekday:
                'long', year: 'numeric', month: 'long', day: 'numeric' }) %>
              </p>
              <p class="card-text"><%= article.body.substring(0, 100) %>...</p>
              <a href="/article/<%= article.slug %> " class="btn btn-dark me-2"
                >Read More</a
              >
              <a
                href="/article/edit/<%= article._id %> "
                class="btn btn-outline-primary me-2"
                >Update</a
              >
              <form
                action="/article/<%= article._id %>?_method=DELETE "
                method="POST"
                class="d-inline"
              >
                <button type="submit" class="btn btn-outline-danger">
                  Delete
                </button>
              </form>
            </div>
          </div>
        </div>
        <% } %> <% } %>
      </div>
    </div>
  </body>
</html>
