{% import 'macro/form.html' as form %}
<div id="grid-filter" class="collapse {% if config['SHOW_GRID_FILTERS'] %}show{% endif %} row row-cols-lg-auto g-1 justify-content-center align-items-center pb-2">
  <div class="col-12 flex-grow-1">
    <label class="visually-hidden" for="grid-status">Status</label>
    <div class="input-group">
      <span class="input-group-text"><i class="ri-checkbox-line"></i><span class="ms-1 d-none d-md-inline"> Status</span></span>
      <select id="grid-status" class="form-select"
        data-filter="metadata"
        autocomplete="off">
        <option value="" {% if not current_status_filter %}selected{% endif %}>All</option>
        {% if not config['HIDE_TABLE_MISSING_PARTS'] %}
          <option value="has-missing" {% if current_status_filter == 'has-missing' or current_status_filter == '-has-missing' %}selected{% endif %}>Missing pieces</option>
        {% endif %}
        {% if not config['HIDE_TABLE_DAMAGED_PARTS'] %}
          <option value="has-damaged" {% if current_status_filter == 'has-damaged' or current_status_filter == '-has-damaged' %}selected{% endif %}>Damaged pieces</option>
        {% endif %}
        {% if not config['HIDE_SET_INSTRUCTIONS'] %}
          <option value="has-missing-instructions" {% if current_status_filter == 'has-missing-instructions' or current_status_filter == '-has-missing-instructions' %}selected{% endif %}>Missing instructions</option>
        {% endif %}
        {% if brickset_storages | length %}
          <option value="has-storage" {% if current_status_filter == 'has-storage' or current_status_filter == '-has-storage' %}selected{% endif %}>In storage</option>
        {% endif %}
        {% for status in brickset_statuses %}
          <option value="{{ status.as_dataset() }}" {% if current_status_filter == status.as_dataset() or current_status_filter == ('-' + status.as_dataset()) %}selected{% endif %}>{{ status.fields.name }}</option>
        {% endfor %}
      </select>
      {{ form.filter_toggle('grid-status') }}
    </div>
  </div>
  <div class="col-12 flex-grow-1">
    <label class="visually-hidden" for="grid-theme">Theme</label>
    <div class="input-group">
      <span class="input-group-text"><i class="ri-price-tag-3-line"></i><span class="ms-1 d-none d-md-inline"> Theme</span></span>
      <select id="grid-theme" class="form-select"
        data-filter="value" data-filter-attribute="theme"
        autocomplete="off">
        <option value="" {% if not current_theme_filter %}selected{% endif %}>All</option>
        {% for theme in collection.themes %}
        <option value="{{ theme | lower }}" {% if current_theme_filter == (theme | lower) or current_theme_filter == ('-' + (theme | lower)) %}selected{% endif %}>{{ theme }}</option>
        {% endfor %}
      </select>
      {{ form.filter_toggle('grid-theme') }}
    </div>
  </div>
  {% if brickset_owners | length %}
    <div class="col-12 flex-grow-1">
      <label class="visually-hidden" for="grid-owner">Owner</label>
      <div class="input-group">
        <span class="input-group-text"><i class="ri-user-line"></i><span class="ms-1 d-none d-md-inline"> Owner</span></span>
        <select id="grid-owner" class="form-select"
          data-filter="metadata"
          autocomplete="off">
          <option value="" {% if not current_owner_filter %}selected{% endif %}>All</option>
          {% for owner in brickset_owners %}
            <option value="{{ owner.as_dataset() }}" {% if current_owner_filter == owner.as_dataset() %}selected{% endif %}>{{ owner.fields.name }}</option>
          {% endfor %}
        </select>
        {{ form.filter_toggle('grid-owner') }}
      </div>
    </div>
  {% endif %}
  {% if brickset_purchase_locations | length %}
    <div class="col-12 flex-grow-1">
      <label class="visually-hidden" for="grid-purchase-location">Purchase location</label>
      <div class="input-group">
        <span class="input-group-text"><i class="ri-building-line"></i><span class="ms-1 d-none d-md-inline"> Purchase location</span></span>
        <select id="grid-purchase-location" class="form-select"
          data-filter="value" data-filter-attribute="purchase-location"
          autocomplete="off">
          <option value="" {% if not current_purchase_location_filter %}selected{% endif %}>All</option>
          {% for purchase_location in brickset_purchase_locations %}
            <option value="{{ purchase_location.fields.id }}" {% if current_purchase_location_filter == purchase_location.fields.id or current_purchase_location_filter == ('-' + purchase_location.fields.id) %}selected{% endif %}>{{ purchase_location.fields.name }}</option>
          {% endfor %}
        </select>
        {{ form.filter_toggle('grid-purchase-location') }}
      </div>
    </div>
  {% endif %}
  <!-- Force line break here to always have 2 rows: status/theme/owner/purchase_location on row 1, storage/tag/year/clear on row 2 -->
  <div class="w-100"></div>
  {% if brickset_storages | length %}
    <div class="col-12 flex-grow-1">
      <label class="visually-hidden" for="grid-storage">Storage</label>
      <div class="input-group">
        <span class="input-group-text"><i class="ri-archive-2-line"></i><span class="ms-1 d-none d-md-inline"> Storage</span></span>
        <select id="grid-storage" class="form-select"
          data-filter="value" data-filter-attribute="storage"
          autocomplete="off">
          <option value="" {% if not current_storage_filter %}selected{% endif %}>All</option>
          {% for storage in brickset_storages %}
            <option value="{{ storage.fields.id }}" {% if current_storage_filter == storage.fields.id or current_storage_filter == ('-' + storage.fields.id) %}selected{% endif %}>{{ storage.fields.name }}</option>
          {% endfor %}
        </select>
        {{ form.filter_toggle('grid-storage') }}
      </div>
    </div>
  {% endif %}
  {% if brickset_tags | length %}
    <div class="col-12 flex-grow-1">
      <label class="visually-hidden" for="grid-tag">Tag</label>
      <div class="input-group">
        <span class="input-group-text"><i class="ri-price-tag-2-line"></i><span class="ms-1 d-none d-md-inline"> Tag</span></span>
        <select id="grid-tag" class="form-select"
          data-filter="metadata"
          autocomplete="off">
          <option value="" {% if not current_tag_filter %}selected{% endif %}>All</option>
          {% for tag in brickset_tags %}
            <option value="{{ tag.as_dataset() }}" {% if current_tag_filter == tag.as_dataset() or current_tag_filter == ('-' + tag.as_dataset()) %}selected{% endif %}>{{ tag.fields.name }}</option>
          {% endfor %}
        </select>
        {{ form.filter_toggle('grid-tag') }}
      </div>
    </div>
  {% endif %}
  <div class="col-12 flex-grow-1">
    <label class="visually-hidden" for="grid-year">Year</label>
    <div class="input-group">
      <span class="input-group-text"><i class="ri-calendar-line"></i><span class="ms-1 d-none d-md-inline"> Year</span></span>
      <select id="grid-year" class="form-select"
        data-filter="value" data-filter-attribute="year"
        autocomplete="off">
        <option value="" {% if not current_year_filter %}selected{% endif %}>All</option>
        {% for year in collection.years %}
        <option value="{{ year }}" {% if current_year_filter == year or current_year_filter == ('-' + year|string) %}selected{% endif %}>{{ year }}</option>
        {% endfor %}
      </select>
      {{ form.filter_toggle('grid-year') }}
    </div>
  </div>
  <div class="col-12 col-lg-auto">
    <button id="grid-filter-clear" class="btn btn-outline-danger w-100" type="button" title="Clear all filters">
      <i class="ri-filter-off-line"></i> <span class="d-none d-md-inline">Clear</span>
    </button>
  </div>
</div>
