{# plugins/plant/growlog/templates/growlog/log_list.html #}
{% extends 'core/base.html' %}

{% block title %}
  {% if plant %}
    Logs for {{ plant.common_name.name }}
  {% else %}
    Recent Grow Logs
  {% endif %}
{% endblock %}

{% block content %}
  <div class="d-flex justify-content-between align-items-center mb-4">
    <h2 class="mb-0">
      {% if plant %}
        Grow Logs for {{ plant.common_name.name }}
      {% else %}
        Recent Grow Logs
      {% endif %}
    </h2>
    <a
      href="{% if plant %}
               {{ url_for('growlog.add_log', plant_uuid=plant.uuid) }}
             {% else %}
               {{ url_for('growlog.add_log') }}
             {% endif %}"
      class="btn btn-success">
      <i class="bi bi-plus-lg"></i> Add Log
    </a>
  </div>

  {% if logs %}
    <div class="list-group">
      {% for log in logs %}
        <div class="list-group-item mb-3">
          <div class="d-flex justify-content-between align-items-start">
            <div>
              <h5 class="mb-1">{{ log.title or 'Untitled' }}</h5>
              <small class="text-muted">
                {{ log.created_at.strftime('%Y-%m-%d %H:%M') }}
              </small>
            </div>
            {% if not plant %}
              <div class="ms-auto text-end">
                <small class="text-secondary">Plant:</small><br>
                <a href="{{ url_for('growlog.list_logs', plant_uuid=log.plant.uuid) }}">
                  {{ log.plant.common_name.name }}
                </a>
              </div>
            {% endif %}
          </div>

          <p class="mt-2 mb-1">{{ log.notes or '—' }}</p>

          <span class="badge {% if log.is_public %}bg-info text-dark{% else %}bg-secondary{% endif %}">
            {% if log.is_public %}Public{% else %}Private{% endif %}
          </span>

          {% if log.media_items.count() %}
            <div class="mt-3 d-flex flex-wrap gap-2">
              {% for media in log.media_items %}
                <img
                  src="{{ generate_image_url(media) }}"
                  class="img-thumbnail"
                  style="max-width: 100px;"
                  alt="{{ media.caption or '' }}"
                >
              {% endfor %}
            </div>
          {% endif %}

          <div class="mt-3">
            <a
              href="{{ url_for(
                        'growlog.edit_log',
                        plant_uuid=plant.uuid if plant else log.plant.uuid,
                        log_id=log.id
                      ) }}"
              class="btn btn-sm btn-outline-primary me-2">
              Edit
            </a>
            <form
              method="POST"
              action="{{ url_for(
                          'growlog.delete_log',
                          plant_uuid=plant.uuid if plant else log.plant.uuid,
                          log_id=log.id
                        ) }}"
              class="d-inline"
              onsubmit="return confirm('Delete this log?');">
              <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
              <button class="btn btn-sm btn-outline-danger">Delete</button>
            </form>
          </div>
        </div>
      {% endfor %}
    </div>
  {% else %}
    <p class="text-muted">
      No grow logs found{% if plant %} for {{ plant.common_name.name }}{% endif %}.
      <a href="{% if plant %}
                 {{ url_for('growlog.add_log', plant_uuid=plant.uuid) }}
               {% else %}
                 {{ url_for('growlog.add_log') }}
               {% endif %}">
        Add one now
      </a>.
    </p>
  {% endif %}
{% endblock %}
