<% content_for :title, "Sanasto Wiki" %>

<div class="min-h-screen flex flex-col">
  <%= render "shared/header" %>

  <!-- Flash messages -->
  <% if flash.any? %>
    <div class="max-w-7xl mx-auto px-4 mt-4 w-full">
      <% flash.each do |type, message| %>
        <div class="<%= type == 'notice' ? 'bg-green-50 border border-green-200 text-green-700' : 'bg-red-50 border border-red-200 text-red-700' %> px-4 py-3 rounded-lg mb-4 relative" role="alert">
          <span class="block sm:inline pr-8"><%= message %></span>
          <button type="button" class="absolute top-0 right-0 mt-3 mr-3 text-current opacity-50 hover:opacity-100 transition" onclick="this.parentElement.remove()">
            <svg class="w-4 h-4" fill="currentColor" viewBox="0 0 20 20">
              <path fill-rule="evenodd" d="M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z" clip-rule="evenodd"></path>
            </svg>
          </button>
        </div>
      <% end %>
    </div>
  <% end %>

  <div class="flex-1 flex flex-col">
    <section id="search">
      <div class="max-w-7xl mx-auto px-4 pb-1 space-y-4">
        <%= render "entries/search_form" %>

        <div id="entries_filters" class="hidden">
          <%= render "entries/filters" %>
        </div>
      </div>
    </section>

    <div id="entries_results">
      <%= render "entries/results" %>
    </div>

  </div>
</div>

<script>
  document.addEventListener("click", function (event) {
    const toggle = event.target.closest("[data-toggle-filters]");
    if (!toggle) {
      return;
    }

    const panel = document.getElementById("entries_filters");
    if (!panel) {
      return;
    }

    const isHidden = panel.classList.toggle("hidden");
    toggle.setAttribute("aria-expanded", (!isHidden).toString());
    toggle.textContent = isHidden ? "Show Filters" : "Hide Filters";
  });
</script>
