{% extends "admin/models/base.html" %}

{% from 'macros/filters.html' import admin_filter %}

{% block extra_script %}
  {{ super() }}
  <script type="text/javascript">
    $(function() {
      var admin_filter = new Admin.ModelAdminFilter();
      admin_filter.bind_listeners();
    });
  </script>
{% endblock %}

{% block content %}
  <p>
    <a class="btn small" href="javascript:void(0);" id="add-filter">Add filter</a>
    <a class="btn small primary" href="{{ url_for(model_admin.get_url_name('add')) }}">Add new {{ model_admin.get_display_name() }}</a>
  </p>
  <div {% if not filters %}style="display:none;" {% endif %}id="filter-wrapper">
    <div class="hidden" id="filter-fields">
    {% for field in form %}
      {{ field(class='field-value') }}
    {% endfor %}
    </div>
    <form method="get" action="." class="modeladmin-filters">
      {% if ordering %}<input type="hidden" name="ordering" value="{{ ordering }}" />{% endif %}
      <ul>
        <li class="hidden" id="template-row">{{ admin_filter(form) }}</li>
        {% for field_name, field_values in raw_filters %}
          {% if field_name in form %}
            {% for field_value in field_values %}
              <li>{{ admin_filter(form, field_name, field_value) }}</li>
            {% endfor %}
          {% endif %}
        {% endfor %}
      </ul>
      <p class="submit">
        <button class="btn primary small" type="submit">Apply</button>
        <a class="btn small" href=".">Reset</a>
        <span class="help-inline">Use the wildcard "*" to partially match text, e.g. <em>*phrase*</em> or <em>starts with*</em></span>
      </p>
    </form>
  </div>
  
  <form method="post" action=".">
  <div class="form-wrapper">
    <table class="zebra-striped">
      <thead>
        <tr>
          <th class="check"><input type="checkbox" name="xxx" onchange="javascript:$('td input[type=checkbox]').attr('checked', this.checked);" /></th>
          {% if model_admin.columns %}
            {% for column in model_admin.columns %}
              {% if ordering == column %}
                {% set sort_by = '-'+column %}
              {% else %}
                {% set sort_by = column %}
              {% endif %}
              <th>{% if model_admin.column_is_sortable(column) %}<a href="./?{{ update_querystring(request.query_string, 'ordering', sort_by)|safe() }}">{% endif %}{{ get_verbose_name(model_admin.model, column) }}{% if model_admin.column_is_sortable(column) %}</a>{% endif %}</th>
            {% endfor %}
          {% else %}
            <th>{{ model_admin.get_display_name() }}</th>
          {% endif %}
        </tr>
      </thead>
      <tbody>
      {% for object in query.get_list() %}
        <tr>
          <td class="check"><input type="checkbox" name="id" value="{{ object.get_pk() }}" /></td>
          {% if model_admin.columns %}
            {% for column in model_admin.columns %}
              {% if loop.index == 1 %}
                <td><a href="{{ url_for(model_admin.get_url_name('edit'), pk=object.get_pk()) }}">{{ get_model_field(object, column) }}</a></td>
              {% else %}
                <td>{{ get_model_field(object, column) }}</td>
              {% endif %}
            {% endfor %}
          {% else %}
            <td><a href="{{ url_for(model_admin.get_url_name('edit'), pk=object.get_pk()) }}">{{ object }}</a></td>
          {% endif %}
        </tr>
      {% endfor %}
        <tr><td colspan="2">
          <button class="btn danger small" type="submit">Delete selected</button>
        </td></tr>
      </tbody>
    </table>
  </div>
  </form>
  {% include "admin/includes/pagination.html" %}
{% endblock %}
