{# partial: list of cases for HTMX search results #}
<div class="list-group">
  {% if cases %}
    {% for case in cases %}
      <div class="list-group-item d-flex justify-content-between align-items-center" data-case-pk="{{ case.pk }}" data-label="{{ case.title|escape }}">
        <div class="flex-fill me-3">
          <div class="d-flex w-100 justify-content-between">
            <h6 class="mb-1">{{ case.title }}</h6>
            <small class="text-muted">{{ case.created_date|date:"Y-m-d" }}</small>
          </div>
          <div class="mb-1">
            <small class="text-muted me-2">{% if case.author.all %}By {{ case.author.all|join:", " }}{% endif %}</small>
            {% for cond in case.condition.all %}
              {% if cond.name %}<span class="badge bg-secondary me-1" title="Condition: {{ cond.name }}">{{ cond.name }}</span>{% endif %}
            {% endfor %}
            {% for pres in case.presentation.all %}
              {% if pres.name %}<span class="badge bg-secondary me-1" title="Presentation: {{ pres.name }}">{{ pres.name }}</span>{% endif %}
            {% endfor %}
            {% for ss in case.subspecialty.all %}
              {% if ss.name %}<span class="badge bg-secondary" title="Subspecialty: {{ ss.name }}">{{ ss.name }}</span>{% endif %}
            {% endfor %}
          </div>

          <div class="mb-1">
          {# show up to three series modalities/examinations if present #}
            {% for s in case.series.all|slice:":3" %}
              {% if s.modality and s.modality.modality or s.examination and s.examination.examination %}
                <span class="badge bg-light text-dark me-1" title="Series: {% if s.modality and s.modality.modality %}{{ s.modality.modality }}{% endif %}{% if s.examination and s.examination.examination %} / {{ s.examination.examination }}{% endif %}">
                  {% if s.modality and s.modality.modality %}{{ s.modality.modality }}{% endif %}{% if s.examination and s.examination.examination %}{% if s.modality and s.modality.modality %} / {% endif %}{{ s.examination.examination }}{% endif %}
                </span>
              {% endif %}
            {% empty %}
              <small class="text-muted">No series</small>
            {% endfor %}
          </div>

          <p class="mb-1 text-truncate">{{ case.description }}</p>
        </div>
        <div class="ms-2 d-flex align-items-center">
          {% if collection %}
            <form class="m-0" hx-post="{% url 'atlas:add_case_to_collection' collection.pk %}" hx-target="#full-question-list" hx-swap="beforeend">
              {% csrf_token %}
              <input type="hidden" name="case" value="{{ case.pk }}">
              <button type="submit" class="btn btn-sm btn-outline-primary">Add</button>
            </form>
            <a class="btn btn-sm btn-outline-secondary ms-2" href="{% url 'atlas:case_detail' case.pk %}" target="_blank" rel="noopener">View</a>
          {% else %}
            <a class="btn btn-sm btn-outline-secondary" href="{% url 'atlas:case_displaysets' case.pk %}">View</a>
          {% endif %}
        </div>
      </div>
    {% endfor %}
  {% else %}
    <div class="list-group-item">No cases found</div>
  {% endif %}
</div>
