{% extends "base.html" %}

{% block title %}Objects — {{ service_name }}{% endblock %}

{% block content %}
  {% if search_notes is defined %}
  {% for category, note, start, end in search_notes %}
    {% if category == "error" %}
      {% set alert_class="alert-danger" %}
    {% elif category == "warning" %}
      {% set alert_class="alert-warning" %}
    {% else %}
      {% set alert_class="alert-info" %}
    {% endif %}
    <div class="alert {{ alert_class }} alert-dismissible" role="alert">
      <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
      {% if category == "error" %}
        <strong>Error:</strong>
      {% elif category == "warning" %}
        <strong>Warning:</strong>
      {% else %}
        <strong>Note:</strong>
      {% endif %}
      {{ note }}
      {% if search_query and end is none %}
        {% set end = search_query | length %}
      {% endif %}
      {% if search_query and start != end %}
        <br />
        <strong>Query:</strong>
        <span class="search-error-full">
          {{ search_query[:start] }}<span class="search-error">{{ search_query[start:end] }}</span>{{ search_query[end:] }}
        </span>
      {% endif %}
    </div>
  {% endfor %}
  {% endif %}
  {% if not advanced_search_had_error %}
  {% if search_tree %}
    {% macro render_search_tree(search_tree, depth=0) -%}
      {% if depth > 100 %}
        ...
      {% else %}
      {% if search_tree.input_text %}
      {{ search_tree.input_text }}
      {% elif search_tree | length  == 2 %}
        <span style="display:flex;flex-direction:row;justify-content: center; align-items:center; background-color: rgba(0, 128, 255, 0.075); border-radius:8px;">
          <span style="margin:5px; font-weight: 800;">{{ search_tree[0].operator }}</span>
          {% if search_tree[1].input_text %}
            <span style="margin:5px; ">{{ search_tree[1].input_text }}</span>
          {% else %}
            <span style="margin:5px; border:1px solid rgba(0, 64, 128, 0.5); border-radius:8px;">{{ render_search_tree(search_tree[1], depth=depth+1) }}</span>
          {% endif %}
        </span>
      {% elif search_tree | length  == 3 %}
        <span style="display:flex;flex-direction:row;justify-content: center; align-items:center; background-color: rgba(0, 128, 255, 0.075); border-radius:8px;">
          {% if search_tree[0].input_text %}
            <span style="margin:5px; ">{{ search_tree[0].input_text }}</span>
          {% else %}
            <div style="margin:5px; border:1px solid rgba(0, 64, 128, 0.5); border-radius:8px;">{{ render_search_tree(search_tree[0], depth=depth+1) }}</div>
          {% endif %}
          <span style="margin:5px; font-weight: 800; ">{{ search_tree[1].operator }}</span>
          {% if search_tree[2].input_text %}
            <span style="margin:5px; ">{{ search_tree[2].input_text }}</span>
          {% else %}
            <span style="margin:5px; border:1px solid rgba(0, 64, 128, 0.5); border-radius:8px;">{{ render_search_tree(search_tree[2], depth=depth+1) }}</span>
          {% endif %}
        </span>
      {% endif %}
      {% endif %}
    {%- endmacro %}
    {% if search_tree.input_text %}
      <span style="display:flex; flex-direction:row;justify-content: center;">
        <span style="padding:5px; border:1px solid rgba(0, 64, 128, 0.5); border-radius:8px; background-color: rgba(0, 128, 255, 0.075)">{{ search_tree.input_text }}</span>
      </span>
    {% else %}
      <span style="display:flex; flex-direction:row;justify-content: center;" id="search-tree">
        <span style="margin:5px; border:1px solid rgba(0, 64, 128, 0.5); border-radius:8px;">{{ render_search_tree(search_tree) }}</span>
      </span>
    {% endif %}
  {% endif %}
  {% endif %}

  {% if objects or not search_notes %}
  {% if project_id %}
    {% set object_suffix = " for <a href=\"{}\">Project #{}: {}</a>".format(url_for('.project', project_id=project_id), project_id, project.name | e) | safe %}
  {% elif group_id %}
    {% set object_suffix = " for <a href=\"{}\">Group #{}: {}</a>".format(url_for('.group', group_id=group_id), group_id, group.name | e) | safe %}
  {% elif action_id %}
    {% set object_suffix = " for <a href=\"{}\">Action #{}: {}</a>".format(url_for('.action', action_id=action_id), action_id, action.name | e) | safe %}
  {% elif location_id %}
    {% set object_suffix = " for <a href=\"{}\">Location #{}: {}</a>".format(url_for('.location', location_id=location_id), location_id, location.name | e) | safe %}
  {% elif user_id %}
    {% set object_suffix = " for <a href=\"{}\">User #{}: {}</a>".format(url_for('.user_profile', user_id=user_id), user_id, user.name | e) | safe %}
  {% elif doi %}
    {% set object_suffix = " for Publication <a href=\"https://doi.org/{}\">doi:{}</a>".format(doi, doi) | safe %}
  {% else %}
    {% set object_suffix = "" %}
  {% endif %}
  {% if action_type %}
  <h1>{{ action_type.object_name_plural }}{{ object_suffix }}</h1>
  {% else %}
  <h1>Objects{{ object_suffix }}</h1>
  {% endif %}
  {% endif %}

  {% if objects %}
  <table class="table" id="table-objects">
  <thead>
    <tr>
      <th scope="col" rowspan="2">ID <span class="icons-sorting"><a href="{{ build_modified_url(sortby='_object_id', order='asc') }}"><i class="glyphicon glyphicon glyphicon-triangle-top {% if sorting_property == '_object_id' and sorting_order == 'asc' %}current_sorting_property{% endif %}" aria-hidden="true"></i></a><a href="{{ build_modified_url(sortby='_object_id', order='desc') }}"><i class="glyphicon glyphicon glyphicon-triangle-bottom {% if sorting_property == '_object_id' and sorting_order == 'desc' %}current_sorting_property{% endif %}" aria-hidden="true"></i></a></span></th>
      <th scope="col" rowspan="2">Name <span class="icons-sorting"><a href="{{ build_modified_url(sortby='name', order='asc') }}"><i class="glyphicon glyphicon glyphicon-triangle-top {% if sorting_property == 'name' and sorting_order == 'asc' %}current_sorting_property{% endif %}" aria-hidden="true"></i></a><a href="{{ build_modified_url(sortby='name', order='desc') }}"><i class="glyphicon glyphicon glyphicon-triangle-bottom {% if sorting_property == 'name' and sorting_order == 'desc' %}current_sorting_property{% endif %}" aria-hidden="true"></i></a></span></th>
      <th scope="col" colspan="2">Created</th>
      <th scope="col" colspan="2">Last modified</th>
      {% if show_action %}<th scope="col" rowspan="2">Instrument</th><th scope="col" rowspan="2">Action</th>{% endif %}
      {% for property_name in display_properties %}
        <th scope="col" rowspan="2">{{ display_property_titles[property_name] }}{% if property_name not in ('hazards', 'tags') %} <span class="icons-sorting"><a href="{{ build_modified_url(sortby=property_name, order='asc') }}"><i class="glyphicon glyphicon glyphicon-triangle-top {% if sorting_property == property_name and sorting_order == 'asc' %}current_sorting_property{% endif %}" aria-hidden="true"></i></a><a href="{{ build_modified_url(sortby=property_name, order='desc') }}"><i class="glyphicon glyphicon glyphicon-triangle-bottom {% if sorting_property == property_name and sorting_order == 'desc' %}current_sorting_property{% endif %}" aria-hidden="true"></i></a></span>{% endif %}</th>
      {% endfor %}
    </tr>
    <tr>
      <th scope="col">Date <span class="icons-sorting"><a href="{{ build_modified_url(sortby='_creation_date', order='asc') }}"><i class="glyphicon glyphicon glyphicon-triangle-top {% if sorting_property == '_creation_date' and sorting_order == 'asc' %}current_sorting_property{% endif %}" aria-hidden="true"></i></a><a href="{{ build_modified_url(sortby='_creation_date', order='desc') }}"><i class="glyphicon glyphicon glyphicon-triangle-bottom {% if sorting_property == '_creation_date' and sorting_order == 'desc' %}current_sorting_property{% endif %}" aria-hidden="true"></i></a></span></th>
      <th scope="col">User</th>
      <th scope="col">Date <span class="icons-sorting"><a href="{{ build_modified_url(sortby='_last_modification_date', order='asc') }}"><i class="glyphicon glyphicon glyphicon-triangle-top {% if sorting_property == '_last_modification_date' and sorting_order == 'asc' %}current_sorting_property{% endif %}" aria-hidden="true"></i></a><a href="{{ build_modified_url(sortby='_last_modification_date', order='desc') }}"><i class="glyphicon glyphicon glyphicon-triangle-bottom {% if sorting_property == '_last_modification_date' and sorting_order == 'desc' %}current_sorting_property{% endif %}" aria-hidden="true"></i></a></span></th>
      <th scope="col">User</th>
    </tr>
  </thead>
  <tbody>
  {% for object in objects %}
    <tr>
      <th scope="row"><a href="{{ url_for('.object', object_id=object.object_id) }}">{{ object.object_id }}</a></th>
      <td><a href="{{ url_for('.object', object_id=object.object_id) }}">{{ object.data['name']['text'] }}</a></td>
      <td>{{ object.created_at }}</td>
      <td><a href="{{ url_for('.user_profile', user_id=object.created_by.id) }}">{{ object.created_by.name }}</a></td>
      <td>{{ object.last_modified_at }}</td>
      <td><a href="{{ url_for('.user_profile', user_id=object.modified_by.id) }}">{{ object.modified_by.name }}</a></td>
      {% if show_action %}
      {% if object.action is not none %}
        {% if object.action.instrument is not none %}
          <td><a href="{{ url_for('.instrument', instrument_id=object.action.instrument_id) }}">{{ object.action.instrument.name }}</a></td>
        {% else %}
          <td>&mdash;</td>
        {% endif %}
        <td><a href="{{ url_for('.action', action_id=object.action.id) }}">{{ object.action.name }}</a></td>
      {% else %}
        <td>&mdash;</td>
        <td>&mdash;</td>
      {% endif %}
      {% endif %}
      {% for property_name in display_properties %}
        {% if object.display_properties[property_name] is not none %}
          {% set data = object.display_properties[property_name][0] %}
          {% set schema = object.display_properties[property_name][1] %}
          <td>
          {% include "objects/view/table_any.html" %}
          </td>
        {% else %}
          <td> &mdash; </td>
        {% endif %}
      {% endfor %}
    </tr>
  {% endfor %}
  </tbody>
  </table>
  {% if pagination_enabled %}
    <div>
    Pages:
    <ol class="object-pagination">
    {% if limit and num_objects_found %}
      {% for i in range((num_objects_found+limit-1)//limit) %}
        {% if i * limit == offset %}
          <li>{{ i + 1 }}</li>
        {% else %}
          <li><a href="{{ build_modified_url(limit=limit, offset=i*limit) }}">{{ i + 1 }}</a></li>
        {% endif %}
      {% endfor %}
    {% else %}
      <li>1</li>
    {% endif %}
    </ol>
    </div>
    <div>
    Objects per page:
    <ol class="object-pagination">
    {% for i in (10, 25, 50, 100, 'all') %}
      {% if i == limit or (limit is none and i == 'all') %}
        <li>{{ i }}</li>
      {% else %}
        <li><a href="{{ build_modified_url(limit=i, offset=0) }}">{{ i }}</a></li>
      {% endif %}
    {% endfor %}
    </ol>
    </div>
  {% endif %}
  {% elif not search_notes %}
    <div class="alert alert-warning" role="alert">
    {% if use_advanced_search and advanced_search_had_error %}
    Failed to interpret advanced search query. Please check your query or try the regular search instead.
    {% else %}
    No objects were found.
    {% endif %}
    </div>
  {% endif %}
{% endblock %}