{% import "partials/lists/table.html" as table %}
{% import "partials/icons.html" as icons %}

<div id="roast-list" class="mt-6" data-star-scope="roasts">
  {% if roasts.items.is_empty() && !navigator.has_search() %}
    <div
      class="rounded-lg border border-dashed px-4 py-6 text-sm text-text-secondary"
    >
      <p class="text-center">
        {% if is_authenticated %}
          No roasts added yet. Click Add to create the first roast.
        {% else %}
          No roasts added yet.
        {% endif %}
      </p>
    </div>
  {% else %}
    <section
      class="rounded-lg border bg-surface"
      {% if roasts.has_next() %}
        data-infinite-scroll
        data-next-url="{{ navigator.fragment_page_href(roasts.next_page().unwrap())|safe }}"
        data-target="#roast-list"
      {% endif %}
    >
      {{ table::search_header(navigator, "#roast-list") }}
      <div class="overflow-x-auto">
        <table
          class="responsive-table min-w-full divide-y text-left text-sm text-text"
        >
          <thead
            class="bg-surface-alt text-xs font-semibold text-text-secondary"
          >
            <tr>
              {{ table::sortable_header("Added", "created-at", navigator, "#roast-list") }}
              {{ table::sortable_header("Roast", "name", navigator, "#roast-list") }}
              {{ table::sortable_header("Origin", "origin", navigator, "#roast-list") }}
              <th scope="col" class="px-4 py-3">Tasting Notes</th>
              <th scope="col" class="actions-col px-4 py-3 text-right"></th>
            </tr>
          </thead>
          <tbody class="divide-y/70">
            {% for roast in roasts.items %}
              <tr
                data-star-key="{{ roast.full_id }}"
                data-sort-created-at="{{ roast.created_at_sort_key }}"
                data-sort-name="{{ roast.name }}"
                data-sort-roaster="{{ roast.roaster_label }}"
                data-sort-origin="{{ roast.origin }}"
                data-sort-producer="{{ roast.producer }}"
                class="transition hover:bg-surface-alt"
                onclick="window.location.href='{{ roast.detail_path }}'"
              >
                <td
                  data-label="Added"
                  class="card-date whitespace-nowrap px-4 py-3 font-medium text-text-secondary"
                >
                  <div>{{ roast.created_date }}</div>
                  <div
                    class="hidden md:block text-xs font-normal text-text-muted"
                  >
                    {{ roast.created_time }}
                  </div>
                </td>
                <td data-label="Roast" class="card-title px-4 py-3">
                  <div class="font-medium text-text">{{ roast.name }}</div>
                  <div class="hidden md:block text-xs text-text-muted">
                    {{ roast.roaster_label }}
                  </div>
                </td>
                <td
                  data-label="Roaster"
                  class="px-4 py-3 whitespace-nowrap md:hidden"
                >
                  {{ roast.roaster_label }}
                </td>
                <td data-label="Origin" class="px-4 py-3 whitespace-nowrap">
                  <span
                    >{% if !roast.origin_flag.is_empty() %}
                      <span class="mr-1">{{ roast.origin_flag }}</span>
                    {% endif %}{{ roast.origin }}</span
                  >
                  {% if !roast.producer.is_empty() %}
                    <div class="hidden md:block text-xs text-text-muted">
                      {{ roast.producer }}
                    </div>
                  {% endif %}
                </td>
                {% if !roast.producer.is_empty() %}
                  <td
                    data-label="Producer"
                    class="px-4 py-3 whitespace-nowrap md:hidden"
                  >
                    {{ roast.producer }}
                  </td>
                {% endif %}
                <td data-label="Tasting Notes" class="card-notes px-4 py-3">
                  {% if !roast.tasting_notes.is_empty() %}
                    <div class="flex flex-wrap gap-1">
                      {% for note in roast.tasting_notes %}
                        <span class="{{ note.pill_class }}"
                          >{{ note.label }}</span
                        >
                      {% endfor %}
                    </div>
                  {% else %}
                    <span class="pill pill-muted">No Notes</span>
                  {% endif %}
                </td>
                <td data-label="" class="card-actions px-4 py-3 text-right">
                  <a
                    href="{{ roast.detail_path }}"
                    aria-label="View {{ roast.name }}"
                    class="inline-flex h-8 w-8 items-center justify-center rounded-md text-text-muted transition hover:text-accent hover:bg-surface-alt"
                  >
                    {{ icons::chevron_right("h-5 w-5") }}
                  </a>
                </td>
              </tr>
            {% endfor %}
          </tbody>
        </table>
      </div>
      {% if roasts.items.is_empty() %}
        <div class="p-8 text-center text-text-muted">
          No roasts match the search.
        </div>
      {% endif %}
      {{ table::pagination_header(roasts, navigator, "#roast-list") }}
      {% if roasts.has_next() %}
        <div
          class="infinite-scroll-sentinel h-4 md:hidden"
          aria-hidden="true"
        ></div>
      {% endif %}
    </section>
  {% endif %}
</div>
