<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <title>EazyEatz</title>
  <link rel="icon" href="/imgs/ezblack.png" type="image/png">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/dist/css/bootstrap.min.css">
  <script src="/scripts/index.js"></script>
</head>

<body class="d-flex flex-column min-vh-100">
  <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
    <div class="container-fluid">
      <a class="navbar-brand" href="/"><img class="navbar-brand-image img-fluid" src="/imgs/ezwhite.png" style="height: 40px;;">
        EazyEatz</a>
      <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav"
        aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>
      <div class="collapse navbar-collapse" id="navbarNav">
        <ul class="navbar-nav ms-auto">
          <li class="nav-item">
            <button type="button" class="btn btn-secondary" data-bs-toggle="modal" data-bs-target="#addRecipeModal">
              Create Recipe
            </button>
          </li>
          <li class="nav-item dropdown">
            <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
              Menu
            </a>
            <ul class="dropdown-menu dropdown-menu-dark" aria-labelledby="navbarDropdown">
              <li><a class="dropdown-item" href="https://crimsonhog.github.io/Sprint-Biggie/" target="_blank">About</a></li>
            </ul>
          </li>
        </ul>
      </div>
    </div>
  </nav>

  <div class="container mb-5">
    <h1 class="text-center mt-4">No Ads, No Stories, Just Eatz.</h1>

    <!-- Search form -->
    <form class="mb-3" method="GET" action="/">
      <div class="input-group">
        <input type="text" class="form-control" name="search" placeholder="Search ingredients, names, steps...">
        <button type="submit" class="btn btn-primary">Search</button>
      </div>
    </form>

    <% if (searchQuery) { %>
      <h4>Search Results for "<%= searchQuery %>"</h4>
    <% } %>

    <% if (recipes.length === 0) { %>
      <p>No recipes found.</p>
    <% } else { %>
      <div class="row row-cols-1 row-cols-md-2 row-cols-lg-3 g-4 mb-3">
        <% recipes.forEach(function(recipe) { %>
          <div class="col">
            <a href="/read?recipe=<%= recipe._id %>" class="text-decoration-none text-dark">
              <div class="card">
                <div class="card-header d-flex align-items-center justify-content-center">
                  <h4 class="card-title fw-bold text-center"><%= recipe.recipeName %></h4>
                </div>
                <div class="card-body d-flex flex-column align-items-center">
                  <p class="card-text">Ingredients: <strong><%= recipe.ingredients.length %></strong></p>
                  <p class="card-text">Steps: <strong><%= recipe.steps.length %></strong></p>
                  <p class="card-text">Time: <strong><%= recipe.time %></strong></p>
                </div>
              </div>
            </a>
          </div>
        <% }) %>
      </div>
    <% } %>

    <!-- Modal for adding a recipe -->
    <div class="modal fade" id="addRecipeModal" tabindex="-1" aria-labelledby="addRecipeModalLabel" aria-hidden="true">
      <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title" id="addRecipeModalLabel">New Recipe</h5>
            <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
          </div>
          <div class="modal-body">
            <form action="/create" method="POST">
              <div class="mb-3">
                <label for="recipeName" class="form-label">Recipe Name</label>
                <input type="text" class="form-control" id="recipeName" name="recipeName" required>
              </div>
              <div class="mb-3">
                <label for="authorName" class="form-label">Author Name</label>
                <input type="text" class="form-control" id="authorName" name="authorName" required>
              </div>
              <div class="mb-3">
                <label for="time" class="form-label">Time</label>
                <input type="text" class="form-control" id="time" name="time" required>
              </div>
              <div class="mb-3">
                <label for="ingredients" class="form-label">Ingredients</label>
                <div id="ingredientsContainer">
                  <input type="text" class="form-control mb-2" name="ingredients[]" required>
                </div>
                <button type="button" class="btn btn-secondary mt-2" id="addIngredientBtn">Add Ingredient</button>
              </div>
              <div class="mb-3">
                <label for="steps" class="form-label">Steps</label>
                <div id="stepsContainer">
                  <textarea class="form-control form-control-sm mb-2" name="steps[]" rows="3" required></textarea>
                </div>
                <button type="button" class="btn btn-secondary mt-2" id="addStepBtn">Add Step</button>
              </div>
              <button type="submit" class="btn btn-primary">Submit</button>
            </form>
          </div>
        </div>
      </div>
    </div>
  </div>

  <footer class="bg-light text-center py-3 mt-auto">
    <div class="container">
      <p class="m-0">EazyEatz &copy; 2024</p>
    </div>
  </footer>

  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/dist/js/bootstrap.bundle.min.js"></script>
</body>

</html>